FRTc@sjddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddlm Z ddl Z ddl mZmZmZmZddlmZddlmZddlmZddlmZddlmZdd lmZdd l mZdd l mZmZdd l mZdd l m Z de!fdYZ"dS(iN(tmd5(tBytesIOtsixturllibt encodebytes(tBotoClientError(tStorageDataError(tPleaseRetryException(tProvider(tKeyFile(tUser(t UserAgent(t compute_md5t compute_hash(tfind_matching_headers(tmerge_headers_by_nametKeyc BseZdZdZdZejjdddZe dddd d d d d gZ e Z x'e D]Z e j e jddqdWe e ddddgBZdVdVdZdZdZedZdZdZeeeZdZdZeeeZdZdZeeeZdZdZ eee Z!d Z"d!Z#e$d"Z%d#Z&d$Z'dVd%dVdVd&Z(dVdVd'Z)d(dVdVdVd)Z*e$Z+e$d*Z,d+Z-e-Z.d,d-Z/dVe0d.Z1dVe$e$e$e0d/Z2d0Z3d1Z4dVd2Z5dVd3Z6d4Z7d5Z8d6Z9dVd7Z:dVd8Z;dVd9Z<dVd:Z=dVd;Z>d<Z?dVd=Z@dVd>ZAd?dVe0e$dVe$dVdVe$e$d@ ZBdVdVdAdVe$dVdBZCdVdVdAdVe$dVdVdCZDe$dDZEdVdEZFdVe0dVdAdVe$dVdVdFZGdVe0dVdAdVdVe$dVe$dVe$dG ZHdVe0dVdAdVdVe$e$dHZIdVe0dVdAdVdVe$e$dIZJdVdVdAe$dVdVdVdJZKdVdVdAe$dVdVdVdVdVdK ZLdVdVdAdLZMdVdVdAe$dVdVdVdMZNdVdVdAe$dVdVdVdNZOdVdVdAe$dVdVdVdOZPdVdPZQdVdVdQZRdRZSdVdSZTdVdTZUdVdUZVRS(WsC 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 tBototkey_buffer_sizei s cache-controlscontent-dispositionscontent-encodingscontent-languages content-md5s content-types x-robots-tagtexpirest-t_s last-modifiedscontent-lengthtdatetetagcCs||_||_i|_d|_|j|_d|_d|_d|_ d|_ d|_ t |_ d|_d|_d|_d|_d|_d|_d|_d|_d|_t |_d|_d|_d|_i|_dS(N(tbuckettnametmetadatatNonet cache_controltDefaultContentTypet content_typetcontent_encodingtcontent_dispositiontcontent_languagetfilenameRtFalset is_latestt last_modifiedtownert_storage_classtpathtresptmodetsizet version_idtsource_version_idt delete_markert encryptedtongoing_restoret expiry_datet local_hashes(tselfRR((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pyt__init__is2                        cCsW|jr%d|jj|jf}n d|j}t|tsS|jd}n|S(Nu usutf-8(RRt isinstancetstrtencode(R3R((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pyt__repr__s   cCs|S(N((R3((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pyt__iter__scCs1d}|jr-|jjr-|jjj}n|S(N(RRt connectiontprovider(R3R;((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pyR;scCs|jS(N(R(R3((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pyt_get_keyscCs ||_dS(N(R(R3tvalue((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pyt_set_keyscCs4d|jkr0|jdr0tj|jdSdS(NR(R2tbinasciitb2a_hex(R3((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pyt_get_md5scCsH|rtj||jd|dkrbd|_|jd|d|ntd|dS(NRuRsRwRxtwsInvalid mode: %s(R*RRR(R3R*RsRwRx((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pytopenRs      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(R)R{RR*R_tclosed(R3tfast((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pytcloseas   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. (RR)R{t BufferSizeRt StopIteration(R3tdata((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pytnextws   icCsQ|j|dkr(|jj}n|jj|}|sM|jn|S(Ni(RR)R{R(R3R+R((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pyR{s   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. RMtreduced_redundancyt preserve_acltvalidate_dst_buckettREDUCED_REDUNDANCYsInvalid storage class: %sN(tcopyRRR#R_R(R3tnew_storage_classt dst_bucketR((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pytchange_storage_classs    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 Rt storage_classRt encrypt_key(RR:tlookupRtcopy_keyR( R3Rtdst_keyRRRRRR((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pyRs/    cCs*|dkr"t||_|jSdSdS(NtOwner(R R&R(R3RtattrsR:((s8/opt/freeware/lib/python2.7/site-packages/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 StorageClassRt VersionId( RRR_R$R#R%RR+RR,tsetattr(R3RR=R:((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pyt endElements&                cCst|jj|jd|S(st Returns True if the key exists :rtype: bool :return: Whether the key exists on S3 Rs(tboolRRR(R3Rs((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pytexistsscCs"|jj|jd|jd|S(s) Delete this key from S3 R,Rs(Rt delete_keyRR,(R3Rs((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pytdeletescCs|jj|S(N(Rtget(R3R((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pyt get_metadata"scCs|jdkr"||jdsc sdk r.jkr.jn'dkrUjrUjdni}tjjd|r}d|d|jdkr(tS|jdr>tSnd|jkoXdknr|jd|_|j}t|t r|j d}n|jdd}|dkr|jd |kr|j d |j|jfqntS|jd krW|j }|j|j|j|}|jdkrWtd |jd|qWntS(Niitlocationii+Rsutf-8s/x-amz-server-side-encryption-customer-algorithms"%s"s2ETag from S3 did not match computed MD5. %s vs. %sitRequestTimeoutsSaw %s, retryingRk(ii(R(RR:R;RzR_RZRRR5RERGRRR{R|R}t error_codeRR#(R3RkRR;Rt)server_side_encryption_customer_algorithmRterr((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pyRs<      cCs.t|d|\}}}||_||fS(s :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+(R R+(R3RR+t hex_digestt b64_digestt data_size((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pyR s  c Cs|jjj} | js4td| jn|j sM|jdkr\tdn|dkrqi}n|r||| jtk r}|jtjkrtd|jnnX|r8|dks|dkr8|dkr8|||nx%| D]}| |j|j|ss versionId=%ss%s=%st&RuRwRxiig?s$Out of space for destination file %stRangei(RRR:RtappendRRR,RtparsetquotetjoinRR+RRRRtwriteRORRterrnotENOSPCRRRVR2R(R3RRsRRRR,RxRRRwRRRpRRRRoRERte((Rs8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pyR s|   "    %   '       * "  cCs|j||||dtS(s 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. R(R!R_(R3RRsRR((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pytget_torrent_filesc Csl|jdk rh|r=|j|||||d|d|qh|j||||d|d|d|ndS(s 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. RR,RN(RRR!( R3RRsRRRR,tres_download_handlerR((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pytget_contents_to_file2s6 c CsyJt|d5} |j| |||d|d|d|d|WdQXWn!tk rmtj|nX|jdk ryJtjj |j} t tjj | } tj | j | | fWqtk rqXndS(sK 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 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. twbRR,R-RN(RR.t ExceptionRtremoveR%RtemailRt parsedate_tzRt mktime_tztutimeR( R3R"RsRRRR,R-RRtmodified_tupletmodified_stamp((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pytget_contents_to_filenamers"6   c Cs_t}|j||||d|d|d||j} |dk r[| j|} n| S(so 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 RR,RN(RR.tgetvalueRRG( R3RsRRRR,RtencodingRR=((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pytget_contents_as_strings6   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! RsN(Rtacltadd_email_grantR(R3t permissiont email_addressRsR((s8/opt/freeware/lib/python2.7/site-packages/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. Rst display_nameN(RR<tadd_user_grantR(R3R>tuser_idRsR@R((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pyRAscCsqt|tkrBt}xO|D]}|j|jq"Wn+i}x"|D]}||||j||=q>q>W|j}i}xa|D]Y}|jds|jdr|jddjdd} n|} |||| RpRARDRRIRLRWRRRSRRXR[R#RaRrRtRRRRRRt__next__R{R_RRRRRRRRRRRRRRRRRRRRRR RRRRR!R R,R.R8R;R=RARHRNRVRX(((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pyR0s               5      #9              M *   3   U   @  ?   4  K ; D=    (#t email.utilsR2R)RRRRRR?RRt boto.utilsRt boto.compatRRRRtboto.exceptionRRRt boto.providerRtboto.s3.keyfileR t boto.s3.userR R R R RRtobjectR(((s8/opt/freeware/lib/python2.7/site-packages/boto/s3/key.pyts,          "