首页 > 解决方案 > boto3 尝试列出存储桶时出错

问题描述

我在用着

>>> 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:不确定如何继续

标签: amazon-web-servicesamazon-s3openstackdevstack

解决方案


你用的是boto3吗?

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

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

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 兼容的服务,则可以向and调用添加endpoint_url参数。client()resource()


推荐阅读