vishal
vishal

Reputation: 1874

how to copy s3 object from one bucket to another using python boto3

I want to copy a file from one s3 bucket to another. I get the following error:

s3.meta.client.copy(source,dest)
TypeError: copy() takes at least 4 arguments (3 given)

I'am unable to find a solution by reading the docs. Here is my code:

#!/usr/bin/env python
import boto3
s3 = boto3.resource('s3')
source= { 'Bucket' : 'bucketname1','Key':'objectname'}
dest ={ 'Bucket' : 'Bucketname2','Key':'backupfile'}
s3.meta.client.copy(source,dest)

Upvotes: 66

Views: 176087

Answers (3)

Jibran
Jibran

Reputation: 913

This is the syntax from docs:

import boto3

s3 = boto3.resource('s3')
copy_source = {
    'Bucket': 'mybucket',
    'Key': 'mykey'
}
s3.meta.client.copy(copy_source, 'otherbucket', 'otherkey')

Upvotes: 9

Adarsh
Adarsh

Reputation: 3573

You can try:

import boto3
s3 = boto3.resource('s3')
copy_source = {
      'Bucket': 'mybucket',
      'Key': 'mykey'
    }
bucket = s3.Bucket('otherbucket')
bucket.copy(copy_source, 'otherkey')

or

import boto3
s3 = boto3.resource('s3')
copy_source = {
    'Bucket': 'mybucket',
    'Key': 'mykey'
 }
s3.meta.client.copy(copy_source, 'otherbucket', 'otherkey')

Note the difference in the parameters

Upvotes: 139

hjpotter92
hjpotter92

Reputation: 80629

Since you are using s3 service resource, why not use its own copy method all the way?

#!/usr/bin/env python
import boto3
s3 = boto3.resource('s3')
source= { 'Bucket' : 'bucketname1', 'Key': 'objectname'}
dest = s3.Bucket('Bucketname2')
dest.copy(source, 'backupfile')

Upvotes: 13

Related Questions