o ckF[zE@sddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddlm Z ddl Z ddl mZmZmZmZddlmZddlmZddlmZddlmZddlmZdd lmZdd l mZdd l mZmZdd l mZdd l m Z Gddde!Z"dS)N)md5)BytesIOsixurllib encodebytesBotoClientError)StorageDataError)PleaseRetryException)Provider)KeyFile)User) UserAgent) compute_md5 compute_hash)find_matching_headers)merge_headers_by_namec@sNeZdZdZdZdZejdddZ e gdZ e Z e D] Z e e dd qe e gd BZdd d ZddZddZeddZddZddZeeeZddZddZeeeZddZddZeeeZd d!Zd"d#Z eee Z!d$d%Z"d&d'Z#d(d)Z$dd+d,Z%d-d.Z&d/d0Z' 1 dd2d3Z(dd4d5Z) dd7d8Z*d*Z+dd9d:Z,d;d<Z-e-Z.dd>d?Z/ @ddAdBZ0 * @ddCdDZ1dEdFZ2dGdHZ3ddIdJZ4ddKdLZ5dMdNZ6dOdPZ7dQdRZ8ddSdTZ9ddUdVZ:ddWdXZ;ddYdZZdd_d`Z?ddadbZ@ *ddddeZA f ddgdhZB f ddidjZCddkdlZDddmdnZE @ ddodpZF @ *ddqdrZG @ * *ddsdtZH @ * *ddudvZI f ddwdxZJ f ddydzZKdd{d|ZL f * dd}d~ZM f * dddZN f * dddZOdddZP dddZQddZRdddZS dddZTdddZUd S)KeyaC Represents a key (object) in an S3 bucket. :ivar bucket: The parent :class:`boto.s3.bucket.Bucket`. :ivar name: The name of this Key object. :ivar metadata: A dictionary containing user metadata that you wish to store with the object or that has been retrieved from an existing object. :ivar cache_control: The value of the `Cache-Control` HTTP header. :ivar content_type: The value of the `Content-Type` HTTP header. :ivar content_encoding: The value of the `Content-Encoding` HTTP header. :ivar content_disposition: The value of the `Content-Disposition` HTTP header. :ivar content_language: The value of the `Content-Language` HTTP header. :ivar etag: The `etag` associated with this object. :ivar last_modified: The string timestamp representing the last time this object was modified in S3. :ivar owner: The ID of the owner of this object. :ivar storage_class: The storage class of the object. Currently, one of: STANDARD | REDUCED_REDUNDANCY | GLACIER :ivar md5: The MD5 hash of the contents of the object. :ivar size: The size, in bytes, of the object. :ivar version_id: The version ID of this object, if it is a versioned object. :ivar encrypted: Whether the object is encrypted while at rest on the server. zapplication/octet-streamz %s Botokey_buffer_sizei )z cache-controlzcontent-dispositionzcontent-encodingzcontent-language content-md5 content-typez x-robots-tagexpires-_)z last-modifiedcontent-lengthdateetagNcCs||_||_i|_d|_|j|_d|_d|_d|_d|_ d|_ d|_ d|_ d|_ d|_d|_d|_d|_d|_d|_d|_d|_d|_d|_d|_i|_dS)NF)bucketnamemetadata cache_controlDefaultContentType content_typecontent_encodingcontent_dispositioncontent_languagefilenamer is_latest last_modifiedowner_storage_classpathrespmodesize version_idsource_version_id delete_marker encryptedongoing_restore expiry_date local_hashes)selfrrr8-/usr/lib/python3/dist-packages/boto/s3/key.py__init__is2 z Key.__init__cCs<|jr d|jj|jf}nd|j}t|ts|d}|S)Nz zutf-8)rr isinstancestrencoder7rr8r8r9__repr__s    z Key.__repr__cCs|SNr8r7r8r8r9__iter__sz Key.__iter__cCs d}|jr|jjr|jjj}|SrA)r connectionprovider)r7rEr8r8r9rEs z Key.providercCs|jSrArrBr8r8r9_get_keysz Key._get_keycC ||_dSrArFr7valuer8r8r9_set_key z Key._set_keycCs,d|jvr|jdrt|jdSdSdSNr)r6binasciib2a_hexrBr8r8r9_get_md5sz Key._get_md5cCs8|r t||jd<dSd|jvr|jdddSdSrM)rNa2b_hexr6poprIr8r8r9_set_md5s  z Key._set_md5cCsPd|jvr$|jdr&|jd}t|ts|d}t|ddSdSdS)Nrr; )r6r<bytesr>rN b2a_base64decoderstrip)r7rr8r8r9_get_base64md5s    zKey._get_base64md5cCsH|rt|tjs |d}t||jd<dSd|jvr"|jd=dSdS)Nr;r)r<r string_typesrWrN a2b_base64r6rIr8r8r9_set_base64md5s    zKey._set_base64md5cCs^|jdur,|jr,t|j|jd}t|r)t|dddr)|dj|_|jSd|_|jS)Nr;rr+STANDARD)r+rlistrr>lengetattr)r7 list_itemsr8r8r9_get_storage_classs zKey._get_storage_classcCrHrA)r+rIr8r8r9_set_storage_classrLzKey._set_storage_classcCs2t|}t|}|ddkr|dd}||fS)z A utility function to create the 2-tuple (md5hexdigest, base64md5) from just having a precalculated md5_hexdigest. rTr)rN unhexlifyr)r7 md5_hexdigestdigest base64md5r8r8r9get_md5_from_hexdigests   zKey.get_md5_from_hexdigestcCs.|jjj}|jr||jd|_dSd|_dSrA)rrDrEserver_side_encryption_header getheaderr3r7r-rEr8r8r9handle_encryption_headerss  zKey.handle_encryption_headerscCsL|jjj}|jr ||jd|_|jdur"|dkr$d|_dSdSdSdS)Nawsr])rrDrEstorage_class_headerrkr+get_provider_namerlr8r8r9handle_storage_class_headers    zKey.handle_storage_class_headerFcCs^|jjj}|jdus |r||jd|_||jd|_||jddkr*d|_dSd|_dS)NfalsetrueTF)rrDrEr0rkcopy_source_version_idr1r2)r7r-forcerEr8r8r9handle_version_headerss   zKey.handle_version_headerscCs|jjj}||j}|durdS|dd}|D]+}dd|dD\}}|dd}|dkr=|d kr9d nd |_q|d krD||_ qdS) N,cSsg|]}|qSr8)strip).0ir8r8r9 sz.Key.handle_restore_headers..="zongoing-requestrsTFz expiry-date) rrDrErkrestore_headersplitreplacelowerr4r5)r7responserEheaderpartspartkeyvalr8r8r9handle_restore_headerss    zKey.handle_restore_headerscCsdS)z Used by Key subclasses to do additional, provider-specific processing of response headers. No-op for this base class. Nr8r7headersr8r8r9handle_addl_headersszKey.handle_addl_headersrc CsD|jdurd|_|jjj}|jjjd|jj|j|||d|_|jjdks*|jjdkr:|j}| |jj|jj ||jj }t j |||_|D]9\}}|dkr^d|vr^t||_qJ|d krqtd d |} t| |_qJ|tjvr||j|d d <qJ||j||j||j||jdSdS)a Open this key for reading :type headers: dict :param headers: Headers to pass in the web request :type query_args: string :param query_args: Arguments to pass in the query string (ie, 'torrent') :type override_num_retries: int :param override_num_retries: If not None will override configured num_retries parameter for underlying GET. :type response_headers: dict :param response_headers: A dictionary containing HTTP headers/values that will override any headers associated with the stored object in the response. See http://goo.gl/EWOPb for details. NrGET query_argsoverride_num_retries+rz Content-Rangez content-rangez.*/(.*)z\1rr)r-r.rrDrE make_requestrstatusreadstorage_response_errorreasonmsgbotoutilsget_aws_metadatar itemsrintr/resubr base_fields__dict__rrvrmrr getheaders) r7rrrresponse_headersrEbodyrrJ end_ranger8r8r9 open_reads@           z Key.open_readcCstd)aK Open this key for writing. Not yet implemented :type headers: dict :param headers: Headers to pass in the write request :type override_num_retries: int :param override_num_retries: If not None will override configured num_retries parameter for underlying PUT. zNot Implementedr)r7rrr8r8r9 open_writePs zKey.open_writercCsN|dkrd|_|j|||ddS|dkr!d|_|j||ddStd|)Nr)rrrw)rrzInvalid mode: %s)r.rrr)r7r.rrrr8r8r9open^s  zKey.opencCs*|jr |s |jd|_d|_d|_dS)a Close this key. :type fast: bool :param fast: True if you want the connection to be closed without first reading the content. This should only be used in cases where subsequent calls don't need to return the content from the open HTTP connection. Note: As explained at http://docs.python.org/2/library/httplib.html#httplib.HTTPConnection.getresponse, callers must read the whole response before sending a new request to the server. Calling Key.close(fast=True) and making a subsequent request to the server will work because boto will get an httplib exception and close/reopen the connection. NT)r-rr.closed)r7fastr8r8r9closems   z Key.closecCs*||j|j}|s|t|S)a By providing a next method, the key object supports use as an iterator. For example, you can now say: for bytes in key: write bytes to a file or whatever All of the HTTP connection stuff is handled for you. )rr-r BufferSizer StopIteration)r7datar8r8r9nexts  zKey.nextrcCs8||dkr|j}n|j|}|s||S)Nr)rr-rr)r7r/rr8r8r9rs  zKey.readTcCsT|p|jj}|dkr|j||jdd|dS|dkr$|j||jdd|dStd|)a Change the storage class of an existing key. Depending on whether a different destination bucket is supplied or not, this will either move the item within the bucket, preserving all metadata and ACL info bucket changing the storage class or it will copy the item to the provided destination bucket, also preserving metadata and ACL info. :type new_storage_class: string :param new_storage_class: The new storage class for the Key. Possible values are: * STANDARD * REDUCED_REDUNDANCY :type dst_bucket: string :param dst_bucket: The name of a destination bucket. If not provided the current bucket of the key will be used. :type validate_dst_bucket: bool :param validate_dst_bucket: If True, will validate the dst_bucket by using an extra list request. r]FT)reduced_redundancy preserve_aclvalidate_dst_bucketREDUCED_REDUNDANCYzInvalid storage class: %s)rrcopyr)r7new_storage_class dst_bucketr bucket_namer8r8r9change_storage_classs   zKey.change_storage_classc CsB|jj||}|r d}n|j}|j||jj|j|||||jdS)a Copy this Key to another bucket. :type dst_bucket: string :param dst_bucket: The name of the destination bucket :type dst_key: string :param dst_key: The name of the destination key :type metadata: dict :param metadata: Metadata to be associated with new key. If metadata is supplied, it will replace the metadata of the source key being copied. If no metadata is supplied, the source key's metadata will be copied to the new key. :type reduced_redundancy: bool :param reduced_redundancy: If True, this will force the storage class of the new Key to be REDUCED_REDUNDANCY regardless of the storage class of the key being copied. The Reduced Redundancy Storage (RRS) feature of S3, provides lower redundancy at lower storage cost. :type preserve_acl: bool :param preserve_acl: If True, the ACL from the source key will be copied to the destination key. If False, the destination key will have the default ACL. Note that preserving the ACL in the new key object will require two additional API calls to S3, one to retrieve the current ACL and one to set that ACL on the new object. If you don't care about the ACL, a value of False will be significantly more efficient. :type encrypt_key: bool :param encrypt_key: If True, the new copy of the object will be encrypted on the server-side by S3 and will be stored in an encrypted form while at rest in S3. :type validate_dst_bucket: bool :param validate_dst_bucket: If True, will validate the dst_bucket by using an extra list request. :rtype: :class:`boto.s3.key.Key` or subclass :returns: An instance of the newly created key object r) storage_classr encrypt_keysrc_version_id)rrDlookuprcopy_keyrr0) r7rdst_keyr rrrrrr8r8r9rs / zKey.copycCs|dkr t||_|jSdS)NOwner)r r*)r7rattrsrDr8r8r9 startElements zKey.startElementcCs|dkr ||_dS|dkr||_dS|dkr$|dkrd|_dSd|_dS|dkr-||_dS|dkr8t||_dS|d krA||_dS|d krGdS|d krP||_dSt|||dS) NrETagIsLatestrsTF LastModifiedSize StorageClassr VersionId) rrr(r)rr/rr0setattr)r7rrJrDr8r8r9 endElement s&       zKey.endElementcCst|jj|j|dS)zt Returns True if the key exists :rtype: bool :return: Whether the key exists on S3 r)boolrrrrr8r8r9exists sz Key.existscCs|jj|j|j|dS)z) Delete this key from S3 )r0r)r delete_keyrr0rr8r8r9delete)sz Key.deletecCs |j|SrA)r getr?r8r8r9 get_metadata0s zKey.get_metadatacCsf|dkr ||jd<n|dkr||jd<n||j|<|tjvr1||j|dd<dSdS)Nr Content-Typer Content-MD5rr)rr rbase_user_settable_fieldsrr)r7rrJr8r8r9 set_metadata3s     zKey.set_metadatacCs|j|dSrA)r update)r7dr8r8r9update_metadata?szKey.update_metadatacCs&|jdur|jj||j|ddSdSNr)rset_aclrr7acl_strrr8r8r9rCs z Key.set_aclcC |jdur|jj|j|dSdSr)rget_aclrrr8r8r9rG z Key.get_aclcCrr)r get_xml_aclrrr8r8r9rKrzKey.get_xml_aclcCs"|jdur|jj||j|dSdSr)r set_xml_aclrrr8r8r9rOs zKey.set_xml_aclcCs|j||j|SrArset_canned_aclrrr8r8r9rSzKey.set_canned_aclcCsD|jjd|jj|j}|jdkr|dS|j|j|j| )zReturn the redirect location configured for this key. If no redirect is configured (via set_redirect), then None will be returned. HEADx-amz-website-redirect-location) rrDrrrrkrErrr)r7rr8r8r9 get_redirectVs   zKey.get_redirectcCs^|duri}n|}||d<|jjd|jj|j|}|jdkr#dS|j|j|j| )aOConfigure this key to redirect to another location. When the bucket associated with this key is accessed from the website endpoint, a 301 redirect will be issued to the specified `redirect_location`. :type redirect_location: string :param redirect_location: The location to redirect. NrPUTrT) rrrDrrrrErrr)r7redirect_locationrrr8r8r9 set_redirectes  zKey.set_redirectcCs|jd|j|S)Nz public-readrrr8r8r9 make_public~rzKey.make_publicrc Cs|jjj} |p |j}|duri}n|}| r| || j<| r*d|_| jr*|j|| j<| r1d|| j<t j ||j | }|jj |||jj|j|||||| S)a$ Generate a URL to access this key. :type expires_in: int :param expires_in: How long the url is valid for, in seconds. :type method: string :param method: The method to use for retrieving the file (default is GET). :type headers: dict :param headers: Any headers to pass along in the request. :type query_auth: bool :param query_auth: If True, signs the request in the URL. :type force_http: bool :param force_http: If True, http will be used instead of https. :type response_headers: dict :param response_headers: A dictionary containing HTTP headers/values that will override any headers associated with the stored object in the response. See http://goo.gl/EWOPb for details. :type expires_in_absolute: bool :param expires_in_absolute: :type version_id: string :param version_id: The version_id of the object to GET. If specified this overrides any value in the key. :type policy: :class:`boto.s3.acl.CannedACLStrings` :param policy: A canned ACL policy that will be applied to the new key in S3. :type reduced_redundancy: bool :param reduced_redundancy: If True, this will set the storage class of the new Key to be REDUCED_REDUNDANCY. The Reduced Redundancy Storage (RRS) feature of S3, provides lower redundancy at lower storage cost. :type encrypt_key: bool :param encrypt_key: If True, the new copy of the object will be encrypted on the server-side by S3 and will be stored in an encrypted form while at rest in S3. :rtype: string :return: The URL to access the key NrAES256)rrDrEr0r acl_headerrrorjrr merge_metar generate_urlr) r7 expires_inmethodr query_auth force_httprexpires_in_absoluter0policyrrrEr8r8r9rs, 6      zKey.generate_url c Cs|j|||||||ddS)at Upload a file to a key into a bucket on S3. :type fp: file :param fp: The file pointer to upload. The file pointer must point at the offset from which you wish to upload. ie. if uploading the full file, it should point at the start of the file. Normally when a file is opened for reading, the fp will point at the first byte. See the bytes parameter below for more info. :type headers: dict :param headers: The headers to pass along with the PUT request :type num_cb: int :param num_cb: (optional) If a callback is specified with the cb parameter this parameter determines the granularity of the callback by defining the maximum number of times the callback will be called during the file transfer. Providing a negative integer will cause your callback to be called with each buffer read. :type query_args: string :param query_args: (optional) Arguments to pass in the query string. :type chunked_transfer: boolean :param chunked_transfer: (optional) If true, we use chunked Transfer-Encoding. :type size: int :param size: (optional) The Maximum number of bytes to read from the file pointer (fp). This is useful when uploading a file in multiple parts where you are splitting the file up into different ranges to be uploaded. If not specified, the default behaviour is to read all bytes from the file pointer. Less bytes may be available. rcbnum_cbrchunked_transferr/N)_send_file_internal)r7fprrrrrr/r8r8r9 send_files ' z Key.send_filec  s>jjjz Wntyd d_Ynwdur%js%dtitfddp.iD f dd} |sEi}n|}t d|D]} || =qNt |d<j dvrcj |j <t d |rntd |_t d |rytd |_t d |} | rt| d kr|| d dur|| d =n'td |_n jrtjd _jdurj_j|d <nj|d <jrÈj|d<rd|d<ntj|d<djjvrtjd} dur| d<tdi| d |d<d|d<tj |j!}jjj"djj#j#|| |d} j$| dd%| &dS)NFrc3|] }||fVqdSrAr8rzalg hash_algsr8r9 z*Key._send_file_internal..cs<durkrn durjrdi}tjd|r)d|d<tjd|r4d|d<|j||fi||D] }||||q@| j j j }dj j _ t |ddd krf|dd}rro} n jrvj} nd} r| dkrd j} ndkrtt| jd } n dkrd } nd} d} || } | r| jkr| } nj} t| ts| d } durd_| rDt| }||7}r|d||| |dn|| D] }|| q| r | |8} | dkr n:r"| d7} | | ks| d kr"|| d} | r1| jkr1| } nj} t| tsB| d } | s|_D] }|j|<qIrc|d|drz| dksp| dkrz|dkrz|| |||j j _ |}|}|s|j |j!||S)Nz9Cannot retry failed request. fp does not support seeking.hostrx skip_hostzaccept-encodingskip_accept_encodingr debuglevel?rdr;Tz%x; z z0 )"tellseekread_from_streamstorage_data_errorrrr putrequest putheader endheadersrrDdebugr`set_debuglevelr/rrmathceilrr<rUr>r_sendrrgr6 getresponse should_retryrrr) http_connrr,rrskipsr save_debugdata_lencb_sizecb_countr{ bytes_togochunk chunk_lenrrr) rr digestersrrrEr7r/sposr8r9sender s                      $    z'Key._send_file_internal..senderz User-Agent)Nr]zContent-EncodingzContent-LanguagerrxrrchunkedzTransfer-EncodingzContent-Lengthz hmac-v4-s3)rhash_algorithmr/_sha256z 100-ContinueExpectr)r(rT)rur8)'rrDrErIOErrorrrdictrrrr+rrorr$r&r_r#r, mimetypes guess_typer"rhr=r/_required_auth_capabilityhashlibsha256rrrrr rrrvrr)r7rrrrrrr/rr(rcontent_type_headerskwargsr-r8) rrr&rrrrEr7r/r'r9rs~    x             zKey._send_file_internalc Cs|jjj}|s|jdvrdS|drdSd|jkr dkr]nn;|d|_|j}t|tr5| d}|dd}|d d}|dur[|dur[|jd |kr[| d |j|jfdS|jd kr}| }| |j|j |}|jd vr}td|j|ddS)N)iiTlocationrrrr;z/x-amz-server-side-encryption-customer-algorithmzx-goog-encryption-kms-key-namez"%s"z2ETag from S3 did not match computed MD5. %s vs. %si)RequestTimeoutzSaw %s, retrying)rF)rrDrErrkrrr<rUrWrrrr error_coder ) r7rrrEr-amz_server_side_encryption_customer_algorithm goog_customer_managed_encryptionrerrr8r8r9rsN         zKey.should_retrycCs t||d\}}}||_||fS)a :type fp: file :param fp: File pointer to the file to MD5 hash. The file pointer will be reset to the same position before the method returns. :type size: int :param size: (optional) The Maximum number of bytes to read from the file pointer (fp). This is useful when uploading a file in multiple parts where the file is being split in place into different parts. Less bytes may be available. )r/)rr/)r7rr/ hex_digest b64_digest data_sizer8r8r9rs zKey.compute_md5c Cs|jjj} | std| |jr|jdkrtd|dur#i}|r*||| j<|r8d|_| j r8|j|| j <|jdurV|sH|j |jrHdS|j |||||d| ddSdS)a" Store an object using the name of the Key object as the key in cloud and the contents of the data stream pointed to by 'fp' as the contents. The stream object is not seekable and total size is not known. This has the implication that we can't specify the Content-Size and Content-MD5 in the header. So for huge uploads, the delay in calculating MD5 is avoided but with a penalty of inability to verify the integrity of the uploaded data. :type fp: file :param fp: the file whose contents are to be uploaded :type headers: dict :param headers: additional HTTP headers to be sent with the PUT request. :type replace: bool :param replace: If this parameter is False, the method will first check to see if an object exists in the bucket with the same key. If it does, it won't overwrite it. The default value is True which will overwrite the object. :type cb: function :param cb: a callback function that will be called to report progress on the upload. The callback should accept two integer parameters, the first representing the number of bytes that have been successfully transmitted to GS and the second representing the total number of bytes that need to be transmitted. :type num_cb: int :param num_cb: (optional) If a callback is specified with the cb parameter, this parameter determines the granularity of the callback by defining the maximum number of times the callback will be called during the file transfer. :type policy: :class:`boto.gs.acl.CannedACLStrings` :param policy: A canned ACL policy that will be applied to the new key in GS. :type reduced_redundancy: bool :param reduced_redundancy: If True, this will set the storage class of the new Key to be REDUCED_REDUNDANCY. The Reduced Redundancy Storage (RRS) feature of S3, provides lower redundancy at lower storage cost. :type size: int :param size: (optional) The Maximum number of bytes to read from the file pointer (fp). This is useful when uploading a file in multiple parts where you are splitting the file up into different ranges to be uploaded. If not specified, the default behaviour is to read all bytes from the file pointer. Less bytes may be available. z$%s does not support chunked transferrzACannot determine the destination object name for the given streamNrT)rr/) rrDrEsupports_chunked_transferrrprrrrorr) r7rrrrrrrrr/rEr8r8r9set_contents_from_streams. =    zKey.set_contents_from_streamc  Cs|jjj} |pi}|r||| j<| rd|| j<| r!|dtjn1t|t sR| }|dtj | |krM|dtj| |krM||t d|||r`d|_ | jr`|j || j<t|dri|j|_|jdur|sz| rzd}d|_nbd}t|t r|}|j| } | |_td |jr|jd }|tt|f}|s||| }|j} n | r| |_n| }|dtj | ||_|||j} |d|_|d |_|jdur|j|_|s|j |jrdS|j!||||| || d |jSdS) a Store an object in S3 using the name of the Key object as the key in S3 and the contents of the file pointed to by 'fp' as the contents. The data is read from 'fp' from its current position until 'size' bytes have been read or EOF. :type fp: file :param fp: the file whose contents to upload :type headers: dict :param headers: Additional HTTP headers that will be sent with the PUT request. :type replace: bool :param replace: If this parameter is False, the method will first check to see if an object exists in the bucket with the same key. If it does, it won't overwrite it. The default value is True which will overwrite the object. :type cb: function :param cb: a callback function that will be called to report progress on the upload. The callback should accept two integer parameters, the first representing the number of bytes that have been successfully transmitted to S3 and the second representing the size of the to be transmitted object. :type num_cb: int :param num_cb: (optional) If a callback is specified with the cb parameter this parameter determines the granularity of the callback by defining the maximum number of times the callback will be called during the file transfer. :type policy: :class:`boto.s3.acl.CannedACLStrings` :param policy: A canned ACL policy that will be applied to the new key in S3. :type md5: A tuple containing the hexdigest version of the MD5 checksum of the file as the first element and the Base64-encoded version of the plain checksum as the second element. This is the same format returned by the compute_md5 method. :param md5: If you need to compute the MD5 for any reason prior to upload, it's silly to have to do it twice so this param, if present, will be used as the MD5 values of the file. Otherwise, the checksum will be computed. :type reduced_redundancy: bool :param reduced_redundancy: If True, this will set the storage class of the new Key to be REDUCED_REDUNDANCY. The Reduced Redundancy Storage (RRS) feature of S3, provides lower redundancy at lower storage cost. :type encrypt_key: bool :param encrypt_key: If True, the new copy of the object will be encrypted on the server-side by S3 and will be stored in an encrypted form while at rest in S3. :type size: int :param size: (optional) The Maximum number of bytes to read from the file pointer (fp). This is useful when uploading a file in multiple parts where you are splitting the file up into different ranges to be uploaded. If not specified, the default behaviour is to read all bytes from the file pointer. Less bytes may be available. :type rewind: bool :param rewind: (optional) If True, the file pointer (fp) will be rewound to the start before any bytes are read from it. The default behaviour is False which reads from the current position of the file pointer (fp). :rtype: int :return: The number of bytes written to the key. rrz8fp is at EOF. Use rewind option or seek() to data start.rrNTFz^"[a-fA-F0-9]{32}"$r~rxr)"rrDrErrjrosSEEK_SETr<r rSEEK_ENDAttributeErrorrrohasattrrr,r?r/getkeyrmatchrrybase64 b64encoderNrerrrhrr)r7rrrrrrrrrrr/rewindrEr'rrrr8r8r9set_contents_from_fileost O                   zKey.set_contents_from_filec CsJt|d} |j| |||||||| d WdS1swYdS)a# Store an object in S3 using the name of the Key object as the key in S3 and the contents of the file named by 'filename'. See set_contents_from_file method for details about the parameters. :type filename: string :param filename: The name of the file that you want to put onto S3 :type headers: dict :param headers: Additional headers to pass along with the request to AWS. :type replace: bool :param replace: If True, replaces the contents of the file if it already exists. :type cb: function :param cb: a callback function that will be called to report progress on the upload. The callback should accept two integer parameters, the first representing the number of bytes that have been successfully transmitted to S3 and the second representing the size of the to be transmitted object. :type cb: int :param num_cb: (optional) If a callback is specified with the cb parameter this parameter determines the granularity of the callback by defining the maximum number of times the callback will be called during the file transfer. :type policy: :class:`boto.s3.acl.CannedACLStrings` :param policy: A canned ACL policy that will be applied to the new key in S3. :type md5: A tuple containing the hexdigest version of the MD5 checksum of the file as the first element and the Base64-encoded version of the plain checksum as the second element. This is the same format returned by the compute_md5 method. :param md5: If you need to compute the MD5 for any reason prior to upload, it's silly to have to do it twice so this param, if present, will be used as the MD5 values of the file. Otherwise, the checksum will be computed. :type reduced_redundancy: bool :param reduced_redundancy: If True, this will set the storage class of the new Key to be REDUCED_REDUNDANCY. The Reduced Redundancy Storage (RRS) feature of S3, provides lower redundancy at lower storage cost. :type encrypt_key: bool :param encrypt_key: If True, the new copy of the object will be encrypted on the server-side by S3 and will be stored in an encrypted form while at rest in S3. :rtype: int :return: The number of bytes written to the key. rbrN)rrK) r7r'rrrrrrrrrr8r8r9set_contents_from_filename!s = $zKey.set_contents_from_filenamec CsDt|ts |d}t|} |j| |||||||| d } | | S)ah Store an object in S3 using the name of the Key object as the key in S3 and the string 's' as the contents. See set_contents_from_file method for details about the parameters. :type headers: dict :param headers: Additional headers to pass along with the request to AWS. :type replace: bool :param replace: If True, replaces the contents of the file if it already exists. :type cb: function :param cb: a callback function that will be called to report progress on the upload. The callback should accept two integer parameters, the first representing the number of bytes that have been successfully transmitted to S3 and the second representing the size of the to be transmitted object. :type num_cb: int :param num_cb: (optional) If a callback is specified with the num_cb parameter this parameter determines the granularity of the callback by defining the maximum number of times the callback will be called during the file transfer. :type policy: :class:`boto.s3.acl.CannedACLStrings` :param policy: A canned ACL policy that will be applied to the new key in S3. :type md5: A tuple containing the hexdigest version of the MD5 checksum of the file as the first element and the Base64-encoded version of the plain checksum as the second element. This is the same format returned by the compute_md5 method. :param md5: If you need to compute the MD5 for any reason prior to upload, it's silly to have to do it twice so this param, if present, will be used as the MD5 values of the file. Otherwise, the checksum will be computed. :type reduced_redundancy: bool :param reduced_redundancy: If True, this will set the storage class of the new Key to be REDUCED_REDUNDANCY. The Reduced Redundancy Storage (RRS) feature of S3, provides lower redundancy at lower storage cost. :type encrypt_key: bool :param encrypt_key: If True, the new copy of the object will be encrypted on the server-side by S3 and will be stored in an encrypted form while at rest in S3. r;rM)r<rUr>rrKr) r7 string_datarrrrrrrrrrr8r8r9set_contents_from_stringds 9 zKey.set_contents_from_stringc Cs"|j||||||||ddd dS)aN Retrieves a file from an S3 Key :type fp: file :param fp: File pointer to put the data into :type headers: string :param: headers to send when retrieving the files :type cb: function :param cb: a callback function that will be called to report progress on the upload. The callback should accept two integer parameters, the first representing the number of bytes that have been successfully transmitted to S3 and the second representing the size of the to be transmitted object. :type cb: int :param num_cb: (optional) If a callback is specified with the cb parameter this parameter determines the granularity of the callback by defining the maximum number of times the callback will be called during the file transfer. :type torrent: bool :param torrent: Flag for whether to get a torrent for the file :type override_num_retries: int :param override_num_retries: If not None will override configured num_retries parameter for underlying GET. :type response_headers: dict :param response_headers: A dictionary containing HTTP headers/values that will override any headers associated with the stored object in the response. See http://goo.gl/EWOPb for details. :type version_id: str :param version_id: The ID of a particular version of the object. If this parameter is not supplied but the Key object has a ``version_id`` attribute, that value will be used when retrieving the object. You can set the Key object's ``version_id`` attribute to None to always grab the latest version from a version-enabled bucket. N) rrrtorrentr0rrrr)_get_file_internal) r7rrrrrQr0rrr8r8r9get_files / z Key.get_filec  s|duri}|jjj} |jjjdkrd|jj_| pg} |r"| ddur,|s,dtitfddp5iD} |dur@|j}|rI| d||r_|D]} | d| tj || fqMd | } |j d || |d d}|r|j duryd}n|j }|j dur|d krd |j }n|dkrtt||j |d}n |dkrd }nd}d}|||z=|D]8}|||t|7}| D] }| ||q|r|dkr||krn|d7}||ks|d kr|||d}qWnty}z|jtjkrtd|jd}~ww|r|dks|dkr|dkr|||| D] }| ||j|<q!|j dur?|s?d|vr?||_ || |jj_dS)NrxrrQrc3rrAr8rrr8r9rrz)Key._get_file_internal..z versionId=%sz%s=%s&rrrdr rz$Out of space for destination file %sRange)rrDrappendrr.r0rparsequotejoinrr/rrrrwriter_rr-errnoENOSPCr rrgr6r)r7rrrrrQr0rrrrrr&rr r!r"r{rUrer8rr9rRs            $ zKey._get_file_internalcCs|j||||ddS)a Get a torrent file (see to get_file) :type fp: file :param fp: The file pointer of where to put the torrent :type headers: dict :param headers: Headers to be passed :type cb: function :param cb: a callback function that will be called to report progress on the upload. The callback should accept two integer parameters, the first representing the number of bytes that have been successfully transmitted to S3 and the second representing the size of the to be transmitted object. :type cb: int :param num_cb: (optional) If a callback is specified with the cb parameter this parameter determines the granularity of the callback by defining the maximum number of times the callback will be called during the file transfer. T)rQ)rS)r7rrrrr8r8r9get_torrent_file)szKey.get_torrent_filec CsJ|jdur#|r|j|||||||ddS|j|||||||ddSdS)a Retrieve an object from S3 using the name of the Key object as the key in S3. Write the contents of the object to the file pointed to by 'fp'. :type fp: File -like object :param fp: :type headers: dict :param headers: additional HTTP headers that will be sent with the GET request. :type cb: function :param cb: a callback function that will be called to report progress on the upload. The callback should accept two integer parameters, the first representing the number of bytes that have been successfully transmitted to S3 and the second representing the size of the to be transmitted object. :type cb: int :param num_cb: (optional) If a callback is specified with the cb parameter this parameter determines the granularity of the callback by defining the maximum number of times the callback will be called during the file transfer. :type torrent: bool :param torrent: If True, returns the contents of a torrent file as a string. :type res_upload_handler: ResumableDownloadHandler :param res_download_handler: If provided, this handler will perform the download. :type response_headers: dict :param response_headers: A dictionary containing HTTP headers/values that will override any headers associated with the stored object in the response. See http://goo.gl/EWOPb for details. :type version_id: str :param version_id: The ID of a particular version of the object. If this parameter is not supplied but the Key object has a ``version_id`` attribute, that value will be used when retrieving the object. You can set the Key object's ``version_id`` attribute to None to always grab the latest version from a version-enabled bucket. N)rQr0rQr0r)rrS) r7rrrrrQr0res_download_handlerrr8r8r9get_contents_to_fileDs 6  zKey.get_contents_to_filec Csz$t|d} |j| |||||||dWdn1swYWn ty1t|w|jdur]ztj|j} t tj | } t | j | | fWdSty\YdSwdS)aO Retrieve an object from S3 using the name of the Key object as the key in S3. Store contents of the object to a file named by 'filename'. See get_contents_to_file method for details about the parameters. :type filename: string :param filename: The filename of where to put the file contents :type headers: dict :param headers: Any additional headers to send in the request :type cb: function :param cb: a callback function that will be called to report progress on the upload. The callback should accept two integer parameters, the first representing the number of bytes that have been successfully transmitted to S3 and the second representing the size of the to be transmitted object. :type num_cb: int :param num_cb: (optional) If a callback is specified with the cb parameter this parameter determines the granularity of the callback by defining the maximum number of times the callback will be called during the file transfer. :type torrent: bool :param torrent: If True, returns the contents of a torrent file as a string. :type res_upload_handler: ResumableDownloadHandler :param res_download_handler: If provided, this handler will perform the download. :type response_headers: dict :param response_headers: A dictionary containing HTTP headers/values that will override any headers associated with the stored object in the response. See http://goo.gl/EWOPb for details. :type version_id: str :param version_id: The ID of a particular version of the object. If this parameter is not supplied but the Key object has a ``version_id`` attribute, that value will be used when retrieving the object. You can set the Key object's ``version_id`` attribute to None to always grab the latest version from a version-enabled bucket. wb)rQr0r`rN) rra ExceptionrAremover)emailr parsedate_tzr mktime_tzutimer) r7r'rrrrQr0r`rrmodified_tuplemodified_stampr8r8r9get_contents_to_filenames.6      zKey.get_contents_to_filenamec Cs<t}|j|||||||d|} |dur| |} | S)ao Retrieve an object from S3 using the name of the Key object as the key in S3. Return the contents of the object as a string. See get_contents_to_file method for details about the parameters. :type headers: dict :param headers: Any additional headers to send in the request :type cb: function :param cb: a callback function that will be called to report progress on the upload. The callback should accept two integer parameters, the first representing the number of bytes that have been successfully transmitted to S3 and the second representing the size of the to be transmitted object. :type cb: int :param num_cb: (optional) If a callback is specified with the cb parameter this parameter determines the granularity of the callback by defining the maximum number of times the callback will be called during the file transfer. :type torrent: bool :param torrent: If True, returns the contents of a torrent file as a string. :type response_headers: dict :param response_headers: A dictionary containing HTTP headers/values that will override any headers associated with the stored object in the response. See http://goo.gl/EWOPb for details. :type version_id: str :param version_id: The ID of a particular version of the object. If this parameter is not supplied but the Key object has a ``version_id`` attribute, that value will be used when retrieving the object. You can set the Key object's ``version_id`` attribute to None to always grab the latest version from a version-enabled bucket. :type encoding: str :param encoding: The text encoding to use, such as ``utf-8`` or ``iso-8859-1``. If set, then a string will be returned. Defaults to ``None`` and returns bytes. :rtype: bytes or str :returns: The contents of the file as bytes or a string r_N)rragetvaluerW) r7rrrrQr0rencodingrrJr8r8r9get_contents_as_strings6 zKey.get_contents_as_stringcCs,|j|d}|j|||j||ddS)a Convenience method that provides a quick way to add an email grant to a key. This method retrieves the current ACL, creates a new grant based on the parameters passed in, adds that grant to the ACL and then PUT's the new ACL back to S3. :type permission: string :param permission: The permission being granted. Should be one of: (READ, WRITE, READ_ACP, WRITE_ACP, FULL_CONTROL). :type email_address: string :param email_address: The email address associated with the AWS account your are granting the permission to. :type recursive: boolean :param recursive: A boolean value to controls whether the command will apply the grant to all keys within the bucket or not. The default value is False. By passing a True value, the call will iterate through all keys in the bucket and apply the same grant to each key. CAUTION: If you have a lot of keys, this could take a long time! rN)racladd_email_grantr)r7 permission email_addressrrr8r8r9rps zKey.add_email_grantcCs0|j|d}|jj|||d|j||ddS)a Convenience method that provides a quick way to add a canonical user grant to a key. This method retrieves the current ACL, creates a new grant based on the parameters passed in, adds that grant to the ACL and then PUT's the new ACL back to S3. :type permission: string :param permission: The permission being granted. Should be one of: (READ, WRITE, READ_ACP, WRITE_ACP, FULL_CONTROL). :type user_id: string :param user_id: The canonical user id associated with the AWS account your are granting the permission to. :type display_name: string :param display_name: An option string containing the user's Display Name. Only required on Walrus. r) display_nameN)rroadd_user_grantr)r7rquser_idrrsrr8r8r9rt)s  zKey.add_user_grantcCsPt|tkrt}|D] }||q |Si}|D] }||||<q|SrA)typesetaddr)r7r norm_metadatakr8r8r9_normalize_metadataBs zKey._normalize_metadatacCs~i}|jD]}t||rt||}|r|dd}|||<q|jj}|jD]}|dd}|j||d||f<q'|S)z Extracts metadata from existing URI into a dict, so we can overwrite/delete from it to form the new set of metadata to apply to a key. rrz%s%s)%_underscore_base_user_settable_fieldsrEr`rrrEmetadata_prefixr )r7rr underscore_namerJ field_nameprefixr8r8r9_get_remote_metadataMs       zKey._get_remote_metadatac Cs||}||}|}|||D] }||vr||=q|j}i}|D]}|ds2|dr=|dddd} n|} |||| <q&|}|j|j|jj|j|||ddS)Nz x-goog-meta-z x-amz-meta-r)r rr)r{rrr startswithrrr) r7 metadata_plusmetadata_minusrrr h src_bucketrewritten_metadata rewritten_hr8r8r9set_remote_metadatacs,     zKey.set_remote_metadatacCsT|jjjd|jj|j|j||dd}|jdvr(|jjj}||j|j| dS)a Restore an object from an archive. :type days: int :param days: The lifetime of the restored object (must be at least 1 day). If the object is already restored then this parameter can be used to readjust the lifetime of the restored object. In this case, the days param is with respect to the initial time of the request. If the object has not been restored, this param is with respect to the completion time of the request. POSTrestore)rrr)rN) rrDrr RestoreBodyrrErrr)r7daysrrrEr8r8r9r|s   z Key.restore)NN)F)NrNN)rNNN)r)NT)NFFFTrA) rNTFNFNNFF)NNrNFN)NNrNFNN)NTNrNFNN) NTNrNNFNFNF)NTNrNNFF)NNrFNNN) NNrFNNNNN)NNr)V__name__ __module__ __qualname____doc__r"rrconfiggetintrrwrr|frxrrr:r@rCpropertyrErGrKrrPrSrrYr\rhrbrcrrirmrqrvrrrrrrrr__next__rrrrrrrrrrrrrrrrrrrrrrrr@rKrNrPrSrRr^rarkrnrprtr{rrrr8r8r8r9r0s             6     % <          P +  M 7 X 3 C B 6 M @ I A   r)# email.utilsrer[r2r/rArrHrNrr boto.utilsr boto.compatrrrrboto.exceptionrr r boto.providerr boto.s3.keyfiler boto.s3.userr rrrrrobjectrr8r8r8r9s.