o
    R+ il                    @   s  d Z ddlZddlZddlZddlZddl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mZmZ ddlmZmZmZmZ ddl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 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& 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. ddl/m0Z0 ddlm1Z1 e2e3Z4dZ5dZ6dZ7dZ8G dd dZ9G dd deZ:G d d! d!e;Z<G d"d# d#e;Z=dS )$z.
This module provides a client class for BOS.
    N)str)bytes)	iteritemsiterkeys
itervalues)ThreadPoolExecutorwaitALL_COMPLETEDFIRST_COMPLETED)bce_client_configuration)utils)bce_v1_signer)BceBaseClient)BceClientError)BceServerError)BceHttpClientError)bce_http_client)handler)http_content_types)http_headers)http_methods)bos)bos_handler)storage_class)required)compats   syncs   asyncZAES256s   httpc                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	UploadTaskHandlez?
    handle to control multi upload file with multi-thread
    c                 C   s   d| _ t | _d S NF)cancel_flag	threadingLockcancel_lockself r$   l/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/baidubce/services/bos/bos_client.py__init__B   s   zUploadTaskHandle.__init__c                 C   s   | j   d| _| j   dS )zI
        cancel putting super object from file with multi-thread
        TNr!   acquirer   releaser"   r$   r$   r%   cancelF   s   
zUploadTaskHandle.cancelc                 C   s   | j   | j}| j   |S )z!
        get cancel flag
        r'   )r#   resultr$   r$   r%   	is_cancelN   s   

zUploadTaskHandle.is_cancelN)__name__
__module____qualname____doc__r&   r*   r,   r$   r$   r$   r%   r   >   s
    r   c                
   @   s  e Zd ZdZdddZdddZeeefdddd	Z	eeefddd
dZ
eeefddddZeeefddddZedd ZeeefeefddddZeeefeddddZeeefddddZeeefddddZeeefddddZeeefddddZeeefddd d!Zeeefddd"d#Zeeefdedfd$d%Zeeefddd&d'Zeeefddd(d)Zeeefed*dd+d,Zeeefddd-d.Zeeefddd/d0Zeeefed1dd2d3Z eeefddd4d5Z!eeefddd6d7Z"eeefddd8d9Z#eeefddd:d;Z$eeefed<dd=d>Z%eeefeefd?dd@dAZ&eeefeefd?ddBdCZ'eeefdddDdEZ(eeefdddFdGZ)eeefdddHdIZ*eeefdddJdKZ+eeefeefdLdMdNddddde,j-fdOdPZ.eeefeefdQ	ddRdSZ/eeefdddTdUZ0eeefdddVdWZ1eeefedX	ddYdZZ2eeefddd[d\Z3eeefddd]d^Z4eeefd		dd`daZ5eeefdddbdcZ6eddde Z7edfdg Z8eeefeefdLddhdiZ9eeefeefdLddkdlZ:eddndoZ;edpdq Z<eeefeefdLddrdsZ=eeefeefeefdt		ddudvZ>eeefeefdLddwdxZ?eeefeefe@eAjBeefdy										ddzd{ZCeeefeefeefd|										dd}d~ZDeeefeefe@eAjBeefdy												dddZEeeefeefeefd													dddZFeeefeefeefd														dddZGeeefeefeefeefd										dddZHeeefeefdLdddZIeeefeddddZJeeefeefeefd		dddZKeeefddddZLeeefddddZMeeefeefdL				dddZNeeefeefeefeOeAjBe@d		dddZPeeefeefeefeefeefeOeAjBeAjBd					dddZQeeefeefeefeOeAjBeefeAjBd		dddZReeefeefeefed			dddZSeeefeefeefddddZTeeefeefeefd		dddZUeeefeefeefddddZVeeefd			dddZWeeefddddZX	dddZYeeefeefeefdt									dddZZeeefeefeefddddZ[eeefeefdL				dddZ\eeefeefdLdddZ]eeefeefdLdddZ^eeefeefeefd			dddZ_eeefeefeefe`d		dddZaeeefeefdÍdddńZbeeefeefefdƍdddȄZcdddʄZdeeOeOdˍddd̈́ZedddτZfeeefddddфZgeeefefdҍdddԄZheeefddddքZieeefefd׍dddلZjeeefddddۄZkeeefdddd݄Zlddd߄ZmdddZndddZodddZpdddZqeeefd		dddZreeefedX	dddZseeefddddZteeefddddZueeefedX	dddZveeefedX	dddZweeefedX	dddZxeeefedX	dddZyeeefedX	dddZzeeefedX	dddZ{eeefedX	dddZ|eeefedX	dddZ}e													dd dZ~ed ddZdd Zed!ddZd	d
 Zdd Zedd Z				d"ddZdS (#  	BosClientz
    sdk client
    Nc                 C   s   t | | d S N)r   r&   r#   configr$   r$   r%   r&   \   s   zBosClient.__init__c                 C   s   | j tj|dS )z
        List buckets of user

        :param config: None
        :type config: BceClientConfiguration
        :returns: all buckets owned by the user.
        :rtype: baidubce.bce_response.BceResponse
        r4   _send_requestr   GETr3   r$   r$   r%   list_buckets_   s   	zBosClient.list_buckets)bucket_namec                 C   s"   ddi}| j tj|||d}|jS )a  
        Get the region which the bucket located in.

        :param bucket_name: the name of bucket
        :type bucket_name: string or unicode
        :param config: None
        :type config: BceClientConfiguration

        :return: region of the bucket
        :rtype: str
        s   location    paramsr4   )r7   r   r8   Zlocation_constraint)r#   r:   r4   r=   responser$   r$   r%   get_bucket_locationj   s   zBosClient.get_bucket_locationc                 C      | j tj||dS )a  
        Create bucket with specific name

        :param bucket_name: the name of bucket
        :type bucket_name: string or unicode
        :param config: None
        :type config: BceClientConfiguration
        :returns:
        :rtype: baidubce.bce_response.BceResponse
        r5   )r7   r   PUTr#   r:   r4   r$   r$   r%   create_bucket{   s   zBosClient.create_bucketc              
   C   s~   z| j tj||d W dS  ty> } z&t|jtr8|jjtj	j
kr)W Y d}~dS |jjtj	jkr8W Y d}~dS |d}~ww )z
        Check whether there is a bucket with specific name

        :param bucket_name: None
        :type bucket_name: str
        :return:True or False
        :rtype: bool
        r5   TNF)r7   r   HEADr   
isinstance
last_errorr   status_codehttpclient	FORBIDDEN	NOT_FOUND)r#   r:   r4   er$   r$   r%   does_bucket_exist   s   
zBosClient.does_bucket_existc                 C      | j tj|ddi|dS )z
        Get Access Control Level of bucket

        :type bucket: string
        :param bucket: None
        :return:
            **json text of acl**
           aclr;   r<   r6   rB   r$   r$   r%   get_bucket_acl      
zBosClient.get_bucket_aclc                 C   s.   i }t | jD ]\}}|ds|||< q|S )N_)r   __dict__
startswith)aclr+   kvr$   r$   r%   _dump_acl_object   s   
zBosClient._dump_acl_object)r:   rU   c                 C   s8   | j tj|tjd|itjdtjt	j
iddi|d dS )z
        Set Access Control Level of bucket

        :type bucket: string
        :param bucket: None

        :type grant_list: list of grant
        :param grant_list: None
        :return:
            **HttpResponse Class**
        accessControlListdefaultrO   r;   bodyheadersr=   r4   Nr7   r   rA   jsondumpsr1   rX   r   CONTENT_TYPEr   JSON)r#   r:   rU   r4   r$   r$   r%   set_bucket_acl   s   


zBosClient.set_bucket_acl)r:   
canned_aclc                 C   s$   | j tj|tj|iddi|d dS )zi

        :param bucket_name:
        :param canned_acl:
        :param config:
        :return:
        rO   r;   r^   r=   r4   N)r7   r   rA   r   BCE_ACL)r#   r:   re   r4   r$   r$   r%   set_bucket_canned_acl   s   	
