我正在尝试使用 IAM 角色连接到 s3 存储桶。
我的存储桶名称是:test-bucket
我创建了一个名为 的角色:我的角色_s3并赋予其完整的 s3 访问权限(即“AmazonS3FullAccess”)
当我尝试使用代码访问存储桶的名称时 -
import boto3
import os
os.environ["HTTP_PROXY"] = "http://<username>:<password>@191.18.0.25:9400"
os.environ["HTTPS_PROXY"] = "https://<username>:<password>@191.18.0.25:9400"
s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
print ("Bucket: %s" % bucket.name)
我能够连接并获取存储桶名称。但为此,boto3 在内部使用 Access 键。但我不想为此使用访问密钥并使用 IAM 角色。
因此,对于 IAM 角色,我正在尝试这段代码-
import boto3
import os
sts_client = boto3.client('sts')
os.environ["HTTP_PROXY"] = "http://<username>:<password>@191.18.0.25:9400"
os.environ["HTTPS_PROXY"] = "https://<username>:<password>@191.18.0.25:9400"
assumedRoleObject = sts_client.assume_role(
RoleArn="arn:aws:iam::824214495785:role/my_role_s3",
RoleSessionName="AssumeRoleSession1"
)
credentials = assumedRoleObject['Credentials']
s3_resource = boto3.resource(
's3',
aws_access_key_id = credentials['AccessKeyId'],
aws_secret_access_key = credentials['SecretAccessKey'],
aws_session_token = credentials['SessionToken'],
)
现在当方法 -sts_client.assume_role被调用时,我收到错误 -
botocore.exceptions.ClientError:调用 AssumeRole 操作时发生错误 (AccessDenied):用户:arn:aws:iam::824214495785:user/testuser 无权对资源执行:sts:AssumeRole:arn:aws:iam: :824214495785:角色/my_role_s3
Boto3 正在使用用户 -testuser。您能否告诉我需要在代码/帐户设置中进行哪些更改,以便不使用 testuser 并使用该角色,或者允许 testuser 访问 sts:Assumerole?