使用 boto3 验证 S3 凭证,无需 GET 或 PUT

2024-01-19

有没有一种方法可以验证给定的一组 S3 凭据是否可以访问特定存储桶,而无需执行某种显式的 PUT 或 GET?

实例化 s3.Client、s3.Resource 或 s3.Bucket 对象似乎根本不验证凭据,更不用说存储桶访问了。

博托3 1.4.7。 蟒蛇2.7.13。

我们有自动化和编排功能,可以自动创建存储桶,我想包括一个验证用户访问密钥和秘密的部分。自从创建存储桶以来,我就知道该存储桶存在。桶是空的。

我想验证用户是否有权访问而无需执行 PUT 操作。

谢谢你的帮助。

* 更新 *

我最终使用 s3.Client 对象执行此操作:

objects = client.list_objects(Bucket=cfg['bucket'])

由于桶是空的,这是一项轻量级操作,并且大部分情况下都是单衬管。 (包裹在 try 块中)


是的,您可以使用IAM 策略模拟 http://boto3.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.simulate_principal_policy为了那个原因。这是一个例子:

import boto3

iam = boto3.client('iam')
sts = boto3.client('sts')

# Get the arn represented by the currently configured credentials
arn = sts.get_caller_identity()['Arn']

# Create an arn representing the objects in a bucket
bucket_objects_arn = 'arn:aws:s3:::%s/*' % 'my-test-bucket'

# Run the policy simulation for the basic s3 operations
results = iam.simulate_principal_policy(
    PolicySourceArn=arn,
    ResourceArns=[bucket_objects_arn],
    ActionNames=['s3:PutObject', 's3:GetObject', 's3:DeleteObject']
)
for result in results['EvaluationResults']:
    print("%s - %s" % (result['EvalActionName'], result['EvalDecision']))

您可以找到所有 s3 操作here http://docs.aws.amazon.com/AmazonS3/latest/dev/using-with-s3-actions.html.

需要注意的是,IAM 最终是一致的,因此,如果您动态创建用户,您可能仍然需要等待一段时间才能传播更改。

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

使用 boto3 验证 S3 凭证,无需 GET 或 PUT 的相关文章

随机推荐