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

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

问题描述:

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

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

s3.meta.client.copy(源,目标)
TypeError:copy()至少接受4个参数(给定3个参数)

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)

您可以尝试:

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

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

请注意参数的差异