o
    }oi                     @  s   U d dl mZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	 er/d dlm
Z
 d dlmZ e Zd dlZd dlmZ W d   n1 sGw   Y  G d	d
 d
ZdddZerjd dlmZ edZded< dS dS )    )annotationsN)TYPE_CHECKING)
try_import)ArtifactNotFound)BinaryIO)S3Client)ClientErrorc                   @  sB   e Zd ZdZ	ddddddZdddZdddZdddZdS )Boto3ArtifactStorea  An artifact backend for Boto3.

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

        client:
            A Boto3 client to use for storage operations. If not specified, a new client will
            be created.

        avoid_buf_copy:
            If True, skip procedure to copy the content of the source file object to a buffer
            before uploading it to S3 ins. This is default to False because using
            ``upload_fileobj()`` method of Boto3 client might close the source file object.

    Example:
        .. code-block:: python

            import optuna
            from optuna.artifacts import upload_artifact
            from optuna.artifacts import Boto3ArtifactStore


            artifact_store = Boto3ArtifactStore("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 ...
    NF)avoid_buf_copybucket_namestrclientS3Client | Noner
   boolreturnNonec                C  s(   t   || _|ptd| _|| _d S )Ns3)_importscheckbucketboto3r   _avoid_buf_copy)selfr   r   r
    r   K/home/ubuntu/.local/lib/python3.10/site-packages/optuna/artifacts/_boto3.py__init__;   s   
zBoto3ArtifactStore.__init__artifact_idr   c              
   C  sn   z| j j| j|d}W n ty) } zt|r$td| j d| d| d }~ww |d}|d us5J |S )NBucketKeyzArtifact storage with bucket: z, artifact_id: z was not foundBody)r   
get_objectr   r   _is_not_found_errorr   get)r   r   objebodyr   r   r   open_readerE   s   
zBoto3ArtifactStore.open_readercontent_bodyc                 C  sB   |}| j st }t|| |d |}| j|| j| d S )Nr   )	r   ioBytesIOshutilcopyfileobjseekr   upload_fileobjr   )r   r   r(   fsrcbufr   r   r   writeS   s   
zBoto3ArtifactStore.writec                 C  s   | j j| j|d d S )Nr   )r   delete_objectr   )r   r   r   r   r   remove\   s   zBoto3ArtifactStore.remove)N)r   r   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'   r1   r3   r   r   r   r   r	      s    &


	r	   r%   r   r   r   c                 C  s8   | j di d}| j di d}|dkp|dkS )NErrorCodeResponseMetadataHTTPStatusCode	NoSuchKeyi  )responser#   )r%   
error_codehttp_status_coder   r   r   r"   `   s   r"   )ArtifactStore r@   _)r%   r   r   r   )
__future__r   r)   r+   typingr   optuna._importsr   optuna.artifacts.exceptionsr   r   mypy_boto3_s3r   r   r   botocore.exceptionsr   r	   r"   optuna.artifacts._protocolr@   rB   __annotations__r   r   r   r   <module>   s&    
K