zBosClient.set_bucket_canned_aclc                 C   >   t |}| jtj|tjd|itjdt	j
tjiddi|dS )N

        :param bucket_name:
        :param config:
        :return:
        ZstorageClassrZ      storageClassr;   r\   r   convert_to_stringr7   r   rA   r`   ra   r1   rX   r   rb   r   rc   )r#   r:   r   r4   r$   r$   r%   set_bucket_storage_class   s   


z"BosClient.set_bucket_storage_classc                 C   rN   )rj   rk   r;   r<   r6   rB   r$   r$   r%   get_bucket_storage_class   s
   z"BosClient.get_bucket_storage_classc                 C   r@   )z
        Delete a Bucket(Must Delete all the Object in Bucket before)

        :type bucket: string
        :param bucket: None
        :return:
            **HttpResponse Class**
        r5   r7   r   DELETErB   r$   r$   r%   delete_bucket   s   
zBosClient.delete_bucketc                 C   sT   i }|dur
||d< |dur||d< | j tj|tj|tjdtjt	j
iddi|dS )a  
        Set index page and not_found 404 page for static website trusteeship

        :type bucket_name: string
        :param bucket_name: None

        :type index:string
        :param index:object name of index page for static website trusteeship

        :type not_found:string
        :param not_found:object name of not_found 404 page for static website trusteeship

        :return:
            **HttpResponse Class**
        NindexZnotFoundrZ      websiter;   r\   r_   )r#   r:   rs   	not_foundr4   r]   r$   r$   r%   put_bucket_static_website  s   
z#BosClient.put_bucket_static_websitec                 C   rN   )z
        Get Information of static website trusteeship

        :type bucket: string
        :param bucket: None

        :return:
            **HttpResponse Class**
        rt   r;   r<   r6   rB   r$   r$   r%   get_bucket_static_website$  
   z#BosClient.get_bucket_static_websitec                 C   rN   )z
        Delete Information of static website trusteeship to be closed

        :type bucket: string
        :param bucket: None

        :return:
            **HttpResponse Class**
        rt   r;   r<   rp   rB   r$   r$   r%   delete_bucket_static_website5  rx   z&BosClient.delete_bucket_static_websitec                 C   ri   )aN  
        Set server encryption for bucket

        :type bucket: string
        :param bucket: None

        :type encryption_algorithm: string
        :param grant_list: server encryption algorithm for bucekt.Now the value of encryption_algorithm
        only is 'AES256'

        :return:
            **HttpResponse Class**
        ZencryptionAlgorithmrZ   
   encryptionr;   r\   rl   )r#   r:   Zencryption_algorithmr4   r$   r$   r%   put_bucket_encryptionF  s   


zBosClient.put_bucket_encryptionc                 C   rN   )z
        Get status of server encryption

        :type bucket: string
        :param bucket: None

        :return:
            **HttpResponse Class**
        rz   r;   r<   r6   rB   r$   r$   r%   get_bucket_encryption^  rx   zBosClient.get_bucket_encryptionc                 C   rN   )z
        Close server encryption

        :type bucket: string
        :param bucket: None

        :return:
            **HttpResponse Class**
        rz   r;   r<   rp   rB   r$   r$   r%   delete_bucket_encryptiono  rx   z"BosClient.delete_bucket_encryption)r:   resourcec                 C   s4   | j tj|tjd|itjdtjt	j
iddi|dS )a  
        Open image copyright protection and set resource

        :type bucket: string
        :param bucket: None

        :type resource: list of  string
        :param grant_list: resource range to be protected

        :return:
            **HttpResponse Class**
        r~   rZ      copyrightProtectionr;   r\   r_   )r#   r:   r~   r4   r$   r$   r%   put_bucket_copyright_protection  s   

z)BosClient.put_bucket_copyright_protectionc                 C   rN   )z
        Get configuration of image copyright protection

        :type bucket: string
        :param grant_list: None

        :return:
            **HttpResponse Class**
        r   r;   r<   r6   rB   r$   r$   r%   get_bucket_copyright_protection  rx   z)BosClient.get_bucket_copyright_protectionc                 C   rN   )z
        Close image copyright protection

        :type bucket: string
        :param bucket: None

        :return:
            **HttpResponse Class**
        r   r;   r<   rp   rB   r$   r$   r%   "delete_bucket_copyright_protection  rx   z,BosClient.delete_bucket_copyright_protection)r:   replicationc                 C   sN   ddi}d|v rt |d |d< | jtj|tj|tjdt	j
tji||dS )a  
        Open cross-region replication

        :type bucket: string
        :param bucket: None

        :type replication: dict
        :type replication: configuration for cross-region replication

        :return:
            **HttpResponse Class**
           replicationr;   id   idrZ   r\   r   convert_to_bytesr7   r   rA   r`   ra   r1   rX   r   rb   r   rc   )r#   r:   r   r4   r=   r$   r$   r%   put_bucket_replication  s   
z BosClient.put_bucket_replicationc                 C   2   ddi}|durt ||d< | jtj|||dS )z
        Get configuration of cross-region replication 

        :type bucket: string
        :param bucket: None

        :type id: string
        :param id: replication rule id

        :return:
            **HttpResponse Class**
        r   r;   Nr   r<   r   r   r7   r   r8   r#   r:   r   r4   r=   r$   r$   r%   get_bucket_replication     z BosClient.get_bucket_replicationc                 C   r   )a   
        Delete configuration of cross-region replication and close it 

        :type bucket: string
        :param bucket: None

        :type id: string
        :param id: replication rule id

        :return:
            **HttpResponse Class**
        r   r;   Nr   r<   r   r   r7   r   rq   r   r$   r$   r%   delete_bucket_replication  r   z#BosClient.delete_bucket_replicationc                 C   r   )a1  
        Get status of cross-region replication,for exapmle 'historyReplicationPercent',
        'latestReplicationTime'

        :type bucket: string
        :param bucket: None

        :type id: string
        :param id: replication rule id

        :return:
            **HttpResponse Class**
        s   replicationProgressr;   Nr   r<   r   r   r$   r$   r%   get_bucket_replication_progress   s   z)BosClient.get_bucket_replication_progressc                 C   s   | j tj|ddd|dS )z
        list configuration of cross-region replication rule

        :type bucket: string
        :param bucket: None

        :return:
            **HttpResponse Class**
        r;   )r   s   listr<   r6   rB   r$   r$   r%   list_bucket_replication  s
   z!BosClient.list_bucket_replication)r:   	inventoryc                 C   s@   t |d }| jtj|tj|tjdt	j
tjid|d|dS )z
        set bucket inventoru

        :type bucket: string
        :param bucket: None

        :type inventory: dict
        :param inventory: configuration for bucket inventory

        :return:
            **HttpResponse Class**
        r   rZ   r;   	   inventoryr   r\   r   )r#   r:   r   r4   Zconf_idr$   r$   r%   put_bucket_inventory'  s   
zBosClient.put_bucket_inventory)r:   inventory_conf_idc                 C       | j tj|dt|d|dS )z
        Get configuration of bucket inventory

        :type bucket: string
        :param bucket: None

        :return:
            **HttpResponse Class**
        r;   r   r<   )r7   r   r8   r   r   r#   r:   r   r4   r$   r$   r%   get_bucket_inventory>  
   zBosClient.get_bucket_inventoryc                 C   r   )z
        Delete configuration of bucket inventory

        :type bucket: string
        :param bucket: None

        :return:
            **HttpResponse Class**
        r;   r   r<   )r7   r   rq   r   r   r   r$   r$   r%   delete_bucket_inventoryN  r   z!BosClient.delete_bucket_inventoryc                 C   rN   )z
        list configuration of bucket inventory

        :type bucket: string
        :param bucket: None

        :return:
            **HttpResponse Class**
        r   r;   r<   r6   rB   r$   r$   r%   list_bucket_inventory^  rx   zBosClient.list_bucket_inventoryc                 C   sF   |dur	t |}| jtj|tjd|itjdt	j
tjiddi|dS )z
        Open bucket trash function

        :type bucket: string
        :param bucket: None

        :type trash_dir: string
        :param trash_dir: directory of trash,optional

        :return:
            **HttpResponse Class**
        NZtrashDirrZ      trashr;   r\   rl   )r#   r:   Z	trash_dirr4   r$   r$   r%   put_bucket_trashn  s   


