在同一存储桶上复制S3中的大量文件

发布于 2025-02-06 04:31:45 字数 479 浏览 0 评论 0原文

我在带有80 tb〜的S3存储桶上有一个“目录”,我需要将所有内容复制到同一存储桶中的另一个目录

source = s3:// mybucket/abc/abc/process/

destiny = s3:// mybucket/cde/process/

我已经尝试使用AWS S3 Sync,但仅用于大文件,仍然留下50 TB进行复制。我正在考虑将BOTO3代码作为下面的示例使用,但是我不知道该如何为多个文件/目录进行递归。

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

我该如何使用Boto3进行操作?

I got a "directory" on a s3 bucket with 80 TB ~ and I need do copy everything to another directory in the same bucket

source = s3://mybucket/abc/process/

destiny = s3://mybucket/cde/process/

I already tried to use aws s3 sync, but worked only for the big files, still left 50 TB to copy. I'm thinking about to use a boto3 code as this example below, but I don't know how to do for multiple files/directories recursively.

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

How can I do this using the boto3?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2025-02-13 04:31:45

虽然可能有更好的方法可以使用桶策略来实现Boto3。
首先,您需要获取

bucket_items =   s3_client.list_objects_v2(Bucket=source_bucket,Prefix=source_prefix)
bucket_contents = bucket_items.get('Contents',[])

source_bucket是bucket的名称的存储桶内容的列表,而source_prefix是文件夹的名称。
接下来,您将在内容上迭代,每个项目都将S3.META.CLIENT.COPY方法称为类似的内容,因此

for content in bucket_contents:
        copy_source = {
            'Bucket': source_bucket,
            'Key': content['Key']
        }
        s3.meta.client.copy(copy_source, source_bucket, destination_prefix + '/' + content['Key'].split('/')[-1])

内容是字典,因此您必须使用“键”获取项目的名称并使用split将其分解为前缀和文件名。

While there may be better ways of doing this using bucket policies, it can be done using boto3.
first you will need to get a list of the contents of the bucket

bucket_items =   s3_client.list_objects_v2(Bucket=source_bucket,Prefix=source_prefix)
bucket_contents = bucket_items.get('Contents',[])

Where source_bucket is the name of the bucket and source_prefix is the name of the folder.
Next you will iterate over the contents and for each item call the s3.meta.client.copy method like so

for content in bucket_contents:
        copy_source = {
            'Bucket': source_bucket,
            'Key': content['Key']
        }
        s3.meta.client.copy(copy_source, source_bucket, destination_prefix + '/' + content['Key'].split('/')[-1])

the contents are a dictionary so you must use 'Key' to get the name of the item and use split to break it into prefix and file name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文