简体   繁体   English

如何打印可用的桶?

[英]How to print buckets available?

I'm trying to print available buckets on AWS but failed.我正在尝试在 AWS 上打印可用的存储桶但失败了。 I tried multiple tutorials online and i would get cannot locate credentials and 's3.ServiceResource' object has no attribute errors.我在线尝试了多个教程,但我cannot locate credentials并且's3.ServiceResource' object has no attribute错误。

s3 = boto3.resource('s3',aws_access_key_id = "Random",aws_secret_access_key = "Secret" )
client = s3.client('s3')
response = client.list_buckets()
print(response)

Can you try:你能试一下吗:

for bucket in s3.buckets.all():

   print(bucket.name)

The problem is probably because you are defining s3 as a resource:问题可能是因为您将s3定义为资源:

s3 = boto3.resource('s3')

But then you are trying to use it as a client:但是随后您尝试将其用作客户端:

client = s3.client('s3')

That won't work.那行不通。 If you want a client, create one with:如果您想要一个客户,请创建一个:

s3_client = boto3.client('s3')

Or, you can extract a client from the resource:或者,您可以从资源中提取客户端

s3_resource = boto3.resource('s3')
response = s3_resource.meta.client.list_buckets()

Or, sticking with the resource, you can use:或者,坚持使用资源,您可以使用:

s3_resource = boto3.resource('s3')
for bucket in s3_resource.buckets.all():
  # Do something with bucket

Confused?使困惑? Try to stick with one method.尝试坚持一种方法。 Client directly matches the underlying API calls made to S3 and is the same as all other languages.客户端直接匹配对 S3 进行的底层 API 调用,并且与所有其他语言相同。 Resource is a more "Pythonic" way of accessing resources. Resource是一种更“Pythonic”的访问资源的方式。 The calls get translated to client API calls.调用被转换为客户端 API 调用。 Resources can be a little more challenging when figuring out required permissions, since there isn't a one-to-one mapping to actual API call.在确定所需的权限时,资源可能更具挑战性,因为没有与实际 API 调用的一对一映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM