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

demandé il y a 2 ans9824 vues
1 réponse
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
INGÉNIEUR EN ASSISTANCE TECHNIQUE
répondu il y a 2 ans
profile picture
EXPERT
vérifié il y a 25 jours
  • 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}
                    )
    

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions