获取AWS S3中所有存储桶的名称和计数

时间:2016-08-23 14:09:30

标签: python python-3.x amazon-web-services amazon-s3 boto3

我是Boto3和AWS API的新手,我想获得桶的列表' S3中的名称和可用桶的数量。

感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

要获取帐户中的所有存储分区,请执行以下操作:

import boto3

s3 = boto3.resource('s3')
bucket_list = [bucket.name for bucket in s3.buckets.all()]
print len(bucket_list)
print bucket_list

答案 1 :(得分:0)

此脚本将帮助您列出所有存储桶名称并获取计数。

import boto 
from boto.s3.connection import OrdinaryCallingFormat

conn = boto.connect_s3(calling_format=OrdinaryCallingFormat())

count = 0

print ("Bucket names: ")

for bucket in conn.get_all_buckets():
    print (bucket.name)
    count = count + 1

print ("Total count of S3 bucket is ", count)

注意:如果尚未在.boto文件中指定aws键,请在脚本中指定aws键

希望它有所帮助!!