Amazon S3 boto: How do you rename a file in a bucket?

前端 未结 4 1115
鱼传尺愫
鱼传尺愫 2020-12-08 13:22

How do you rename a S3 key in a bucket with boto?

4条回答
  •  醉梦人生
    2020-12-08 13:45

    Here is an example of a Python function that will copy an S3 object using Boto 2:

    import boto
    
    def copy_object(src_bucket_name,
                    src_key_name,
                    dst_bucket_name,
                    dst_key_name,
                    metadata=None,
                    preserve_acl=True):
        """
        Copy an existing object to another location.
    
        src_bucket_name   Bucket containing the existing object.
        src_key_name      Name of the existing object.
        dst_bucket_name   Bucket to which the object is being copied.
        dst_key_name      The name of the new object.
        metadata          A dict containing new metadata that you want
                          to associate with this object.  If this is None
                          the metadata of the original object will be
                          copied to the new object.
        preserve_acl      If True, the ACL from the original object
                          will be copied to the new object.  If False
                          the new object will have the default ACL.
        """
        s3 = boto.connect_s3()
        bucket = s3.lookup(src_bucket_name)
    
        # Lookup the existing object in S3
        key = bucket.lookup(src_key_name)
    
        # Copy the key back on to itself, with new metadata
        return key.copy(dst_bucket_name, dst_key_name,
                        metadata=metadata, preserve_acl=preserve_acl)
    

提交回复
热议问题