o
    ýeVw ã                   @   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Z8dZ9G dd„ dƒZ:G 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
   bcebos.coms   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$   úPD:\Projects\ConvertPro\env\Lib\site-packages\baidubce/services/bos/bos_client.pyÚ__init__D   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%   ÚcancelH   s   
zUploadTaskHandle.cancelc                 C   s   | j  ¡  | j}| j  ¡  |S )z!
        get cancel flag
        r'   )r#   Úresultr$   r$   r%   Ú	is_cancelP   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÷d‚dƒ„ƒZFeeefeefeefd„													død…d†„ƒZGeeefeefeefeefd‡								dùdˆd‰„ƒZHeeefeefdLdídŠd‹„ƒ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íd’d“„ƒZLeeefddíd”d•„ƒZMeeefeefdL				dód–d—„ƒZNeeefeefeefeOeAjBe@d˜		dód™dš„ƒZPeeefeefeefeefeefeOeAjBeAjBd›					dúdœd„ƒZQeeefeefeefeOeAjBeefeAjBdž		dódŸd „ƒZReeefeefeefed¡		dïd¢d£„ƒZSeeefeefeefd¤díd¥d¦„ƒZTeeefeefeefd¤		dîd§d¨„ƒZUeeefeefeefd¤díd©dª„ƒZVeeefd			dúd«d¬„ƒZWeeefddîd­d®„ƒZX	dïd¯d°„ZYeeefeefeefdt	±								dûd²d³„ƒZZeeefeefeefd´dídµd¶„ƒZ[eeefeefdL				dód·d¸„ƒZ\eeefeefdLdíd¹dº„ƒZ]eeefeefdLdíd»d¼„ƒZ^eeefeefeefd½			dîd¾d¿„ƒZ_eeefeefeefe`dÀ		dúdÁdÂ„ƒZaeeefeefdÃdídÄdÅ„ƒZbeeefeefefdÆdïdÇdÈ„ƒZcdídÉdÊ„ZdeeOeOdËdídÌdÍ„ƒZedídÎdÏ„ZfeeefddídÐdÑ„ƒZgeeefefdÒdídÓdÔ„ƒZheeefddídÕdÖ„ƒZieeefefd×dídØdÙ„ƒZjeeefddídÚdÛ„ƒZkeeefddídÜdÝ„ƒZle												d÷dÞdß„ƒZmedüdádâ„ƒZndãdä„ Zoedýdådæ„ƒZpdçdè„ Zqedédê„ ƒZr				dþdëdì„Zsd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_bucketsa   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_locationl   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_website7  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_encryptionH  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_encryptionq  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_inventoryP  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_trashp  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  |¡}|  |	|¡}	|pi }|pi }|dur||	_t |	j|	j¡\}}}|}||jkr5|dt  |¡ 7 }||tj	< |  
|	||¡}|
tjkrP|
tjkrPtdgƒ}|	jdur]|	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**
        Nó   :s   hosts   %s://%s%s?%sF)r   rˆ   Ú_merge_configÚprotocolr   Z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
httpmethodZendpoint_protocolZendpoint_hostZendpoint_portZ	full_hostÚpathr$   r$   r%   Úgenerate_pre_signed_url©  s@   
ÿ



ø

ý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"  s   ü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_cors7  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_corsF  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 )a¡  
        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**
        Ns   maxKeysó   prefixs   markeró	   delimiterr<   r6   )r#   r:   Zmax_keysÚprefixÚmarkerÚ	delimiterr4   r=   r$   r$   r%   Úlist_objectsU  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_objectsz  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Ú	TypeErrorÚlenÚ
ValueErrorr   Ú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   sj   t  |¡}t|ƒdks| d¡rtdƒ‚t |¡}|dur(|du r#i }||tj< | j	t
j||||tjdS )úx

        :param bucket_name:
        :param key:
        :param range:
        :param config:
        :return:
        r   ó   /ú(Key can not be empty or start with '/' .N©r^   r4   Úbody_parser)r   rˆ   rÓ   rT   r   r1   r×   r   ÚBOS_TRAFFIC_LIMITr7   r   r8   rà   )r#   r:   r¢   rÖ   Útraffic_limitr4   Ú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=   r4   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   s4   t  |¡}| j||||d}|j ¡ }|j ¡  |S )rá   )rÖ   r4   )r   rˆ   ré   rÞ   Úreadr÷   )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€   t  |¡}t|ƒdks| d¡rtdƒ‚t  ˆ¡‰t |¡}|dur-|du r(i }||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**
        r   râ   rã   Nc                    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>;  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%   Úget_object_to_file  s"   



