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 年前檢視次數 9913 次
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}
                    )
    

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南