本例中“otherkey”的用途是什么。我不明白为什么我们需要的不仅仅是源密钥

2024-06-24 12:55:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我想复制文件从一个s3桶到另一个使用boto3。我想不出我应该用什么来代替“otherkey”。代码是否从复制源获取密钥并将其放入“otherbucket”

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

Tags: 文件代码importobjsources3bucket密钥
1条回答
网友
1楼 · 发布于 2024-06-24 12:55:31

我想出来了

import boto3
s3 = boto3.resource('s3')
copy_source = {
    'Bucket': 'bucket_from', #Name of bucket you want to copy FROM
    'Key': 'key' #file/object you want to copy
}
bucket = s3.Bucket('bucket_to') #name of bucket you want to copy TO
bucket.copy(copy_source, 'dump/new_file_name') # What you want the new copy to be named and where it should be placed
                                               #dump/ is the "subfolder", new_file_name is what the copied file is renamed to.

相关问题 更多 >