首先,我意识到这些问题:
但是解决方案不适用于我。
我创建了一个角色“ sample_role
”,并附加了AmazonS3FullAccess
-policy并将角色分配给了ec2-instance。
我的bucket-policy如下:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::My-Account-ID:role/sample_role"
},
"Action": "s3:*",
"Resource": "arn:aws:s3:::my_bucket/*"
}
]
}
在我的ec2-instance上,从命令行(aws s3 ls
)和python脚本列出我的存储桶都可以正常工作。但是当我尝试上传文件 test.txt
到我的存储桶时,我得到了AccessDenied
:
import boto3
s3_client = boto3.client('s3')
s3_resource = boto3.resource('s3')
bucket = s3_resource.Bucket('my_bucket')
with open('test.txt', "rb") as f:
s3_client.upload_fileobj(f, bucket.name, 'text.txt')
错误消息:
botocore.exceptions.ClientError: An error occurred (AccessDenied) when calling the PutObject operation: Access Denied
当我只是尝试列出存储桶中的对象]时也会发生同样的情况。命令行aws s3api list-objects --my_bucket
或python脚本:
import boto3 s3_resource = boto3.resource('s3') bucket = s3_resource.Bucket('my_bucket') for my_bucket_object in bucket.objects.all(): print(my_bucket_object)
错误消息:
公共访问权限”并在我的访问控制列表中启用公共访问权限时,它显然可以工作。但是我需要限制对指定角色的访问。botocore.exceptions.ClientError: An error occurred (AccessDenied) when calling the ListObjects operation: Access Denied
当我在我的存储桶设置中关闭“阻止all
我想念的是什么?感谢您的帮助!
首先,我意识到以下问题:授予EC2实例对S3存储桶的访问权限调用PutObject操作时,无法使用ec2实例中的IAM角色使用IAM-role访问s3存储桶...拒绝访问...]
看来您的要求是:
my_bucket
)当我在我的存储桶设置中关闭“禁止所有公共访问”并在我的访问控制列表中启用公共访问时,显然可以使用。
所以我发现了问题。我的ec2实例的credentails配置有未分配角色的dev用户帐户的访问密钥。
我通过运行aws sts get-caller-identity
发现,它返回实际使用的身份(例如IAM角色)。