zBosClient.put_bucket_trashc                 C   rN   )z
        Get status of bucket trash

        :type bucket: string
        :param grant_list: None

        :return:
            **HttpResponse Class**
        r   r;   r<   r6   rB   r$   r$   r%   get_bucket_trash  rx   zBosClient.get_bucket_trashc                 C   rN   )z
        Close bucket trash

        :type bucket: string
        :param bucket: None

        :return:
            **HttpResponse Class**
        r   r;   r<   rp   rB   r$   r$   r%   delete_bucket_trash  rx   zBosClient.delete_bucket_trash)r:   keyr   i  c              
   C   s  t |}t|dks|dkrtd| |	|}	|pi }|p i }|dur(||	_t|	j|	j\}}}|}||j	krC|dt | 7 }||t
j< | |	||}|
tjkr^|
tjkr^tdg}|	jdurk|	j|t
j < t|	j|
|||||||t
j < dt |j||t|df S )	a  
        Get an authorization url with expire time.
        specified  protocol in endpoint > protocal > default protocol in config.

        :type timestamp: int
        :param timestamp: None

        :type expiration_in_seconds: int
        :param expiration_in_seconds: None

        :type options: dict
        :param options: None

        :param is_official_domain: default use not official domain,example: bucket.bj.bcebos.com

        :return:
            **URL string**
        r   s   v1z!generate url the key param error!N   :s   hosts   %s://%s%s?%sF)r   r   len
ValueError_merge_configprotocolr   parse_host_portendpointdefault_portr   ZHOST	_get_pathr   r8   rD   setsecurity_tokenSTS_SECURITY_TOKENlowerr   signcredentialsZAUTHORIZATIONnameZget_canonical_querystring)r#   r:   r   	timestampZexpiration_in_secondsr^   r=   Zheaders_to_signr   r4   Z
httpmethodendpoint_protocolZendpoint_hostendpoint_portZ	full_hostpathr$   r$   r%   generate_pre_signed_url  sD   






