2016-08-23 278 views
0

我是Boto3和AWS API的新手,我想獲取S3中存儲桶名稱和可用存儲桶的數量列表。獲取AWS S3中所有存儲桶的名稱和數量

任何幫助表示讚賞。

+0

所有剷鬥或只是你的桶? –

+0

所有的桶。我需要監視名稱總數和桶的數量。 –

+0

S3 API不支持列出其他帳戶的存儲桶,除非您擁有明確的權限。您可以通過DNS發現存儲桶名稱,但我不知道具體情況。 –

回答

2

要在帳戶中獲得所有桶:

import boto3 

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

感謝您提供整潔的解決方案。我設法以更復雜的方式獲得名單。這樣更有效率。謝謝! –

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) 

注:請指定腳本AWS鍵,如果你還沒有在.boto文件指定的

希望它可以幫助!

+0

感謝您的幫助,但我在Boto3中需要它,並列出桶的名稱。 –

相關問題