boto3 尝试列出存储桶时出错

时间:2021-07-03 02:57:44

标签: amazon-web-services amazon-s3 openstack devstack

我正在使用

>>> s3 = session.client(service_name='s3',
... aws_access_key_id='access_key_id_goes_here',
... aws_secret_access_key='secret_key_goes_here',
... endpoint_url='endpoint_url_goes_here')
>>> s3.list_buckets() 

列出我现有的存储桶,但收到错误 botocore.exceptions.ClientError: An error occurred () when calling the ListBuckets operation: 不知道如何处理

1 个答案:

答案 0 :(得分:2)

你在使用 boto3 吗?

这是一些示例代码。 boto有两种使用方式:

  • 映射到 AWS API 调用的“客户端”方法,或
  • 更加 Pythonic 的 'resource' 方法

boto3 会自动从配置文件中检索您的用户凭据,因此无需在代码中放置凭据。您可以使用 AWS CLI aws configure 命令创建配置文件。

import boto3

# Using the 'client' method
s3_client = boto3.client('s3')

response = s3_client.list_buckets()

for bucket in response['Buckets']:
    print(bucket['Name'])

# Or, using the 'resource' method
s3_resource = boto3.resource('s3')

for bucket in s3_resource.buckets.all():
    print(bucket.name)

如果您使用的是与 S3 兼容的服务,则可以向 endpoint_urlclient() 调用添加 resource() 参数。

相关问题