z!BosClient.generate_pre_signed_url)r:   rulesc                 C   $   | j tj|dditd|i|dS )z
        Put Bucket Lifecycle
       
        :type bucket: string
        :param bucket: None

        :type rules: list
        :param rules: None

        :return:**Http Response**
        	   lifecycler;   ruler=   r]   r4   r7   r   rA   r`   ra   )r#   r:   r   r4   r$   r$   r%   put_bucket_lifecycle  s   zBosClient.put_bucket_lifecyclec                 C   rN   )z
        Get Bucket Lifecycle

        :type bucket: string
        :param bucket: None

        :return:**Http Response**
        r   r;   r<   r6   rB   r$   r$   r%   get_bucket_lifecycle  
   
zBosClient.get_bucket_lifecyclec                 C   rN   )z
        Delete Bucket Lifecycle
        
        :type bucket: string
        :param bucket: None

        :return:**Http Response**
        r   r;   r<   rp   rB   r$   r$   r%   delete_bucket_lifecycle  r   z!BosClient.delete_bucket_lifecycle)r:   cors_configurationc                 C   r   )z
        Put Bucket Cors
        :type bucket: string
        :param bucket: None

        :type cors_configuration: list
        :param cors_configuration: None

        :return:**Http Response**
           corsr;   ZcorsConfigurationr   r   )r#   r:   r   r4   r$   r$   r%   put_bucket_cors#     zBosClient.put_bucket_corsc                 C   rN   )z~
        Get Bucket Cors

        :type bucket: string
        :param bucket: None

        :return:**Http Response**
        r   r;   r<   r6   rB   r$   r$   r%   get_bucket_cors8  r   zBosClient.get_bucket_corsc                 C   rN   )z
        Delete Bucket Cors

        :type bucket: string
        :param bucket: None

        :return:**Http Response**
        r   r;   r<   rp   rB   r$   r$   r%   delete_bucket_corsG  r   zBosClient.delete_bucket_cors  c                 C   sX   i }|dur
||d< |dur||d< |dur||d< |dur"||d< | j tj|||dS )  
        Get Object Information of bucket

        :type bucket: string
        :param bucket: None

        :type delimiter: string
        :param delimiter: None

        :type marker: string
        :param marker: None

        :type max_keys: int
        :param max_keys: value <= 1000

        :type prefix: string
        :param prefix: None

        :return:
            **_ListObjectsResponse Class**
        N   maxKeys   prefix   marker	   delimiterr<   r6   )r#   r:   max_keysprefixmarker	delimiterr4   r=   r$   r$   r%   list_objectsV  s   zBosClient.list_objectsc                 c   sB    d}	 | j |||||d}|jD ]}|V  q|jr|j}ndS q)

        :param bucket_name:
        :param prefix:
        :param delimiter:
        :param config:
        :return:
        NT)r   r   r   r4   )r   contentsis_truncatedZnext_marker)r#   r:   r   r   r4   r   r>   itemr$   r$   r%   list_all_objects{  s   


zBosClient.list_all_objectsc                 C   sH   | d u rd S t | ttfstdt| dkrtdtjdt|  iS )Nz!range should be a list or a tuple   zrange should have length of 2   bytes=%d-%d)rE   listtuple	TypeErrorr   r   r   RANGE)ranger$   r$   r%   _get_range_header_dict  s   z BosClient._get_range_header_dictc                 C   s   i }|   }tjrg }|D ]\}}| }|||f q|}ttj}|D ]\}}||rB|t	|d }t
||t
|< q'||j_| |_dS )a  Sets response.body to http_response and response.user_metadata to a dict consists of all http
        headers starts with 'x-bce-meta-'.

        :param http_response: the http_response object returned by HTTPConnection.getresponse()
        :type http_response: httplib.HTTPResponse

        :param response: general response object which will be returned to the caller
        :type response: baidubce.BceResponse

        :return: always true
        :rtype bool
        NT)
getheadersr   PY3r   appendrm   r   BCE_USER_METADATA_PREFIXrT   r   Zconvert_to_unicodemetadatauser_metadatadata)http_responser>   r   Zheaders_listZ
temp_headsrV   rW   r   r$   r$   r%   _parse_bos_object  s(   
zBosClient._parse_bos_objectc           	   	   C   s   i }|durt |}d|i}t |}t|dks|dr#tdt|}|dur7|du r2i }||tj< | j	t
j|||||tjdS )x

        :param bucket_name:
        :param key:
        :param range:
        :param config:
        :return:
        N	   versionIdr      /(Key can not be empty or start with '/' .r^   r=   r4   body_parser)r   r   r   rT   r   r1   r   r   BOS_TRAFFIC_LIMITr7   r   r8   r   )	r#   r:   r   r   traffic_limit
version_idr4   query_paramsrange_headerr$   r$   r%   
get_object  s*   




zBosClient.get_objectStandardc              	   C   sl   t |}i }|dur||tj< t |dvrtd|t ||tj< | jt	j
|||ddi|tjdS )zb

        :param bucket_name:
        :param key:
        :param config:
        :return:
        N)r   Z	ExpeditedZLowCostz\invalid tier:{} for restore_object.The valid value is "Standard" or "Expedited" or "LowCost"s   restorer;   r   )r   r   r   ZBOS_RESTORE_DAYSrm   r   formatZBOS_RESTORE_TIERr7   r   POSTr1   r   )r#   r:   r   daysZtierr4   r^   r$   r$   r%   restore_object  s$   
	
zBosClient.restore_object @  c                 C   s^   t |d}z$|rt|jj}t| ||}n| }t||| |   W |  dS |  w )NwbT)	openintr   content_lengthr   make_progress_adaptershutilcopyfileobjclose)r   r>   	file_namebuf_sizeprogress_callbackf	file_sizestreamr$   r$   r%   _save_body_to_file  s   


zBosClient._save_body_to_filec                 C   s   | | | dS )NT)init_from_http_response)r   r>   select_responser$   r$   r%   _parse_select_message  s   zBosClient._parse_select_messagec                 C   s6   t |}| j|||||d}|j }|j  |S )r   )r   r   r4   )r   r   r   r   readr  )r#   r:   r   r   r   r4   r>   sr$   r$   r%   get_object_as_string  s
   



zBosClient.get_object_as_string)r:   r   r  c	                    s   i }	|durt |}d|i}	t |}t|dks|dr#tdt t|}
|dur<|
du r7i }
||
tj< j	t
j|||
|	  fdddS )	a  
        Get Content of Object and Put Content to File

        :type bucket: string
        :param bucket: None

        :type key: string
        :param key: None

        :type file_name: string
        :param file_name: None

        :type range: tuple
        :param range: (0,9) represent get object contents of 0-9 in bytes. 10 bytes date in total.
        :return:
            **HTTP Response**
        Nr   r   r   r   c                    s   t j| | ddS )Nrecv_buf_size)r  )r1   r	  _get_config_parameterr   r>   r4   r  r  r#   r$   r%   <lambda>F  s    
z.BosClient.get_object_to_file.<locals>.<lambda>r   )r   r   r   rT   r   r1   r   r   r   r7   r   r8   )r#   r:   r   r  r   r4   r  r   r   r   r   r$   r  r%   get_object_to_file  s,   




zBosClient.get_object_to_filec                 C   >   i }|durt |}d|i}t |}| jtj||||dS )z
        Get head of object

        :type bucket: string
        :param bucket: None

        :type key: string
        :param key: None
        :return:
            **_GetObjectMetaDataResponse Class**
        Nr   r<   )r   r   r7   r   rD   r#   r:   r   r   r4   r   r$   r$   r%   get_object_meta_dataN     

zBosClient.get_object_meta_data)r:   r   r   r   content_md5c                 C   s   t |}t |}| j||||	||
|||d	}|tjkr$tdtj ddi}|dur0||d< |r8t||}| jt	j
||||||dS )a4  
        Put an appendable object to BOS or add content to an appendable object

        :type bucket: string
        :param bucket: None

        :type key: string
        :param key: None

        :type content_length: long
        :type offset: long
        :return:
            **HTTP Response**
        	r   r  content_typecontent_sha256r   r   user_headersr   object_taggingDObject length should be less than %d. Use multi-part upload instead.s   appendr;   Ns   offsetr\   )r   r   _prepare_object_headersr   ZMAX_APPEND_OBJECT_LENGTHr   r   r   r7   r   r   )r#   r:   r   r   r  r   offsetr  r   r  r   r  r  r   r  r4   r^   r=   r$   r$   r%   append_objectc  s>   
 

zBosClient.append_object)r:   r   r   c                 C   s   t |}t|tr|tj}d}z3t|}|du r't	j
|| |dd}| j||||t||||||	|
|||dW |durE|  S S |durO|  w w )z
        Create an appendable object and put content of string to the object
        or add content of string to an appendable object
        Nr  r  )r:   r   r   r  r   r"  r  r   r  r   r  r  r   r4   )r   r   rE   r   encodebaidubceDEFAULT_ENCODINGioBytesIOr   get_md5_from_fpr  r#  r   r  )r#   r:   r   r   r  r"  r  r   r  r   r  r  r   r4   fpr$   r$   r%   append_object_from_string  s<   





z#BosClient.append_object_from_stringc                 C   s|   t |}t |}| j||||||	|
||d	}| |d}|tjkr*tdtj |r2t||}| j	t
j|||||dS )al  
        Put object and put content of file to the object

        :type bucket: string
        :param bucket: None

        :type key: string
        :param key: None

        :type fp: FILE
        :param fp: None

        :type file_size: long
        :type offset: long
        :type content_length: long
        :return:
            **HTTP Response**
        r  r  r   )r]   r^   r4   )r   r   r!  r  r   MAX_PUT_OBJECT_LENGTHr   r   r   r7   r   rA   )r#   r:   r   r   r   r  r  r  r   r   r  
encryptioncustomer_keycustomer_key_md5r  r   r  r4   r^   r  r$   r$   r%   
put_object  s8   
&

zBosClient.put_object)bucketr   r   c                 C   s   t |}t|tr|tj}d}z8t|}|du r't	j
|| |dd}| j|||ft|||||||	|
||||||dW |durJ|  S S |durT|  w w )af  
        Create object and put content of string to the object

        :type bucket: string
        :param bucket: None

        :type key: string
        :param key: None

        :type input_content: string
        :param input_content: None

        :type options: dict
        :param options: None
        :return:
            **HTTP Response**
        Nr  r$  r   r  r  r  r   r   r  r.  r/  r0  r  r   r  r4   )r   r   rE   r   r%  r&  r'  r(  r)  r   r*  r  r1  r   r  )r#   r2  r   r   r  r  r  r   r   r  r.  r/  r0  r  r   r  r4   r+  r$   r$   r%   put_object_from_string  s>   
 




z BosClient.put_object_from_string)r2  r   r  c                 C   s   t |}t|d}zL|du r|dtj | }|d |du r1| |d}tj	|||d}|du r:t
|}| j|||f||||||	|
|||||||dW |  S |  w )a^  
        Put object and put content of file to the object

        :type bucket: string
        :param bucket: None

        :type key: string
        :param key: None

        :type file_name: string
        :param file_name: None

        :type options: dict
        :param options: None
        :return:
            **HttpResponse Class**
        rbNr   r  )lengthr  r3  )r   r   r   seekosSEEK_ENDtellr  r   r*  guess_content_type_by_file_namer1  r  )r#   r2  r   r  r   r  r  r  r   r   r  r.  r/  r0  r  r   r  r4   r+  r  r$   r$   r%   put_object_from_fileT  s>   
#


zBosClient.put_object_from_file)source_bucket_name
source_keytarget_bucket_name
target_keyc              
   C   s   t |}t |}| j||||	||d}tdt ||f d}|dur/|dt |  }||tj< |dur=||tj< |dusE|durKd|tj< nd|tj< |
durlz	t	
||
d}W n tyk } z|d}~ww | jtj||||tjd	S )
a{  
        Copy one object to another object

        :type source_bucket: string
        :param source_bucket: None

        :type source_key: string
        :param source_key: None

        :type target_bucket: string
        :param target_bucket: None

        :type target_key: string
        :param target_key: None
        :return:
            **HttpResponse Class**
        )r  r   r   r  r   r     /%s/%sFNs   ?versionId=%ss   replaces   copyT)r^   r4   r   )r   r   r!  r   normalize_stringr   BCE_COPY_SOURCEBCE_COPY_SOURCE_IF_MATCHZBCE_COPY_METADATA_DIRECTIVEr1   _get_user_header	Exceptionr7   r   rA   r   Zparse_copy_object_response)r#   r=  r>  r?  r@  etagr  r   r   r  Zcopy_object_user_headersr   r  Zsource_version_idr4   r^   Zmerge_source_keyrL   r$   r$   r%   copy_object  sP   
"



zBosClient.copy_objectc                 C   r  )z
        Delete Object

        :type bucket: string
        :param bucket: None

        :type key: string
        :param key: None
        :return:
            **HttpResponse Class**
        Nr   r<   r   r  r$   r$   r%   delete_object  r  zBosClient.delete_object)r:   key_listc                 C   s2   dd |D }| j tj|td|iddi|dS )z
        Delete Multiple Objects

        :type bucket: string
        :param bucket: None

        :type key_list: string list
        :param key_list: None
        :return:
            **HttpResponse Class**
        c                 S   s   g | ]	}d t |iqS r   )r   rm   ).0rV   r$   r$   r%   
<listcomp>   s    z5BosClient.delete_multiple_objects.<locals>.<listcomp>objectss   deleter;   r]   r=   r4   r7   r   r   r`   ra   )r#   r:   rJ  r4   Zkey_list_jsonr$   r$   r%   delete_multiple_objects  s   z!BosClient.delete_multiple_objects)source_buckettarget_buckettarget_prefixc              	   C   s&   | j tj|ddit||d|dS )z
        Put Bucket Logging

        :type source_bucket: string
        :param source_bucket: None

        :type target_bucket: string
        :param target_bucket: None
        :return:
            **HttpResponse Class**
           loggingr;   )ZtargetBucketZtargetPrefixr   r   )r#   rR  rS  rT  r4   r$   r$   r%   put_bucket_logging  s   zBosClient.put_bucket_loggingc                 C   rN   )z
        Get Bucket Logging

        :type bucket_name: string
        :param bucket_name: None
        :return:
            **HttpResponse Class**
        rU  r;   r<   r6   rB   r$   r$   r%   get_bucket_logging!  r   zBosClient.get_bucket_loggingc                 C   rN   )z
        Delete Bucket Logging

        :type bucket_name: string
        :param bucket_name: None
        :return:
            **HttpResponse Class**
        rU  r;   r<   rp   rB   r$   r$   r%   delete_bucket_logging0  r   zBosClient.delete_bucket_loggingc           	   
   C   s   t |}i }|dur||tj< |durt||tj< ntj|tj< |dur?z	t	
