{r Qc@s*ddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl mZddlmZddlmZddl mZddl mZyddlmZWn!ek rddlmZnXd efd YZdS( iN(tBotoClientError(tProvider(tKeyFile(tUser(t UserAgent(t compute_md5(tmd5tKeyc BseZdZdZdZdZedddddd gZeZx'eD]Z ej e j d d qLWdEdEd Z d ZdZdZdZedZdZdZedZdZdEddEdEdZdEdEdZddEdEdEdZeZedZdZddZdEe dZ!dEeeee d Z"d!Z#d"Z$d#Z%d$Z&d%Z'd&Z(d'Z)dEd(Z*dEd)Z+dEd*Z,dEd+Z-dEd,Z.d-Z/d.Z0dEd/Z1d0dEe edEedEdEeed1 Z2dEdEd2dEedEd3Z3dEd4Z4dEe dEd2dEedEdEd5Z5dEe dEd2dEdEedEedEed6 Z6dEe dEd2dEdEeed7Z7dEe dEd2dEdEeed8Z8dEdEd2edEdEdEd9Z9dEdEd2edEdEdEdEd:Z:dEdEd2d;Z;dEdEd2edEdEdEd<Z<dEdEd2edEdEdEd=Z=dEdEd2edEdEd>Z>dEd?Z?dEdEd@Z@dAZAdEdBZBdEdCZCdEdDZDRS(FsC 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. sapplication/octet-streams %s i s cache-controlscontent-dispositionscontent-encodingscontent-languages content-md5s content-typet-t_cCs||_||_i|_d|_|j|_d|_d|_d|_ d|_ d|_ t |_ d|_d|_d|_d|_d|_d|_d|_d|_d|_d|_d|_t |_d|_d|_d|_dS(NtSTANDARD(tbuckettnametmetadatatNonet cache_controltDefaultContentTypet content_typetcontent_encodingtcontent_dispositiontcontent_languagetfilenametetagtFalset is_latestt last_modifiedtownert storage_classRt base64md5tpathtresptmodetsizet version_idtsource_version_idt delete_markert encryptedtongoing_restoret expiry_date(tselfR R ((s./tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/s3/key.pyt__init__as4                         cCs/|jr d|jj|jfSd|jSdS(Ns s(R R (R'((s./tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/s3/key.pyt__repr__s cCs|dkr|jStdS(Ntkey(R tAttributeError(R'R ((s./tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/s3/key.pyt __getattr__s cCs-|dkr||jd|dd!}n||fS(s A utility function to create the 2-tuple (md5hexdigest, base64md5) from just having a precalculated md5_hexdigest. is i(tbinasciit unhexlifytbase64t encodestring(R't md5_hexdigesttdigestR((s./tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/s3/key.pytget_md5_from_hexdigests cCs@|jjj}|jr3|j|jd|_n d|_dS(N(R R1R2tserver_side_encryption_headert getheaderRR$(R'RR2((s./tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/s3/key.pythandle_encryption_headerss cCs|jjj}|jdks$|r?|j|jd|_n|j|jd|_|j|jddkr~t |_n t |_dS(Ntfalsettrue( R R1R2R!RR;tcopy_source_version_idR"R#tTrueR(R'RtforceR2((s./tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/s3/key.pythandle_version_headerss   cCs|jd}|dkrdS|jdd}x|D]}g|jdD]}|j^qN\}}|jdd}|dkr|jdkrtnt|_q8|d kr8||_ q8q8WdS( Ns x-amz-restoret,it=t"tsongoing-requestR>s expiry-date( R;RtsplittstriptreplacetlowerR@RR%R&(R'tresponsetheadertpartstparttiR*tval((s./tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/s3/key.pythandle_restore_headerss  . $ RFc CsW|jdkrSd|_|jjj}|jjjd|jj|j|d|d||_|jjdks|jjdkr|jj }|j |jj|jj |n|jj }t jj|||_xU|jD]G\}}|jdkr!d|kr!t||_q|jd krZtjd d |} t| |_q|jd krx||_q|jd kr||_q|jdkr||_q|jdkr||_q|jdkr||_q|jdkr||_q|jdkr||_qqW|j|j|j|jndS(s 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. trtGETt query_argstoverride_num_retriesii+scontent-lengths Content-Ranges content-ranges.*/(.*)s\1Rs content-typescontent-encodingscontent-languages last-modifieds cache-controlscontent-dispositionN( RRRR R1R2t make_requestR tstatustreadtstorage_response_errortreasontmsgtbototutilstget_aws_metadataR titemsRJtintR tretsubRRRRRRRRBR<( R'theadersRTRUtresponse_headersR2tbodyR R.t end_range((s./tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/s3/key.pyt open_readsJ   $          cCstddS(sK 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. sNot ImplementedN(R(R'RcRU((s./tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/s3/key.pyt open_write s RRcCsv|dkr4d|_|jd|d|d|n>|dkrbd|_|jd|d|ntd|dS(NRRRcRTRUtwsInvalid mode: %s(RRgRhR(R'RRcRTRU((s./tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/s3/key.pytopens      cCs?|jr | r |jjnd|_d|_t|_dS(s 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. N(RRXRRR@tclosed(R'tfast((s./tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/s3/key.pytclose*s   cCs<|j|jj|j}|s8|jtn|S(s 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. (RgRRXt BufferSizeRmt StopIteration(R'tdata((s./tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/s3/key.pytnext@s   icCsQ|j|dkr(|jj}n|jj|}|sM|jn|S(Ni(RgRRXRm(R'R Rp((s./tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/s3/key.pyRXQs   c Cs|dkr7|j|jj|jdtdtd|S|dkrn|j|jj|jdtdtd|Std|dS(s 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 treduced_redundancyt preserve_acltvalidate_dst_buckettREDUCED_REDUNDANCYsInvalid storage class: %sN(tcopyR R RR@R(R'tnew_storage_classt dst_bucketRt((s./tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/s3/key.pytchange_storage_class[s    c Csa|jjj||}|r'd}n |j}|j||jj|j|d|d|d|S(s 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 RuRRst encrypt_key(R R1tlookupRtcopy_keyR ( R'Rxtdst_keyR RrRsRzRtR((s./tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/s3/key.pyRvs/    cCs*|dkr"t||_|jSdSdS(NtOwner(RRR(R'R tattrsR1((s./tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/s3/key.pyt startElements cCs|dkr||_n|dkr0||_n|dkr`|dkrTt|_qt|_n|dkrx||_nm|dkrt||_nO|dkr||_n7|dkrn(|d kr||_ nt |||dS( NRtETagtIsLatestR>t LastModifiedtSizet StorageClassR~t VersionId( R RR@RRRR`R RR!tsetattr(R'R R.R1((s./tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/s3/key.pyt endElements&                cCst|jj|jS(st Returns True if the key exists :rtype: bool :return: Whether the key exists on S3 (tboolR R{R (R'((s./tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/s3/key.pytexistsscCs|jj|jd|jS(s) Delete this key from S3 R!(R t delete_keyR R!(R'((s./tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/s3/key.pytdeletescCs|jj|S(N(R tget(R'R ((s./tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/s3/key.pyt get_metadatascCs||j|tj}|j||||d|d|d||jS(s 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. :rtype: string :returns: The contents of the file as a string RR!Rd(RRtgetvalue(R'RcRRRR!RdR((s./tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/s3/key.pytget_contents_as_strings ) cCs<|jd|}|jj|||j|d|dS(s 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! RcN(Rtacltadd_email_grantR(R't permissiont email_addressRcR((s./tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/s3/key.pyR-scCsB|jd|}|jj||d||j|d|dS(s 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. Rct display_nameN(RRtadd_user_grantR(R'Rtuser_idRcRR((s./tmp/tmp.yUYbTOKr8o/gsutil/boto/boto/s3/key.pyRHscCsqt|tkrBt}xO|D]}|j|jq"Wn+i}x"|D]}||||j||=q>q>W|j}i}xa|D]Y}|jds|jdr|jddjdd} n|} |||| s(