[英]How to get a total count of S3 Buckets with Python Boto3
I'm attempting to get a list of total amount of S3 Buckets on a given AWS account.我正在尝试获取给定 AWS 账户上的 S3 存储桶总量列表。
Using boto3 and Python 2.7, I have done the following:使用 boto3 和 Python 2.7,我做了以下事情:
import boto3
s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
bucket_names = (bucket.name)
print bucket_names.count('\n')
However, this results in a 0 output for each line in bucket_names.但是,这会导致 bucket_names 中每一行的 output 为 0。 Essentially, I'm trying to get the 'wc -l' equivalent if I were to do this in a nix shell.
本质上,如果我要在 nix shell 中执行此操作,我正在尝试获得等效的“wc -l”。
You can use s3 client.您可以使用 s3 客户端。
import boto3
client = boto3.client('s3')
response = client.list_buckets()
print(len(response['Buckets']))
You can also obtain the number of buckets by using boto3 resource such a way,您也可以通过使用boto3资源这样的方式获取桶的数量,
import boto3
buckets = [bucket.name for bucket in boto3.resource('s3').buckets.all()]
print(len(buckets))
print('\n'.join(buckets))
where it will also prints the bucket names.它还将在其中打印存储桶名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.