||d}W n ty> } z|d}~ww | jtj|||ddi|dS )z
        Initialize multi_upload_file.

        :type bucket: string
        :param bucket: None

        :type key: string
        :param key: None
        :return:
            **HttpResponse**
        NF   uploadsr;   rf   )r   r   r   BOS_STORAGE_CLASSr   convert_to_standard_stringrb   r   OCTET_STREAMr1   rE  rF  r7   r   r   )	r#   r:   r   r  r   r  r4   r^   rL   r$   r$   r%   initiate_multipart_upload?  s,   

z#BosClient.initiate_multipart_upload)r:   r   	upload_idpart_number	part_sizepart_fpc              	   C   s   t |}|tjk s|tjkrtd|tjtjf |tjkr&tdtj tj|tj	t
ji}|dur8||tj< |rAt|||}|	durJ|	|tj< | jtj||||||d|
dS )a?  
        Upload a part.

        :type bucket: string
        :param bucket: None

        :type key: string
        :param key: None

        :type upload_id: string
        :param upload_id: None

        :type part_number: int
        :param part_number: None

        :type part_size: int or long
        :param part_size: None

        :type part_fp: file pointer
        :param part_fp: not None

        :type part_md5: str
        :param part_md5: None

        :type config: dict
        :param config: None

        :return:
               **HttpResponse**
        z9Invalid part_number %d. The valid range is from %d to %d.z+Single part length should be less than %d. Ns
   partNumber   uploadIdr\   )r   r   r   ZMIN_PART_NUMBERZMAX_PART_NUMBERr   r-  r   CONTENT_LENGTHrb   r   r\  CONTENT_MD5r   r   r   r7   r   rA   )r#   r:   r   r^  r_  r`  ra  part_md5r  r   r4   r^   r$   r$   r%   upload_partj  s6   
'



zBosClient.upload_part)r=  r>  r?  r@  r^  r_  r`  r"  c                 C   s   t |}t |}| j|
||d}tdt ||f d|tj< d||| d f }||tj< |	dur:|	|tj< | j	t
j|||||d|dS )	a  
        Copy part.

        :type source_bucket_name: string
        :param source_bucket_name: None

        :type source_key: string
        :param source_key: None

        :type target_bucket_name: string
        :param target_bucket_name: None

        :type target_key: string
        :param target_key: None

        :type upload_id: string
        :param upload_id: None

        :return:
            **HttpResponse**
        )r  r   r   rA  Fr      Nrb  rf   )r   r   r!  r   rB  r   rC  ZBCE_COPY_SOURCE_RANGErD  r7   r   rA   )r#   r=  r>  r?  r@  r^  r_  r`  r"  rG  r  r   r   r4   r^   r   r$   r$   r%   upload_part_copy  s2   
&




zBosClient.upload_part_copy)r:   r   r^  r_  r`  r  r"  c                 C   sR   t |}t|d}z|| | j||||||||	|
|d
W |  S |  w )z

        :param bucket_name:
        :param key:
        :param upload_id:
        :param part_number:
        :param part_size:
        :param file_name:
        :param offset:
        :param part_md5:
        :param config:
        :return:
        r5  )rf  r  r   r4   )r   r   r   r7  rg  r  )r#   r:   r   r^  r_  r`  r  r"  rf  r  r   r4   r  r$   r$   r%   upload_part_from_file  s   


zBosClient.upload_part_from_file)r:   r   r^  	part_listc           	      C   sB   t |}| jtj||d}| jtj||t	d|i|d|idS )ac  
        After finish all the task, complete multi_upload_file.

        :type bucket: string
        :param bucket: None

        :type key: string
        :param key: None

        :type upload_id: string
        :param upload_id: None

        :type part_list: list
        :param part_list: None

        :return:
            **HttpResponse**
        )r  r   r  partsrc  )r]   r^   r=   )
r   r   r!  r   rc   r7   r   r   r`   ra   )	r#   r:   r   r^  rk  r   r  r4   r^   r$   r$   r%   complete_multipart_upload
  s   
z#BosClient.complete_multipart_upload)r:   r   r^  c                 C   s"   t |}| jtj||d|idS )a  
        Abort upload a part which is being uploading.

        :type bucket: string
        :param bucket: None

        :type key: string
        :param key: None

        :type upload_id: string
        :param upload_id: None
        :return:
            **HttpResponse**
        rc  )r=   r   )r#   r:   r   r^  r4   r$   r$   r%   abort_multipart_upload3  s   
z BosClient.abort_multipart_uploadc                 C   sH   t |}d|i}|dur||d< |dur||d< | jtj||||dS )a  
        List all the parts that have been upload success.

        :type bucket: string
        :param bucket: None

        :type key: string
        :param key: None

        :type upload_id: string
        :param upload_id: None

        :type max_parts: int
        :param max_parts: None

        :type part_number_marker: string
        :param part_number_marker: None
        :return:
            **_ListPartsResponse Class**
        rc  Ns   maxPartss   partNumberMarkerr<   r   )r#   r:   r   r^  Z	max_partspart_number_markerr4   r=   r$   r$   r%   
list_partsG  s   
zBosClient.list_partsc                 c   sJ    t |}d}	 | j|||||d}|jD ]}|V  q|js!dS |j}q	)z|

        :param bucket_name:
        :param key:
        :param upload_id:
        :param config:
        :return:
        NT)ro  r4   )r   r   rp  rl  r   Znext_part_number_marker)r#   r:   r   r^  r4   ro  r>   r   r$   r$   r%   list_all_partsh  s   



zBosClient.list_all_partsc                 C   s\   ddi}|dur||d< |dur||d< |dur||d< |dur$||d< | j tj|||dS )	a|  
        List all Multipart upload task which haven't been ended.(Completed Init_MultiPartUpload
        but not completed Complete_MultiPartUpload or Abort_MultiPartUpload)

        :type bucket: string
        :param bucket: None

        :type delimiter: string
        :param delimiter: None

        :type max_uploads: int
        :param max_uploads: <=1000

        :type key_marker: string
        :param key_marker: None

        :type prefix: string
        :param prefix: None

        :type upload_id_marker: string
        :param upload_id_marker:
        :return:
            **_ListMultipartUploadResponse Class**
        rY  r;   Nr   s
   maxUploadss	   keyMarkerr   r<   r6   )r#   r:   Zmax_uploads
key_markerr   r   r4   r=   r$   r$   r%   list_multipart_uploads}  s   z BosClient.list_multipart_uploadsc                 c   sl    d}	 | j |||||d}|jD ]}|V  q|jsdS |jdur%|j}nt|jdkr3|jd j}ndS q)r   NT)rr  r   r   r4   r   )rs  Zuploadsr   Znext_key_markerr   r   )r#   r:   r   r   r4   rr  r>   r   r$   r$   r%   list_all_multipart_uploads  s&   


z$BosClient.list_all_multipart_uploadsc                 C   s   |	  rtd| d S z#| j||||||||
|d	}|||jjd td| W d S  tyG } z
td| |d }~ww )Nz(upload task canceled with partNumber={}!)r  r   )
partNumberZeTagz'upload task success with partNumber={}!z&upload task failed with partNumber={}!)	r,   _loggerdebugr   rj  r   r   rG  rF  )r#   r:   Z
object_keyr^  r_  r`  r  r"  rk  uploadTaskHandler  r   r>   rL   r$   r$   r%   _upload_task  s$   

