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 年前9961 查看次数
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
专家
已审核 1 个月前
  • 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}
                    )
    

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则