How to clone a key in Amazon S3 using Python (and boto)?

前端 未结 6 1158
温柔的废话
温柔的废话 2021-02-19 01:27

I have a file contained in a key in my S3 bucket. I want to create a new key, which will contain the same file. Is it possible to do without downloading that file? I\'m looking

相关标签:
6条回答
  • 2021-02-19 02:14

    Although nobody asked, I thought it might be relevant to show how to do this with simples3:

    >>> b.copy("my_bucket/file.txt", "file_copy.txt", acl="public")
    

    I'm not sure what Boto does here, but it's worth noting that the permissions (ACL) will not be copied by S3, it will be reset to "private" if nothing else is specified. To copy the ACL, you have to ask for it first.

    0 讨论(0)
  • 2021-02-19 02:18

    Where bucket is the destination bucket:

    bucket.copy_key(new_key,source_bucket,source_key)
    
    0 讨论(0)
  • 2021-02-19 02:21
    from boto.s3.key import Key
    
    #Get source key from bucket by name
    source_key = source_bucket.get_key(source_key_name)
    
    #Copy source key to a new bucket with a new key name (can be the same as source)
    #Note: source_key is Key
    source_key.copy(dest_bucket_name,dest_key_name)
    
    #The signature of boto's Key class:
    def copy(self, dst_bucket, dst_key, metadata=None,
                 reduced_redundancy=False, preserve_acl=False,
                 encrypt_key=False, validate_dst_bucket=True)
    
    #set preserve_acl=True to copy the acl from the source key
    
    0 讨论(0)
  • 2021-02-19 02:23

    S3 allows object by object copy. The CopyObject operation creates a copy of an object when you specify the key and bucket of a source object and the key and bucket of a target destination. Not sure if boto has a compact implementation.

    0 讨论(0)
  • 2021-02-19 02:28

    Browsing through boto's source code I found that the Key object has a "copy" method. Thanks for your suggestion about CopyObject operation.

    0 讨论(0)
  • 2021-02-19 02:31

    Note that the 'copy' method on the Key object has a "preserve_acl" parameter (False by default) that will copy the source's ACL to the destination object.

    0 讨论(0)
提交回复
热议问题