zBosClient._upload_task   c                 C   s  |dks|dkrt dtj|}|dkrt d|du s"|dkr&t }|d d }|| }|| dkr:|d7 }|du rAt }| j|||||	d	j}t	|}g }d}d}g }|dkr||k rb|}|
| j||||||||||
|}|| ||8 }||7 }|d7 }|dks\t|td
 | rtd | j|||d dS t||krtd | j|||d dS |jdd d | |||| dS )zn
        Multipart Upload file to bos

        param chunk_size: part size , default part size is 5MB
        i   r   zEchunk size is valid, it should be more than 0 and not nore than 5120!l       PC z'File size must not be more than 48.8TB!Nrh  i   )r  r   r  )return_whenz!putting super object is canceled!)r^  Fzputting super object failed!c                 S   s   | d S )Nrv  r$   )xr$   r$   r%   r    s    z6BosClient.put_super_obejct_from_file.<locals>.<lambda>rK  T)r   r8  r   getsizemultiprocessing	cpu_countr   r]  r^  r   submitrz  r   r   r	   r,   rw  rx  rn  r   sortrm  )r#   r:   r   r  
chunk_sizeZ
thread_numry  r  r   r  r  r   r4   Z	left_sizer`  Z
total_partr^  executor	all_tasksr"  r_  rk  Z	temp_taskr$   r$   r%   put_super_obejct_from_file  s`   



z$BosClient.put_super_obejct_from_file)r:   r   rU   c              	   C   sD   t |}| jtj||tjd|itjdt	j
tjiddi|d dS )z
        Set Access Control Level of object

        :type bucket: string
        :param bucket: None

        :type acl: list of grant
        :param acl: None
        :return:
            **HttpResponse Class**
        rY   rZ   rO   r;   r\   Nr   )r#   r:   r   rU   r4   r$   r$   r%   set_object_acl  s   



zBosClient.set_object_aclc           	      C   s   t |}d}d}|durtjt |i}|d7 }|dur)tjt |i}|d7 }|dur9tjt |i}|d7 }|dkrAtd|dkrItd| jtj	|||ddi|d	 dS )
a  

        :type bucket_name: string
        :param bucket_name: None

        :type key: string
        :param key: None

        :type canned_acl: string
        :param canned_acl: for header 'x-bce-acl', it's value only is
        canned_acl.PRIVATE or canned_acl.PRIVATE_READ

        :type grant_read: string
        :param grant_read: Object id of getting READ right permission.
        for exapmle,grant_read = 'id="6c47...4c94",id="8c42...4c94"'

        :type grant_full_control: string
        :param grant_full_control: Object id of getting READ right permission.
        for exapmle,grant_full_control = 'id="6c47...4c94",id="8c42...4c94"'

        :param config:
        :return:
            **HttpResponse Class**
        Nr   rh  z,donn't give any object canned acl arguments!r   z5cann't get more than one object canned acl arguments!rO   r;   rf   )
r   r   r   rg   ZBOS_GRANT_READZBOS_GRANT_FULL_CONTROLr   r7   r   rA   )	r#   r:   r   re   Z
grant_readZgrant_full_controlr4   r^   num_argsr$   r$   r%   set_object_canned_acl/  s.   

zBosClient.set_object_canned_aclc                 C   $   t |}| jtj||ddi|dS )z
        Get Access Control Level of object

        :type bucket: string
        :param bucket: None

        :type key: string
        :param key: None

        :return:
            **HttpResponse Class**
        rO   r;   r<   r   r#   r:   r   r4   r$   r$   r%   get_object_aclf     
zBosClient.get_object_aclc                 C   r  )z
        Get Access Control Level of  object

        :type bucket: string
        :param bucket: None

        :type key: string
        :param key: None

        :return:
            **HttpResponse Class**
        rO   r;   r<   r   r  r$   r$   r%   delete_object_acl|  r  zBosClient.delete_object_acl)r:   r   urlc                 C   s^   t |}i }t ||tj< |dur||tj< |dur!||tj< | jtj|||ddi|dS )a  
        fetch object with given url and save to Baidu object storage

        :type bucket: string
        :param bucket: None

        :type key: string
        :param key: object name to be saved

        :type url:string
        :param url: url of resource to be fetched

        :type fetch_mode:string
        :param fetch_mode: fetch mode for get resource, valid value only is
        'sync' and 'async'

        :return:
            **HttpResponse Class**
        Ns   fetchr;   rf   )	r   r   r   ZBOS_FETCH_SOURCEZBOS_FETCH_MODErZ  r7   r   r   )r#   r:   r   r  Z
fetch_moder   r4   r^   r$   r$   r%   fetch_object  s   


zBosClient.fetch_object)r:   r@  symlinkforbid_overwritec
                 C   s   t |}t |}|du rt|}| j|||d}
||
tj< |dur1|r,d|
tj< nd|
tj< |dur=t ||
tj< | j	t
j|||
ddi|	dS )a  
        put object symlink

        :type bucket: string
        :param bucket: None

        :type key: string
        :type key: object name

        :type symlink: string
        :type symlink_key: symlink name

        :return:
            **HttpResponse Class**
        N)r   r  r   s   trues   false   symlinkr;   rf   )r   r   r   r;  r!  r   ZBOS_SYMLINK_TARGETZBOS_FORBID_OVERWRITEZBOS_SYMLINK_BUCKETr7   r   rA   )r#   r:   r@  r  r  r   r   rS  r  r4   r^   r$   r$   r%   put_object_symlink  s,   




zBosClient.put_object_symlink)r:   r  c                 C   s$   t |}| jtj||ddi|dS )z
        Get symlink info

        :type bucket: string
        :param bucket: None

        :type symlink: string
        :param symlink: symlink

        :return:
            **HttpResponse Class**
        r  r;   r<   r   )r#   r:   r  r4   r   r$   r$   r%   get_object_symlink  r  zBosClient.get_object_symlink)r:   r   select_object_argsc                    s   t |}|pi }d|v rd|d v rd}nd|v r#d|d v r#d}nd}t  | jtj||tjd|itj	d|d	|d
| fddd  S )a-  

        :type bucket_name: string
        :param bucket_name: bucket name

        :type key: string
        :param key: object name

        :type select_object_args: dict
        :param select_object_args: requesta parameters for select object api

        :param config:
        :return:
        ZinputSerializationr`   s   jsoncsvs   csvs   parquetZselectRequestrZ   r;   )s   selects   typec                    s   t | | S r2   )r1   r  r  r  r$   r%   r  	  s    z)BosClient.select_object.<locals>.<lambda>)r]   r^   r=   r4   r   )
r   r   SelectResponser7   r   r   r`   ra   r1   rX   )r#   r:   r   r  r^   r4   Zselect_typer$   r  r%   select_object  s&   

