python – 使用boto库,我可以避免在S3中基础桶上授予列表权限吗?
作者:互联网
我目前有一个IAM角色,其策略如下:
{
"Version":"2008-10-17",
"Statement": [
{
"Effect":"Allow",
"Action":["s3:ListBucket"],
"Resource":[
"arn:aws:s3:::blah.example.com"
]
},
{
"Effect":"Allow",
"Action":["s3:GetObject", "s3:GetObjectAcl", "s3:ListBucket", "s3:PutObject", "s3:PutObjectAcl", "s3:DeleteObject"],
"Resource":[
"arn:aws:s3:::blah.example.com/prefix/"
]
}
]
}
Boto似乎要求在桶的根目录上存在ListBucket权限才能执行get_bucket调用.如果我删除Statement数组中的第一个哈希,get_bucket(‘blah.example.com’)将失败.这是错误文本:
*** S3ResponseError: S3ResponseError: 403 Forbidden <?xml version="1.0" encoding="UTF-8"?> <Error><Code>AccessDenied</Code><Message>Access Denied</Message><RequestId>xxxx</RequestId><HostId>yyyy</HostId></Error>
有没有办法在仍然使用boto时将存储桶的列表限制为某个前缀(例如“prefix /”)?
UPDATE
为了使一切正常,我使用了以下策略:
{
"Version":"2008-10-17",
"Statement": [
{
"Effect":"Allow",
"Action":["s3:ListBucket"],
"Resource":[
"arn:aws:s3:::blah.example.com"
],
"Condition":{
"StringLike":{
"s3:prefix":"prefix/*"
}
}
},
{
"Effect":"Allow",
"Action":["s3:GetObject", "s3:GetObjectAcl", "s3:PutObject", "s3:PutObjectAcl", "s3:DeleteObject"],
"Resource":[
"arn:aws:s3:::blah.example.com/prefix/*"
]
}
]
}
您仍然必须对get_bucket方法使用validate = False参数,但它允许在前缀中列出.
解决方法:
默认情况下,boto尝试通过对存储桶执行LIST操作来验证存在桶,请求零结果.如果您希望它跳过此验证步骤,只需将其调用如下:
>>> import boto
>>> s3 = boto.connect_s3()
>>> bucket = s3.get_bucket('mybucket', validate=False)
这应该跳过LIST操作.
标签:python,amazon-web-services,amazon-s3,boto 来源: https://codeday.me/bug/20190902/1787322.html