program story

Amazon S3 boto : 버킷에있는 파일의 이름을 어떻게 바꾸나요?

inputbox 2020. 10. 30. 07:59
반응형

Amazon S3 boto : 버킷에있는 파일의 이름을 어떻게 바꾸나요?


boto를 사용하여 버킷의 S3 키 이름을 어떻게 바꾸나요?


Amazon S3에서는 파일 이름을 바꿀 수 없습니다. 새 이름으로 복사 한 다음 원본을 삭제할 수 있지만 적절한 이름 바꾸기 기능은 없습니다.


다음은 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)

s3에서 파일 이름을 바꾸는 직접적인 방법은 없습니다. 해야 할 일은 새 이름으로 기존 파일을 복사하고 (대상 키만 설정) 이전 파일을 삭제하는 것입니다. 감사합니다


//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);

참고 URL : https://stackoverflow.com/questions/2481685/amazon-s3-boto-how-do-you-rename-a-file-in-a-bucket

반응형