zBosClient.select_objectc                 C      | j tjddi|dS )zI
        get user quota

        :param config:
        :return:
        	   userQuotar;   r<   r6   r3   r$   r$   r%   get_user_quota	     zBosClient.get_user_quota)max_bucket_countmax_capacity_mega_bytesc                 C   s$   | j tjt||dddi|dS )a  
        put user quota

        :type max_bucket_count: int
        :param max_bucket_count: max bucket count

        :type max_capacity_mega_bytes: long
        :param max_capacity_mega_bytes: max capacity mega bytes

        :param config:
        :return:
        )ZmaxBucketCountZmaxCapacityMegaBytesr  r;   rO  r   )r#   r  r  r4   r$   r$   r%   put_user_quota(	  s   zBosClient.put_user_quotac                 C   r  )zL
        delete user quota

        :param config:
        :return:
        r  r;   r<   rp   r3   r$   r$   r%   delete_user_quota<	  r  zBosClient.delete_user_quotac                 C   rN   )z
        get notification

        :type bucket_name: string
        :param bucket_name: bucket name

        :param config:
        :return:
           notificationr;   r:   r=   r4   r6   rB   r$   r$   r%   get_notificationG	  s   zBosClient.get_notification)r:   notificationsc                 C   s$   | j tj|td|iddi|dS )z
        put user quota

        :type bucket_name: string
        :param bucket_name: bucket

        :type notifications: list of dict
        :param notifications: notifacation param

        :param config:
        :return:
        r  r  r;   r:   r]   r=   r4   r   )r#   r:   r  r4   r$   r$   r%   put_notificationW	  s
   zBosClient.put_notificationc                 C   rN   )z
        delete notification

        :type bucket_name: string
        :param bucket_name: bucket name

        :param config:
        :return:
        r  r;   r  rp   rB   r$   r$   r%   delete_notificationi	  s   zBosClient.delete_notification)r:   mirror_argsc                 C   *   | j tj|tjd|itjdddi|dS )z
        put bucket mirroring

        :type bucket_name: string
        :param bucket_name: bucket name

        :param mirror_args: mirror conf
        :return:
        ZbucketMirroringConfigurationrZ   	   mirroringr;   r  r7   r   rA   r`   ra   r1   rX   )r#   r:   r  r4   r$   r$   r%   put_bucket_mirroringx	  s   zBosClient.put_bucket_mirroringc                 C   rN   )z
        get bucket mirroring

        :type bucket_name: string
        :param bucket_name: bucket name

        :return:
        r  r;   r  r6   rB   r$   r$   r%   get_bucket_mirroring	  rQ   zBosClient.get_bucket_mirroringc                 C   rN   )z
        delete bucket mirroring

        :type bucket_name: string
        :param bucket_name: bucket name

        :return:
        r  r;   r  rp   rB   r$   r$   r%   delete_bucket_mirroring	  rQ   z!BosClient.delete_bucket_mirroringc                 C   s(   | j tj||tj|tjdddi|dS )  
        put object tagging

        :type bucket_name: string
        :param bucket_name: bucket name

        :type key: string
        :param key: object name

        :type obj_tag_args: dict
        :param obj_tag_args: object tagging args

        :return:
        rZ      taggingr;   )r:   r   r]   r=   r4   r  )r#   r:   r   Zobj_tag_argsr4   r$   r$   r%   put_object_tagging	  s   zBosClient.put_object_taggingc                 C   s0   i }t ||tj< | jtj|||ddi|dS )r  r  r;   )r:   r   r^   r=   r4   )r   r   r   BOS_TAGGINGr7   r   rA   )r#   r:   r   Z
tag_headerr4   r^   r$   r$   r%   put_object_tagging_canned	  s   z#BosClient.put_object_tagging_cannedc                 C   s   | j tj||ddi|dS )z
        put object tagging

        :type bucket_name: string
        :param bucket_name: bucket name

        :type key: string
        :param key: object name

        :return:
        r  r;   )r:   r   r=   r4   r6   r  r$   r$   r%   get_object_tagging	  s   zBosClient.get_object_taggingc                 C   r  )z
        put bucket versioning

        :type bucket_name: string
        :param bucket_name: bucket name

        :type status: string
        :param key: version status:disable/enabled/suspended

        :return:
        statusrZ   
   versioningr;   r  r  )r#   r:   r  r4   r$   r$   r%   put_bucket_versioning	  s   zBosClient.put_bucket_versioningc                 C   rN   )z
        get bucket versioning

        :type bucket_name: string
        :param bucket_name: bucket name

        :return:
        r  r;   r  r6   rB   r$   r$   r%   get_bucket_versioning
  s   	zBosClient.get_bucket_versioningc           	      C   sl   ddi}|dur||d< |dur||d< |dur||d< |dur$||d< |dur,||d< | j tj|||d	S )
r   s   versionsr;   Nr   r   r   r   s   versionIdMarkerr<   r6   )	r#   r:   r   r   r   Zversion_markerr   r4   r=   r$   r$   r%   list_objects_versions
  s   zBosClient.list_objects_versionsc                 C   r   )z
        init bucket object lock
        :type bucket: string
        :param bucket: None

        :type retention_days: int
        :param retention_days: None

        :return:**Http Response**
        
   objectlockr;   ZretentionDaysr   rP  )r#   r:   Zretention_daysr4   r$   r$   r%   init_bucket_object_lock8
  r   z!BosClient.init_bucket_object_lockc                 C   rN   )z
        get bucket object lock

        :type bucket: string
        :param bucket: None

        :return:**Http Response**
        r  r;   r<   r6   rB   r$   r$   r%   get_bucket_object_lockM
  r   z BosClient.get_bucket_object_lockc                 C   rN   )z
        Delete Bucket Object Lock

        :type bucket: string
        :param bucket: None

        :return:**Http Response**
        r  r;   r<   rp   rB   r$   r$   r%   delete_bucket_object_lock\
  r   z#BosClient.delete_bucket_object_lockc                 C   rN   )z
        complete bucket object lock
        :type bucket: string
        :param bucket: None

        :type retention_days: int
        :param retention_days: None

        :return:**Http Response**
        s   completeobjectlockr;   r<   )r7   r   r   rB   r$   r$   r%   complete_bucket_object_lockk
  s
   z%BosClient.complete_bucket_object_lockc                 C   r   )z
        extend bucket object lock
        :type bucket: string
        :param bucket: None

        :type extend_retent_days: int
        :param extend_retent_days: None

        :return:**Http Response**
        s   extendobjectlockr;   ZextendRetentionDaysr   rP  )r#   r:   Zextend_retent_daysr4   r$   r$   r%   extend_bucket_object_lock~
  r   z#BosClient.extend_bucket_object_lockc                 C   rN   z~
        get bucket quota
        :type bucket: string
        :param bucket: None

        :return:**Http Response**
           quotar;   r<   r6   rB   r$   r$   r%   get_bucket_quota
  rx   zBosClient.get_bucket_quotac                 C   &   | j tj|tj|tjdddi|dS )z
        put quota conf of bucket
        :type bucket: string
        :param bucket: None

        :type retention_days: int
        :param retention_days: None

        :return:**Http Response**
        rZ   r  r;   rO  r  )r#   r:   Z
quota_confr4   r$   r$   r%   put_bucket_quota
     zBosClient.put_bucket_quotac                 C   rN   r  rp   rB   r$   r$   r%   delete_bucket_quota
  rx   zBosClient.delete_bucket_quotac                 C   rN   )z
        get bucket tagging
        :type bucket: string
        :param bucket: None

        :return:**Http Response**
        r  r;   r<   r6   rB   r$   r$   r%   get_bucket_tagging
  rx   zBosClient.get_bucket_taggingc                 C   r  )z
        put tagging conf of bucket
        :type bucket: string
        :param bucket: None

        :type retention_days: int
        :param retention_days: None

        :return:**Http Response**
        rZ   r  r;   rO  r  )r#   r:   Ztag_confr4   r$   r$   r%   put_bucket_tagging
  r  zBosClient.put_bucket_taggingc                 C   rN   )z
        delete bucket tagging
        :type bucket: string
        :param bucket: None

        :return:**Http Response**
        r  r;   r<   rp   rB   r$   r$   r%   delete_bucket_tagging
  rx   zBosClient.delete_bucket_taggingc              
   C   s  i }| d ur| r| dk rt dt| |tj< |d ur$t||tj< |d ur1t||tj< nt	j
|tj< |d ur@||tj< |d urNdt| |tj< |d urd}t }t|ts`tdt|D ]*\}}||  t|}t|}tj| }|||< |t|7 }|t|7 }qd|tjkrt dtj t|t|krt d|d ur||tj< |d urt||tj< |	d urt|	|tj< |
d urt|
|tj< |d urz	t||d}W n ty } z|d }~ww |d ur||tj< |d urt||tj < |S )Nr   z&content_length should not be negative.s   "%s"z%user_metadata should be of type dict.z,Metadata size should not be greater than %d.z%user_metadata has duplicate elements.F)!r   r   r   r   rd  r   r[  re  rb   r   r\  ZBCE_CONTENT_SHA256ZETAGr   rE   dictr   r   addr   r   r   r   ZMAX_USER_METADATA_SIZErZ  ZBOS_SERVER_SIDE_ENCRYPTIONZ'BOS_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEYZ+BOS_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5r1   rE  rF  r   r  )r   r  r  r  rG  r   r   r  r.  r/  r0  r   r  r^   Z	meta_sizeZmeta_data_setrV   rW   Znormalized_keyrL   r$   r$   r%   r!  
  sn   








z!BosClient._prepare_object_headersFc                 C   s   t |ts	tdtj}|s |ttjtjtj	tj
tjg}n|ttjtjtjg}t|D ]\}}t|}|tjkrDt|}||v rL|| |< q1| S )Nz$user_headers should be of type dict.)rE   r  r   r   ZBOS_BASE_ALLOW_HEADERSunionr   ZCACHE_CONTROLZCONTENT_ENCODINGZCONTENT_DISPOSITIONZEXPIRESZBOS_PROCESSZBCE_COPY_SOURCE_IF_NONE_MATCHZ#BCE_COPY_SOURCE_IF_UNMODIFIED_SINCEZ!BCE_COPY_SOURCE_IF_MODIFIED_SINCEr   r   r[  ZBOS_OBJECT_EXPIRES)r^   r  Zis_copyZbos_headersZuser_headers_setrV   rW   r$   r$   r%   rE  P  s,   






