Amazon S3 boto: как переименовать файл в корзине? - PullRequest
70 голосов
/ 20 марта 2010

Как переименовать ключ S3 в ведро с бото?

Ответы [ 4 ]

68 голосов
/ 20 марта 2010

Вы не можете переименовывать файлы в Amazon S3. Вы можете скопировать их с новым именем, а затем удалить оригинал, но нет правильной функции переименования.

39 голосов
/ 09 мая 2012

Вот пример функции Python, которая будет копировать объект S3 с использованием 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)
0 голосов
/ 28 сентября 2016

Нет прямого способа переименовать файл в s3.что вам нужно сделать, это скопировать существующий файл с новым именем (просто установите целевой ключ) и удалить старый.Спасибо

0 голосов
/ 14 ноября 2011
//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);
...