S3存储桶策略:在公共存储桶中,将子文件夹设为私有

10

我有一个桶,里面装着大部分需要公开的内容。然而,有一个文件夹(也就是"前缀")只能由经过身份验证的IAM用户访问。

{
  "Statement": [
    {
      "Sid": "AllowIAMUser",
      "Action": [
        "s3:GetObject"
      ],
      "Effect": "Allow",
      "Resource": "arn:aws:s3:::bucket/prefix1/prefix2/private/*",
      "Principal": {
        "AWS": [
          "arn:aws:iam::123456789012:user/bobbydroptables"
        ]
      }
    },
    {
      "Sid": "AllowAccessToAllExceptPrivate",
      "Action": [
        "s3:GetObject",
        "s3:GetObjectVersion"
      ],
      "Effect": "Allow",
      "Resource": "arn:aws:s3:::bucket/*",
      "Condition": {
        "StringNotLike": {
          "s3:prefix": "prefix1/prefix2/private/"
        }
      },
      "Principal": {
        "AWS": [
          "*"
        ]
      }
    }
  ]
}

当我尝试保存此策略时,AWS会返回以下错误消息:

Conditions do not apply to combination of actions and resources in statement -
  Condition "s3:prefix"
  and action "s3:GetObject"
  in statement "AllowAccessToAllExceptPrivate"

显然,这个错误只适用于第二个语句。使用"s3:prefix"条件和"s3:GetObject"操作不可能吗?

是否可以将公共存储桶的一部分仅限于经过身份验证的用户才能访问?

如果有关系,该存储桶将仅通过api进行只读访问。

这个问题类似于Amazon S3 bucket policy for public restrictions only,但我正在尝试采用不同的方法解决问题。

1个回答

26

经过深入研究AWS文档以及在策略编辑器中反复尝试,我认为我已经找到了一个适当的解决方案。

显然,AWS提供了一个名为NotResource的选项(当前在策略生成器中未找到)。

The NotResource element lets you grant or deny access to all but a few
of your resources, by allowing you to specify only those resources to
which your policy should not be applied.

有了这个,我甚至不需要玩弄条件。这意味着以下语句可以在存储桶策略中起作用:

{
  "Sid": "AllowAccessToAllExceptPrivate",
  "Action": [
    "s3:GetObject",
    "s3:GetObjectVersion"
  ],
  "Effect": "Allow",
  "NotResource": [
    "arn:aws:s3:::bucket/prefix1/prefix2/private/*",
    "arn:aws:s3:::bucket/prefix1/prefix2/private"
  ],
  "Principal": {
    "AWS": [
      "*"
    ]
  }
}

它说我必须等一天。 :) 无论如何,我通常都会这样做,以防有人有更好的答案。 - SunSparc
小修正,您需要在“NotResource”:[...]属性后面加上逗号才能使JSON有效。 - Austin Floyd
2
如果有人想知道,这也可以与条件结合使用。谢谢! - Jeff Kilbride
1
非常完美地工作。谢谢! - benface

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接