o
    Ni~                     @   s   d Z ddlZddlZddlmZ ddlmZ ddlZddl	Zddl
ZddlmZ ddlmZ dd	lmZ G d
d deZedd ZdS )z
Cloudflare R2.
    N)cache)Optional)Config   )_split_cloud_path   )SchemeClientc                   @   s   e Zd Zejejjejjf ZdZ	de
ddfddZdd Zdee
 fd	d
Zdee fddZdejddfddZdededefddZdS )R2Clientr2resourcereturnNc                 C   s0   t | | t|d\| _| _t | _d | _d S )Nr
   )r   __init__r   bucket_namepath_get_s3_resource_clients3object_info)selfr    r   J/home/ubuntu/.local/lib/python3.10/site-packages/cached_path/schemes/r2.pyr      s   
zR2Client.__init__c              
   C   sp   | j d u r6z| jj| j| jd| _ W d S  tjjy5 } z|jd d dkr0t	d| j
 d| d }~ww d S )N)BucketKeyResponseMetadataHTTPStatusCodei  zFile z
 not found)r   r   head_objectr   r   botocore
exceptionsClientErrorresponseFileNotFoundErrorr   )r   er   r   r   _ensure_object_info"   s   
zR2Client._ensure_object_infoc                 C   "   |    | jd usJ | jdS )NETagr!   r   getr   r   r   r   get_etag+      zR2Client.get_etagc                 C   r"   )NContentLengthr$   r&   r   r   r   get_size0   r(   zR2Client.get_size	temp_filec                 C   s"   |    | jj|| j| jd d S )N)Fileobjr   r   )r!   r   download_fileobjr   r   )r   r+   r   r   r   get_resource5   s   zR2Client.get_resourceindexlengthc                 C   s>   |    | jj| j| jd| d|| d  d}|d  S )Nzbytes=-r   )r   r   RangeBody)r!   r   
get_objectr   r   read)r   r/   r0   r   r   r   r   get_bytes_range9   s
   zR2Client.get_bytes_range)__name__
__module____qualname__r   recoverable_errorsr   r   HTTPClientErrorConnectionErrorschemestrr   r!   r   r'   intr*   ioBufferedWriterr.   bytesr6   r   r   r   r   r	      s    	r	   c                  C   s   t jd} | d u rtdt jd}t jd}t jd}|d ur.|d ur.||d}n|d ur7d|i}ntdtjjdi |}|jd	| d
tdddddS )NR2_ENDPOINT_URLzPR2 endpoint url is not set. Did you forget to set the 'R2_ENDPOINT_URL' env var?
R2_PROFILER2_ACCESS_KEY_IDR2_SECRET_ACCESS_KEY)aws_access_key_idaws_secret_access_keyprofile_namezTo authenticate for R2, you either have to set the 'R2_PROFILE' env var and set up this profile, or set R2_ACCESS_KEY_ID and R2_SECRET_ACCESS_KEY.r   auto
   standard)max_attemptsmode)retries)service_nameendpoint_urlregion_nameconfigr   )	osenvironr%   
ValueErrorboto3sessionSessionclientr   )rQ   rI   access_key_idsecret_access_keysession_kwargs
s3_sessionr   r   r   r   A   s0   
r   )__doc__r@   rT   	functoolsr   typingr   boto3.dynamodbrW   boto3.sessionbotocore.exceptionsr   botocore.configr   commonr   scheme_clientr   r	   r   r   r   r   r   <module>   s    .