úzBosClient.get_object_to_filec                 C   ó   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**
        r5   )r   rˆ   r7   r   rD   ©r#   r:   r¢   r4   r$   r$   r%   Úget_object_meta_dataC  ó   
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r/||d< |r7t ||¡}| 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ç   úDObject 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ç   r4   r^   r=   r$   r$   r%   Úappend_objectS  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   sz   t  |¡}t  |¡}| j||||||	|
|d}|  |d¡}|tjkr)tdtj ƒ‚|r1t ||¡}| 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ç   r4   r^   rù   r$   r$   r%   Ú
put_objectÁ  s6   
%
ø

ÿúzBosClient.put_object)Úbucketr¢   rÞ   c                 C   s¤   t  |¡}t|tƒr| tj¡}d}z5t |¡}|du r't	j
||  |d¡d}| j|||t|ƒ||||||	|
|||||dW |durG| ¡  S S |durQ| ¡  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#  r$  rú   rç   r4   )r   rˆ   rE   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ç   r4   r  r$   r$   r%   Úput_object_from_string  s<   


ÿ
ó

ÿ
ÿz BosClient.put_object_from_string)r&  r¢   rø   c                 C   s²   t  |¡}t|dƒ}zI|du r| dtj¡ | ¡ }| d¡ |du r1|  |d¡}tj	|||d}|du r:t 
|¡}| j|||||||||	|
||||||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ù   r'  )r   rˆ   rñ   ÚseekÚosÚSEEK_ENDÚtellr  r   r  Zguess_content_type_by_file_namer%  r÷   )r#   r&  r¢   rø   ró   r  r  r  rÝ   r   r  r"  r#  r$  rú   rç   r4   r  r  r$   r$   r%   Úput_object_from_file>  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¡|tj< |dur-||tj< |dus5|dur;d|tj< nd|tj< |
dur\z	t	 
||
d¡}W n ty[ } 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ç   ó   /%s/%sFNs   replaces   copyTrä   )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#   r0  r1  r2  r3  Úetagr  rÝ   r   r  Zcopy_object_user_headersrç   r4   r^   rL   r$   r$   r%   Úcopy_object~  sH   
 
ûþ
ý

€ÿúzBosClient.copy_objectc                 C   r  )zÁ
        Delete Object

        :type bucket: string
        :param bucket: None

        :type key: string
        :param key: None
        :return:
            **HttpResponse Class**
        r5   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:   r=  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#   rD  rE  rF  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**
        rG  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**
        rG  r;   r<   rp   rB   r$   r$   r%   Údelete_bucket_logging  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   r8  r9  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   rN  ÚCONTENT_MD5r   rô   ræ   r7   r   rA   )r#   r:   r¢   rP  rQ  rR  rS  Úpart_md5rú   rç   r4   r^   r$   r$   r%   Úupload_partF  s6   
'
ÿ
ÿÿ

ùzBosClient.upload_part)r0  r1  r2  r3  rP  rQ  rR  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ç   r4  FrÏ   é   NrT  rf   )r   rˆ   r  r   r5  r   r6  ZBCE_COPY_SOURCE_RANGEr7  r7   r   rA   )r#   r0  r1  r2  r3  rP  rQ  rR  r  r:  r  rÝ   rç   r4   r^   rÖ   r$   r$   r%   Úupload_part_copy‰  s2   
&
ý
ÿ
þ

úzBosClient.upload_part_copy)r:   r¢   rP  rQ  rR  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:
        r)  )rX  rú   rç   r4   )r   rˆ   rñ   r+  rY  r÷   )r#   r:   r¢   rP  rQ  rR  rø   r  rX  rú   rç   r4   rû   r$   r$   r%   Úupload_part_from_fileÅ  s   


