我希望有人能够解释为什么我在控制台登录和 api 登录之间出现授权错误。我有一个可以访问特定 S3 存储桶的客户端,该存储桶对存储桶具有完全 CRUD 访问权限,并且能够通过控制台执行下载、创建、删除,但是当通过 API 访问时,如果他们提前知道密钥,他们也可以下载视图但在尝试读取存储桶中对象的密钥时收到未经授权的错误。
以下策略与用户帐户相关联,试图将该用户隔离到一个 S3 存储桶。
{
"Statement": [
{
"Sid": "AllowGroupToSeeBucketListAndAlsoAllowGetBucketLocationRequiredForListBucket",
"Effect": "Allow",
"Action": [
"s3:ListAllMyBuckets",
"s3:GetBucketLocation"
],
"Resource": "arn:aws:s3:::*",
"Condition": {}
},
{
"Sid": "AllowRootLevelListingOfCompanyBucket",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::mybucket",
"Condition": {
"StringEquals": {
"s3:prefix": "",
"s3:delimiter": "/"
}
}
},
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:GetObjectVersion",
"s3:PutObject",
"s3:GetObjectAcl",
"s3:GetObjectVersionAcl",
"s3:PutObjectAcl",
"s3:PutObjectVersionAcl",
"s3:DeleteObject",
"s3:DeleteObjectVersion"
],
"Resource": "arn:aws:s3:::mybucket/*",
"Condition": {}
}
]
}
以下是因未经授权而失败的 api 调用:
-=-=-=-
import boto, boto.s3
s3conn = boto.s3.connection.S3Connection(aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
is_secure=True,
debug=0)
# This line fails with "403 Forbidden" b/c it tries to read the keys in # the bucket:
#s3bucket = s3conn.get_bucket('mybucket')
# This line does NOT try to read the keys and therefore succeeds:
s3bucket = s3conn.get_bucket('mybucket', validate=False)
# This loop fails with "403 Forbidden" on the very first iteration:
#for k in s3bucket.list():
# print k
# This line works, but I had to use the webpage to learn the name of the # key first.
k = s3bucket.get_key('specificfilename.csv')
print k
# This line works.
print k.generate_url(expires_in=10) + '\n'