简体   繁体   English

如何使用 Python Boto3 获取 S3 存储桶的总数

[英]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.

相关问题 如何使用 python boto3 获取 AWS 的 S3(所有存储桶)中每个 object 的大小 - How to get the size of each object in S3 (all buckets) for an AWS using python boto3 如何使用 Python boto3 获取 s3 存储桶中超过 60 天的文件/对象的计数? - How to use Python boto3 to get count of files/object in s3 bucket older than 60 days? 使用Boto3访问S3中的嵌套存储桶 - Accessing nested buckets in S3 with Boto3 AWS S3 - 使用 python 的 boto3 获取特定文件夹内的文件夹计数 - AWS S3 - Get folder count inside a particular folder using python's boto3 使用 Boto3 获取特定 S3 文件夹中的对象数 - Get count of objects in a specific S3 folder using Boto3 我们可以使用boto3 Python在aws s3存储桶之间递归复制文件和文件夹吗? - Can we copy the files and folders recursively between aws s3 buckets using boto3 Python? 使用 Python boto3 对大量存储桶进行 S3 默认服务器端加密 - S3 Default server side encryption on large number of buckets using Python boto3 按需将 S3 存储桶存储到 Glacier 是否可以通过 boto3 API? - S3 buckets to Glacier on demand Is it possible from boto3 API? 使用boto3列出s3中启用版本的存储桶 - Listing version enabled buckets in s3 using boto3 如何使用 python boto3 重命名 s3 文件名 - How to rename the s3 file name by using python boto3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM