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

十年热恋 提交于 2019-12-01 03:34:05

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

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')

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')

You have to give detination bucket and key seperately. http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.copy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!