gpt4 book ai didi

python-2.7 - 使用 boto3 在没有 GET 或 PUT 的情况下验证 S3 凭据

转载 作者:行者123 更新时间:2023-12-04 13:36:41 26 4
gpt4 key购买 nike

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

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

boto3 1.4.7。
python 2.7.13。

我们有自动化和编排来自动创建存储桶,我想包括一个验证用户访问 key 和 secret 的部分。自从我创建它以来,我知道此时该存储桶存在。桶是空的。

我想验证用户无需执行 PUT 操作即可访问。

谢谢你的帮助。

* 更新 *

我最终用一个 s3.Client 对象来做这个:

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

由于桶是空的,这是一种轻量级的操作,并且在大多数情况下是单衬。 (包裹在 try 块中)

最佳答案

是的,您可以使用 IAM policy simulation为了那个原因。下面是一个例子:

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 .

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

关于python-2.7 - 使用 boto3 在没有 GET 或 PUT 的情况下验证 S3 凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47056628/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com