【发布时间】:2018-06-02 06:37:52
【问题描述】:
我想将文件从一个 s3 存储桶路径(例如 B1/x/*)传输到另一个 S3 存储桶(例如 B2/y/*),其中 B1 和 B2 是两个 s3 存储桶,x 和 y 是其中包含 csv 文件的文件夹分别。
我写了下面的脚本来做到这一点。但我收到错误“object_list”未定义。此外,我不确定它是否会执行传输文件的工作。
参考下面的脚本:
import boto3
s3 = boto3.client("s3")
# list_objects_v2() give more info
more_objects=True
found_token = True
while more_objects :
if found_token :
response= s3.list_objects_v2(
Bucket="B1",
Prefix="x/",
Delimiter="/")
else:
response= s3.list_objects_v2(
Bucket="B1",
ContinuationToken=found_token,
Prefix="x/",
Delimiter="/")
# use copy_object or copy_from
for source in object_list["Contents"]:
raw_name = source["Key"].split("/")[-1]
new_name = "new_structure/{}".format(raw_name)
s3.copy_from(CopySource='B1/x')
# Now check there is more objects to list
if "NextContinuationToken" in response:
found_token = response["NextContinuationToken"]
more_objects = True
else:
more_objects = False
如果有人可以帮助我对上述脚本进行更改,那将非常有帮助。
谢谢
【问题讨论】:
标签: python-3.x amazon-s3 amazon boto3