使用带有IAM角色的bo​​to3连接到Amazon S3

Mar*_*ski 5 python amazon-s3 amazon-iam boto3

我试图从python boto转移到较新的boto3模块,用于操作Amazon S3上的文件.

我还需要使用Amazon IAM角色,就像我使用旧的boto模块一样.我不确定如何在服务器上设置IAM角色,但我所要做的就是:

s3_conn = S3Connection()
Run Code Online (Sandbox Code Playgroud)

我可以访问服务器可以访问的所有存储桶.

这在boto3中似乎有所不同:

s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
     print(bucket.name)
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/boto3/resources/collection.py", line 83, in __iter__
  for page in self.pages():
File "/usr/local/lib/python2.7/dist-packages/boto3/resources/collection.py", line 161, in pages
  pages = [getattr(client, self._py_operation_name)(**params)]
File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 310, in _api_call
  return self._make_api_call(operation_name, kwargs)
File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 407, in _make_api_call
  raise ClientError(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (AccessDenied) when calling the ListBuckets operation: Access Denied
Run Code Online (Sandbox Code Playgroud)

我正在浏览boto3文档,但我不确定我需要做什么来设置IAM角色:

http://boto3.readthedocs.org/en/latest/

Jor*_*ips 7

您需要s3:ListBucket在策略中获得所有相关存储区的权限.您可以在此处找到S3策略条件的完整列表.