þzBosClient.upload_part_from_file)r:   r¢   rP  Ú	part_listc                 C   s@   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Ý   ÚpartsrU  )r]   r^   r=   )
r   rˆ   r  r   rc   r7   r   rì   r`   ra   )r#   r:   r¢   rP  r]  rÝ   r4   r^   r$   r$   r%   Úcomplete_multipart_uploadæ  s   
þúz#BosClient.complete_multipart_upload)r:   r¢   rP  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**
        rU  )r=   r   )r#   r:   r¢   rP  r4   r$   r$   r%   Úabort_multipart_upload  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**
        rU  Ns   maxPartss   partNumberMarkerr<   r‹   )r#   r:   r¢   rP  Z	max_partsÚpart_number_markerr4   r=   r$   r$   r%   Ú
list_parts!  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)ra  r4   )r   rˆ   rb  r^  rË   Znext_part_number_marker)r#   r:   r¢   rP  r4   ra  r>   rÌ   r$   r$   r%   Úlist_all_partsB  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**
        rK  r;   NrÄ   s
   maxUploadss	   keyMarkerrÃ   r<   r6   )r#   r:   Zmax_uploadsÚ
key_markerrÅ   rÇ   r4   r=   r$   r$   r%   Úlist_multipart_uploadsW  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)rd  rÅ   rÇ   r4   r   éÿÿÿÿ)re  ZuploadsrË   Znext_key_markerrÓ   r¢   )r#   r:   rÅ   rÇ   r4   rd  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ë   r\  rÚ   rÜ   r:  r9  )r#   r:   Z
object_keyrP  rQ  rR  rø   r  r]  Ú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
 | ¡ r¡t d¡ | j|||d dS t|ƒ|kr¶t 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!NrZ  i   )r  r   r  )Úreturn_whenz!putting super object is canceled!)rP  Fzputting super object failed!c                 S   s   | d S )Nrh  r$   )Úxr$   r$   r%   r	  í  s    z6BosClient.put_super_obejct_from_file.<locals>.<lambda>r>  T)r   r,  r±   ÚgetsizeÚmultiprocessingÚ	cpu_countr   rO  rP  r   Úsubmitrl  rÚ   r   r	   r,   ri  rj  r`  rÓ   Úsortr_  )r#   r:   r¢   rø   Ú
chunk_sizeZ
thread_numrk  r  r   r  rú   rç   r4   Z	left_sizerR  Z
total_partrP  ÚexecutorÚ	all_tasksr  rQ  r]  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   rZ  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$   r%   Úget_object_acl@  ó   
û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_aclV  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_MODErL  r7   r   rì   )r#   r:   r¢   r€  Z
fetch_moder   r4   r^   r$   r$   r%   Úfetch_objectl  s   


úzBosClient.fetch_object)r:   r3  ÚsymlinkÚforbid_overwritec	           
      C   s‚   t  |¡}t  |¡}| j||d}	||	tj< |dur'|r"d|	tj< nd|	tj< |dur3t  |¡|	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**
        )rÝ   r   Ns   trues   falseó   symlinkr;   rf   )
r   rˆ   r  r   ZBOS_SYMLINK_TARGETZBOS_FORBID_OVERWRITEZBOS_SYMLINK_BUCKETr7   r   rA   )
r#   r:   r3  r‚  rƒ  rÝ   r   rE  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;   rB  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_notification	  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_notification.	  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_notification@	  s   þzBosClient.delete_notification)r:   Úmirror_argsc                 C   s*   | 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_mirroringO	  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_mirroringb	  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_mirroringr	  rQ   z!BosClient.delete_bucket_mirroringc              
   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r‘d}t|tƒs]tdƒ‚t|ƒD ]#\}}t |¡}t |¡}tj| }|||< |t|ƒ7 }|t|ƒ7 }qa|tjkr‘t dtj ƒ‚|d urš||tj< |d ur¦t |¡|tj< |	d ur²t |	¡|tj< |
d ur¾t |
¡|tj< |d urÚz	t ||d¡}W n tyÙ } z|‚d }~ww |d urã||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.F)rÔ   r   rˆ   r   rV  r   rM  rW  rb   r   rN  ZBCE_CONTENT_SHA256ZETAGrE   ÚdictrÒ   r   rÛ   rÓ   r   ZMAX_USER_METADATA_SIZErL  ZBOS_SERVER_SIDE_ENCRYPTIONZ'BOS_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEYZ+BOS_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5r1   r8  r9  ræ   )ró   r  r  r  r:  rÝ   r   r  r"  r#  r$  rç   r^   Z	meta_sizerV   rW   Znormalized_keyrL   r$   r$   r%   r  „	  sb   





