小编Mar*_*ark的帖子

如何在 Python 中使用线程来并行化 AWS S3 API 调用?

我编写了一个 Python 脚本,试图通过使用AWS Boto 3 list_objects() 方法来确定所有可用 AWS S3 存储桶的总大小。

逻辑很简单:

  1. 从每个 S3 存储桶中获取对象的初始列表(在 1,000 个对象后自动截断)
  2. 遍历对象列表中的每个对象,将该对象的大小添加到 total_size 变量中
  3. 当存储桶仍有其他对象时,检索它们并重复步骤 2

这是相关的代码片段:

import boto3

s3_client = boto3.client('s3')

# Get all S3 buckets owned by the authenticated sender of the request
buckets = s3_client.list_buckets()

# For each bucket...
for bucket in buckets['Buckets']:
    # Get up to first 1,000 objects in bucket
    bucket_objects = s3_client.list_objects(Bucket=bucket['Name'])

    # Initialize total_size
    total_size = 0

    # Add size of each individual item in bucket …
Run Code Online (Sandbox Code Playgroud)

python concurrency multithreading amazon-s3 amazon-web-services

5
推荐指数
2
解决办法
4186
查看次数