o
    }oi
                     @  s   U d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ er-d dlmZ e	 Zd dlZW d   n1 s?w   Y  ed	G d
d dZerad dlmZ edZded< dS dS )    )annotations)BytesIO)TYPE_CHECKING)experimental_class)
try_import)ArtifactNotFound)BinaryIONz3.4.0c                   @  s<   e Zd ZdZ	ddd	d
ZdddZdddZdddZdS )GCSArtifactStorea|  An artifact backend for Google Cloud Storage (GCS).

    Args:
        bucket_name:
            The name of the bucket to store artifacts.

        client:
            A google-cloud-storage ``Client`` to use for storage operations. If not specified, a
            new client will be created with default settings.

    Example:
        .. code-block:: python

            import optuna
            from optuna.artifacts import GCSArtifactStore, upload_artifact


            artifact_backend = GCSArtifactStore("my-bucket")


            def objective(trial: optuna.Trial) -> float:
                ... = trial.suggest_float("x", -10, 10)
                file_path = generate_example(...)
                upload_artifact(
                    artifact_store=artifact_store,
                    file_path=file_path,
                    study_or_trial=trial,
                )
                return ...

        Before running this code, you will have to install ``gcloud`` and run

        .. code-block:: bash

            gcloud auth application-default login

        so that the Cloud Storage library can automatically find the credential.
    Nbucket_namestrclient"google.cloud.storage.Client | NonereturnNonec                 C  s2   t   || _|ptjj | _| j|| _	d S N)
_importscheckr
   googlecloudstorageClientr   bucket
bucket_obj)selfr
   r    r   I/home/ubuntu/.local/lib/python3.10/site-packages/optuna/artifacts/_gcs.py__init__;   s   zGCSArtifactStore.__init__artifact_id
'BinaryIO'c                 C  s<   | j |}|d u rtd| j d| d| }t|S )NzArtifact storage with bucket: z, artifact_id: z was not found)r   get_blobr   r
   download_as_bytesr   )r   r   blobbodyr   r   r   open_readerE   s   zGCSArtifactStore.open_readercontent_bodyc                 C  s"   | j |}| }|| d S r   )r   r!   readupload_from_string)r   r   r$   r!   datar   r   r   writeQ   s   zGCSArtifactStore.writec                 C  s   | j | d S r   )r   delete_blob)r   r   r   r   r   removeV   s   zGCSArtifactStore.remover   )r
   r   r   r   r   r   )r   r   r   r   )r   r   r$   r   r   r   )r   r   r   r   )__name__
__module____qualname____doc__r   r#   r(   r*   r   r   r   r   r	      s    *


r	   )ArtifactStore r/   _)
__future__r   ior   typingr   optuna._experimentalr   optuna._importsr   optuna.artifacts.exceptionsr   r   r   google.cloud.storager   r	   optuna.artifacts._protocolr/   r1   __annotations__r   r   r   r   <module>   s"    
G