ÿ
ÿÿ€ÿ
z!BosClient._prepare_object_headersFc                 C   s~   t |tƒs	tdƒ‚|sttjtjtjtjgƒ}n
ttj	tj
tjgƒ}t|ƒD ]\}}t |¡}t |¡}||v r<|| |< q&| S )Nz$user_headers should be of type dict.)rE   r   rÒ   r©   r   ZCACHE_CONTROLZCONTENT_ENCODINGZCONTENT_DISPOSITIONZEXPIRESZBCE_COPY_SOURCE_IF_NONE_MATCHZ#BCE_COPY_SOURCE_IF_UNMODIFIED_SINCEZ!BCE_COPY_SOURCE_IF_MODIFIED_SINCEr   r   rM  )r^   r  Zis_copyZuser_headers_setrV   rW   r$   r$   r%   r8  Ð	  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  æ	  s   
zBosClient._get_config_parameterc                 C   sJ   | j }|r| j}| jst |¡st ||¡rt tj|¡S t tj||¡S r2   )	r¦   Úbackup_endpointZcname_enabledr   Úis_cname_like_hostZis_custom_hostZ
append_urir   Z
URL_PREFIX)r4   r:   r¢   Zuse_backup_endpointÚhostr$   r$   r%   r¨   ï	  s   zBosClient._get_pathc                 C   sÖ   t   | j¡}|d ur| |¡ |d urit | jj¡si| jj}t |¡ d¡}| 	t
¡rit|ƒdkri| t¡r\| d¡}t|ƒdk rC|S |d d t |¡ d |d  }t |¡|_|S t |¡d t |¡ |_|S )Nó   .é   s   //rÎ   r   rZ  )Úcopyr4   Zmerge_non_none_valuesr   r¤  r¦   r   rˆ   ÚsplitÚendswithÚDEFAULT_BOS_DOMAIN_SUFFIXrÓ   rT   ÚHTTP_PROTOCOL_HEAD)r#   r4   r:   Z
new_configZuser_endpointZuser_endpoint_splitZhttp_head_splitZbucket_endpointr$   r$   r%   r¤   ø	  s,   


ÿÿÿzBosClient._merge_configc                 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   ri  rj  )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)Nrê   N)rï   N)NNNN)	NNNNNNNNN)
NNNNNNNNNN)NNNNNNNNNNN)NNNNNNNNNNNN)NNNNNNNNNNNNN)NNNNNNNN)NNNNN)	rm  NNNNNNNN)F)NNF)NNNNNNN)tr-   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   r%  r(  r/  r;  r<  rC  rH  rI  rJ  rO  rò   rY  r[  r\  r_  r`  rb  rc  re  rg  rl  rx  ry  r{  r}  r  r  Úboolr…  r†  r‹  rŽ  r’  r“  r–  r™  rš  r  rž  rŸ  r  r8  r  r¨   r¤   r²  r7   r$   r$   r$   r%   r1   Z   sâ   


öDýýþ$


"
ÿ+üõ:þö,üó=ô:ó?ýö=þüú*ûþ=ùø4úþýý#þ þ'
þøBü6ý&ÿ%
%


ôK	
ü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_messageJ
  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_messageS
  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__e
  s   

ÿzSelectMessage.__str__N)r-   r.   r/   r0   r¾  rÂ  rÄ  rÅ  r$   r$   r$   r%   r¹  F
  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&   r
  s   
zSelectResponse.__init__c                 C   s   || _ || _dS )z2
        get HttpResponse and BceResponse
        Nr  )r#   rß   r>   r$   r$   r%   rÿ   u
  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rÈt d| d¡¡d }
|d dkr¼t|d |d | jjjd‚| ||
¡ d| _|V  | jrt‚| j  ¡  w )z-
        generator for SelectMessage
        é   Nz>Ir   é   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   ÚBrZ  z>HrÎ   )rÓ   rÌ  rÍ  r   rm   )r^   Zhmrs   Zkey_lenr¢   Z	value_lenÚvaluer$   r$   r%   rÎ  §
  s   óz$SelectResponse._parse_select_headersN)	r-   r.   r/   r0   r&   rÿ   r+   r´  rÎ  r$   r$   r$   r%   rŠ  n
  s    +rŠ  )?r0   r  r¨  Úhttp.clientrH   r,  r`   Úloggingrõ   rÌ  Úbuiltinsr   r   Zfuture.utilsr   r   r   Úconcurrent.futuresr   r   r	   r
   r   Ú	functoolsrq  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-   ri  ZFETCH_MODE_SYNCZFETCH_MODE_ASYNCrµ  r«  r¬  r   r1   r¶  r¹  rŠ  r$   r$   r$   r%   Ú<module>   s~   
                   (