What is the ideal way to copy objects from one S3 to another in a different region via boto3

1

Although it's quite seamless to copy objects from one s3 to another in a different region via CLI as shown here, it's almost impossible to find docs on how to do the same via boto3.

It's also seamless to copy (with boto3) from one s3 to another in the same region, as shown here but what about copying to a different region? What's the obvious way to do this?

Following the approach here throws IllegalLocationConstraintException

질문됨 2년 전9893회 조회
1개 답변
1

Hello,

I understand that you want to copy data from one S3 bucket to another S3 bucket in another region using boto3 api.

You can use copy_object() to copy an object in Amazon S3 to another prefix, another bucket and even another Region. The copying takes place entirely within S3, without needing to download/upload the object. [+]https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.copy_object

For example, to copy an object in mybucket from folder1/foo.txt to folder2/foo.txt, you could use:

import boto3

s3_client = boto3.client('s3')

response = s3_client.copy_object(
    CopySource='/mybucket/folder1/foo.txt',  # /Bucket-name/path/filename
    Bucket='mybucket',                       # Destination bucket
    Key='folder2/foo.txt'                    # Destination path/filename
)
AWS
지원 엔지니어
답변함 2년 전
profile picture
전문가
검토됨 한 달 전
  • Doesn't seem to work for all regions:

    An error occurred (IllegalLocationConstraintException) when calling the CopyObject operation: The ap-southeast-4 location constraint is incompatible for the region specific endpoint this request was sent to.
    

    Had to create another client as a workaround:

    aus_client = boto3.client('s3', region_name='ap-southeast-4')
    

    ...and then:

    aus_client.copy_object(
                        Bucket = aus_bucket,
                        Key = filename,
                        CopySource = { 'Bucket': src_bucket, 'Key': filename}
                    )
    

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