如何向 boto 中的联合用户授予 s3 存储桶的权限?

2024-05-19

尝试从文档中找出答案,但无法创建可以访问 s3 存储桶的联合用户

首先是进口

>>> from boto.s3.connection import S3Connection
>>> from boto.sts import STSConnection

然后在s3中创建一个bucket

>>> s3c = ('access key', 'secret key')  
>>> s3c.create_bucket('ranjith_new_bucket')

然后创建一个联合用户,如果我理解正确的话,这是创建临时凭证的一种方法

>>> sts = STSConnection('access key', 'secret key')  
>>> user = sts.get_federation_token('guest_user_1')

但是,当我尝试获取所有可访问存储桶的列表时,出现错误。

>>> s3c2 = S3Connection(user.credentials.access_key, user.credentials.secret_key)
>>> s3c2.get_all_buckets()

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ranjith/work/venv/local/lib/python2.7/site-packages/boto-2.16.0-py2.7.egg/boto/s3/connection.py", line 387, in get_all_buckets
    response.status, response.reason, body)
boto.exception.S3ResponseError: S3ResponseError: 403 Forbidden
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>InvalidAccessKeyId</Code><Message>The AWS Access Key Id you provided does not exist in our records.</Message><RequestId>154CDA2184E00650</RequestId><HostId>xxx</HostId><AWSAccessKeyId>xxx</AWSAccessKeyId></Error>

文档 http://docs.pythonboto.org/en/latest/ref/sts.html#boto.sts.STSConnection.get_federation_token说我必须提供一个策略字符串,但文档非常难以理解。我知道我必须在某处定义一个策略并在创建联合用户时使用它,但不确定在哪里或如何做到这一点。

我确实尝试过这个:

permission = {
  "Version": "2012-10-17",
  "Statement": [
    {
       "Effect": "Allow",
       "Action": ["sts:GetFederationToken"],
       "Resource": "*"
     },
    {
       "Effect": "Allow",
       "Action": ["s3:*"],
       "Resource": "arn:aws:s3:::mybucket/ranjith_new_bucket/*"
     }
   ]
 }

sts.get_federation_token('some_other_user', json.dumps(permission))

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ranjith/work/venv/local/lib/python2.7/site-packages/boto-2.16.0-py2.7.egg/boto/sts/connection.py", line 235, in get_federation_token
    FederationToken, verb='POST')
  File "/home/ranjith/work/venv/local/lib/python2.7/site-packages/boto-2.16.0-py2.7.egg/boto/connection.py", line 1138, in get_object
    raise self.ResponseError(response.status, response.reason, body)
boto.exception.BotoServerError: BotoServerError: 400 Bad Request
<ErrorResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
  <Error>
    <Type>Sender</Type>
    <Code>MalformedInput</Code>
  </Error>
  <RequestId>75734932-4ac1-11e3-a17b-e9e245983c07</RequestId>
</ErrorResponse>

这真的很愚蠢,因为我所做的就是放入策略的 json 转储中。我什至无法找到解决该问题的任何在线资源。

如果有人可以指导我,以便我最终创建一个可以访问的联合用户,那就太好了ranjith_new_bucket上面创建的。代码片段将不胜感激。


从此调用返回的 FederationToken 对象:

>>> user = sts.get_federation_token('guest_user_1')

有一个属性叫做credentials并且这个子对象包含一个access_key, a secret_key, and a session_token。所有这三个都需要在请求中发送。您目前只发送access_key and secret_key。我想如果你将代码更改为如下所示:

> s3c2 = S3Connection(user.credentials.access_key, user.credentials.secret_key, security_token=user.credentials.session_token)

你应该没事。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何向 boto 中的联合用户授予 s3 存储桶的权限? 的相关文章

随机推荐