Amazon S3 boto:如何重命名存储桶中的文件?

时间:2021-01-26 10:47:43

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

如何使用boto重命名存储桶中的S3密钥?

4 个解决方案

#1


68  

You can't rename files in Amazon S3. You can copy them with a new name, then delete the original, but there's no proper rename function.

您无法在Amazon S3中重命名文件。您可以使用新名称复制它们,然后删除原始文件,但没有正确的重命名功能。

#2


38  

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

下面是一个使用Boto 2复制S3对象的Python函数示例:

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)

#3


0  

There is no direct method to rename the file in s3. what do you have to do is copy the existing file with new name (Just set the target key) and delete the old one. Thank you

没有直接的方法来重命名s3中的文件。你要做的是用新名称复制现有文件(只需设置目标密钥)并删除旧文件。谢谢

#4


-6  

//Copy the object
AmazonS3Client s3 = new AmazonS3Client("AWSAccesKey", "AWSSecretKey");

CopyObjectRequest copyRequest = new CopyObjectRequest()
      .WithSourceBucket("SourceBucket")
      .WithSourceKey("SourceKey")
      .WithDestinationBucket("DestinationBucket")
      .WithDestinationKey("DestinationKey")
      .WithCannedACL(S3CannedACL.PublicRead);
s3.CopyObject(copyRequest);

//Delete the original
DeleteObjectRequest deleteRequest = new DeleteObjectRequest()
       .WithBucketName("SourceBucket")
       .WithKey("SourceKey");
s3.DeleteObject(deleteRequest);

#1


68  

You can't rename files in Amazon S3. You can copy them with a new name, then delete the original, but there's no proper rename function.

您无法在Amazon S3中重命名文件。您可以使用新名称复制它们,然后删除原始文件,但没有正确的重命名功能。

#2


38  

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

下面是一个使用Boto 2复制S3对象的Python函数示例:

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)

#3


0  

There is no direct method to rename the file in s3. what do you have to do is copy the existing file with new name (Just set the target key) and delete the old one. Thank you

没有直接的方法来重命名s3中的文件。你要做的是用新名称复制现有文件(只需设置目标密钥)并删除旧文件。谢谢

#4


-6  

//Copy the object
AmazonS3Client s3 = new AmazonS3Client("AWSAccesKey", "AWSSecretKey");

CopyObjectRequest copyRequest = new CopyObjectRequest()
      .WithSourceBucket("SourceBucket")
      .WithSourceKey("SourceKey")
      .WithDestinationBucket("DestinationBucket")
      .WithDestinationKey("DestinationKey")
      .WithCannedACL(S3CannedACL.PublicRead);
s3.CopyObject(copyRequest);

//Delete the original
DeleteObjectRequest deleteRequest = new DeleteObjectRequest()
       .WithBucketName("SourceBucket")
       .WithKey("SourceKey");
s3.DeleteObject(deleteRequest);