当我尝试在 Python 中使用 BOTO 访问某些文件时,我在 S3 存储桶上遇到权限问题。这是存储桶策略:
{
"Version": "2008-10-17",
"Id": "Policy1407346649831",
"Statement": [
{
"Sid": "Stmt1407346646598",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::029030651757:user/my_iam_user"
},
"Action": "s3:*",
"Resource": ["arn:aws:s3:::my_bucket/*",
"arn:aws:s3:::my_bucket"]
},
{
"Sid": "2",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity EFUS443HMBYF"
},
"Action": "s3:GetObject",
"Resource": ["arn:aws:s3:::my_bucket/*",
"arn:aws:s3:::my_bucket"]
},
{
"Sid": "3",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity EFUS443HMBYF"
},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::my_bucket/*"
}
]
}
我有 3 个陈述。第一个是授权用户 my_iam_user
访问存储桶 my_bucket
,第二个是授权 Cloudfront 分配读取存储桶,最后一个是授权一个Cloudfront 分发以写入存储桶。
现在我必须在存储桶 profile_pictures/15/file1.jpg
和 profile_pictures/15/file2.jpg
中添加文件。第一个是使用签名的 url 和 CloudFront 创建的,第二个是使用 Boto 放在 S3 上的。现在我尝试使用 Boto 访问这些文件。这是我的代码:
import boto
from boto.s3.key import Key
s3 = boto.connect_s3(
aws_access_key_id="access_key_of_my_iam_user",
aws_secret_access_key="secret_key_of_my_iam_user"
)
bucket = s3.get_bucket("my_bucket", validate=False)
k1 = Key(bucket)
k1.key = "profile_pictures/15/file1.jpg"
k1.get_contents_as_string()
k2 = Key(bucket)
k2.key = "profile_pictures/15/file2.jpg"
k2.get_contents_as_string()
问题是访问file1返回错误:
S3ResponseError: S3ResponseError: 403 Forbidden
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>AccessDenied</Code><Message>Access Denied</Message><RequestId>8C5DE910C7B18F9E</RequestId><HostId>XiKU5Q+B0Wme3GpUNmUoD9KpUN63T3bFu/rAb/wh3rhDMkykoRsdQIFgyIp8zfAwMR1apbqUEFY=</HostId></Error>
而第二个是成功的。有什么问题吗?
注意:运行代码的客户端上的时间很好。
最佳答案
即使 IAM 用户已被第一个策略授予对存储桶的完全访问权限,他们仍然不会自动获得对 file1
的访问权限,因为他们不是该对象的所有者(Cloudfront 是)并且他们没有被授予对该对象的显式访问权限。此外,据推测,IAM 用户也不是存储桶的所有者。
如果你看Example 1 on this page您将看到几乎相同的情况,并进一步解释了如何使用对象上下文来确定请求是被批准还是被拒绝。
关于python - S3 响应错误 : 403 Forbidden using Boto,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31568445/