如何使用python boto3将s3对象从一个存储桶复制到另一个存储桶

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47468148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:13:30  来源:igfitidea点击:

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

pythonamazon-web-servicesamazon-s3boto3

提问by vishal.k

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

我想将文件从一个 s3 存储桶复制到另一个存储桶。我收到以下错误:

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

s3.meta.client.copy(source,dest)
TypeError: copy() 需要至少 4 个参数(给定 3 个)

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)

回答by Adarsh

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

注意参数的不同

回答by hjpotter92

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

由于您使用S3服务的资源,为什么不使用自己的copy方法,所有的方式?

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

回答by Jibran

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

你必须分别给定桶和钥匙。 http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.copy