使用boto库,我可以避免在S3中对基本存储桶授予列表权限吗?

时间:2012-07-13 21:40:50

标签: python amazon-s3 amazon-web-services boto

我目前有一个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 /”)?

更新

为了让一切正常,我使用了以下政策:

{
  "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/*"
    ]
  }
  ]
}

您仍然必须使用validate=False方法get_bucket方法,但它允许在前缀中列出。

1 个答案:

答案 0 :(得分:5)

默认情况下,boto尝试通过对存储桶执行LIST操作来验证存在桶,请求零结果。如果您希望它跳过此验证步骤,请按以下方式调用它:

>>> import boto
>>> s3 = boto.connect_s3()
>>> bucket = s3.get_bucket('mybucket', validate=False)

这应该跳过LIST操作。