zBosClient._get_user_headerc                 C   s.   d }|d urt ||}|d ur|S t | j|S r2   )getattrr4   )r#   r4   attrr+   r$   r$   r%   r  j  s   
zBosClient._get_config_parameterc                 C   s`   | j }|r| j}t| j | j\}}}| js!t|s!t||r(tt	j
|S tt	j
||S r2   )r   backup_endpointr   r   r   cname_enabledis_cname_like_hostZis_custom_hostZ
append_urir   Z
URL_PREFIX)r4   r:   r   Zuse_backup_endpointhostr   Z	host_namer   r$   r$   r%   r   s  s   zBosClient._get_pathc                 C   s4   t  | j}|d ur|| | ||}||_|S r2   )copyr4   Zmerge_non_none_values_change_user_endpointr   )r#   r4   r:   Z
new_configr   r$   r$   r%   r     s   
zBosClient._merge_configc                 C   s*  t |j|j\}}}t|d}|j}t |o t|dk}t 	|r)|jS |j
r/|jS |js7t |r@|r=td|jS |d ur{|r{|tro|d}	t|	dk rY|jS |	d d t| d |	d  }
t|
S t|d t| S t|dkr|d ur|d t|krtd	|jS )
N   .   z<endpoint is not cname domain, please set cname_enabled=Falses   //r   r   rh     z@your endpoint's bucket_name is not equal your query bucket_name!)r   r   r   r   r   r   splitZis_bos_suffixed_hostr   Z
check_ipv4Zpath_style_enabler  r  r   rT   HTTP_PROTOCOL_HEAD)r#   r4   r:   r   Zuser_host_namer   Zuser_endpoint_splitZuser_endpointZis_bos_path_style_hostZhttp_head_splitZbucket_endpointr$   r$   r%   r    sD   



zBosClient._change_user_endpointc                 C   sP   t | trdS t | tr&| jtjjkrdS | jtjjkrdS | jtj	kr&dS dS )NTF)
rE   IOErrorr   rG   rH   rI   INTERNAL_SERVER_ERRORSERVICE_UNAVAILABLEcodeZREQUEST_EXPIRED)errorr$   r$   r%   _need_retry_backup_endpoint  s   

z%BosClient._need_retry_backup_endpointc	                 C   s   |  ||}t|||}	|d u rtj}|jd ur#|pi }|j|tj< zt	|t
jtj|g||	|||W S  tys }
 z3|jd u rC|
t|
jrmtd t|||d}	t|t
jtj|g||	|||d	W  Y d }
~
S |
d }
~
ww )Ns   Retry for backup endpoint.T)r   r1   r   r   Z
parse_jsonr   r   r   r   send_requestr   r   Zparse_errorr   r  r  rF   rw  rx  Zsend_request_no_underlined)r#   Zhttp_methodr:   r   r]   r^   r=   r4   r   r   rL   r$   r$   r%   r7     s2   



zBosClient._send_requestr2   )NNN)NN)r   NNNN)NNNN)Nr   N)r   N)NNNNN)
NNNNNNNNNN)NNNNNNNNNNNN)NNNNNNNNNNNNN)NNNNNNNNNNNNNN)	r{  NNNNNNNN)NNNNNN)r   NNNNN)F)NNF)NNNNNNN)r-   r.   r/   r0   r&   r9   r   r   r   r?   rC   rM   rP   staticmethodrX   r   r  rd   rh   rn   ro   rr   rv   rw   ry   ENCRYPTION_ALGORITHMr{   r|   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r8   r   r   r   r   r   r   r   r   r   r   r   r   r   r	  r  r  r  r  objectr   integer_typesr#  r,  r1  r4  r<  rH  rI  rQ  rV  rW  rX  r]  r   rg  ri  rj  rm  rn  rp  rq  rs  ru  rz  r  r  r  r  r  r  boolr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r!  rE  r  r   r   r  r  r7   r$   r$   r$   r%   r1   X   sD   
G$


"
0<,?<AD*=4% 'B6&(%

&R	
,r1   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )SelectMessagez1
    returned message from select object api
    c                 C   s   d| _ || _|| _|| _dS )z/
        Initialize for record message
        RecordsN)typer^   payloadcrc)r#   r^   r  r  r$   r$   r%   set_record_message  s   
z SelectMessage.set_record_messagec                 C   s"   d| _ || _|| _|| _|| _dS )z1
        Initialize for continue message
        ContN)r  r^   bytes_scannedbytes_returnedr  )r#   r^   r  r  r  r$   r$   r%   set_cont_message  s
   
zSelectMessage.set_cont_messagec                 C   s   d| _ || _|| _dS )z,
        Initialize for end message
        EndN)r  r^   r  )r#   r^   r  r$   r$   r%   set_end_message  s   
zSelectMessage.set_end_messagec                 C   sD   | j dkrd| j| jS | j dkrd| j| j| jS d| jS )Nr  z{}
{}r  z%{}
bytes_scanned/bytes_returned={}/{}z{})r  r   r^   r  r  r  r"   r$   r$   r%   __str__  s   

zSelectMessage.__str__N)r-   r.   r/   r0   r  r  r  r  r$   r$   r$   r%   r    s    	
r  c                   @   s4   e Zd ZdZdd Zdd Zdd Zedd	 Zd
S )r  z0
    deal with message of select object api
    c                 C   s
   d| _ d S r   )finishr"   r$   r$   r%   r&     s   
zSelectResponse.__init__c                 C   s   || _ || _dS )z2
        get HttpResponse and BceResponse
        Nr  )r#   r   r>   r$   r$   r%   r
    s   
z&SelectResponse.init_from_http_responsec                 c   s   | j }z| js|d}|sttd|dd d }td|dd d }||}| |}t }|d dkr`|| d }||}	td|dd }
|	|t
|	|
 |V  nh|d d	kr|d}|d}td|dd }
td
|d }td
|d }|||||
 |V  n1|d dkrtd|dd }
|d dkrt|d |d | jjjd|||
 d| _|V  | jrt| j   w )z-
        generator for SelectMessage
           Nz>Ir   r  zmessage-typer     r  z>Qr  z
error-codesuccesszerror-message)r  Z
request_idT)r   r  r  StopIterationr  structunpack_parse_select_headersr  r  r   rm   r  r   r>   r   Zbce_request_idr  )r#   r  ZpreludeZ	total_lenZheaders_lenr^   Zheaders_mapmsgZpayload_lenr  r  r  r  r$   r$   r%   r+     sL   





!zSelectResponse.resultc                 C   s   i }d}|t | k rVtd| ||d  d }|d7 }| |||  }||7 }td| ||d  d }|d7 }| |||  }||7 }t||t|< |t | k s
|S )z\
        parse SELECT headers
        :param headers: <str>
        :return: <dict>
        r   Brh  z>Hr   )r   r  r  r   rm   )r^   Zhmrs   Zkey_lenr   Z	value_lenvaluer$   r$   r%   r  H  s   z$SelectResponse._parse_select_headersN)	r-   r.   r/   r0   r&   r
  r+   r  r  r$   r$   r$   r%   r    s    +r  )>r0   r(  r  http.clientrH   r8  r`   loggingr   r  builtinsr   r   Zfuture.utilsr   r   r   concurrent.futuresr   r   r	   r
   r   	functoolsr  r&  r   r   Zbaidubce.authr   Zbaidubce.bce_base_clientr   Zbaidubce.exceptionr   r   r   Zbaidubce.httpr   r   r   r   r   Zbaidubce.servicesr   Zbaidubce.services.bosr   r   Zbaidubce.utilsr   r   	getLoggerr-   rw  ZFETCH_MODE_SYNCZFETCH_MODE_ASYNCr  r  r   r1   r  r  r  r$   r$   r$   r%   <module>   s   
                       &(