amazon-web-services - 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:
不确定如何继续
解决方案
你用的是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()
推荐阅读
- cmake - 如何链接重复的目标名称
- vue.js - Vue 提供的排序库
- java - 如何正确调用 Item2.calculateUnitTotal() 以便我可以将金额添加到总变量
- scala - 如何使用 flatMapConcat?
- python-3.x - Websocket Autobahn Python客户端:如何使用服务器和客户端证书连接到服务器?
- java - 如何在Android中使用数组存储手动生成的编辑文本值
- php - 在标签中的图像上添加有效的检查图像或图标
- r - 如何弃用与 R 包关联的数据?
- docker - 以 Docker 模式运行的 Minikube 在启动仪表板时返回 503
- node.js - Angular 7 浏览器未反映代码中所做的更改