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年前9941ビュー
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}
                    )
    

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