【问题标题】:S3 bucket policy: In a Public Bucket, make a sub-folder privateS3 存储桶策略:在公共存储桶中,将子文件夹设为私有
【发布时间】:2013-12-06 20:20:31
【问题描述】:

我有一个桶,里面装满了大部分需要公开的内容。但是,只有一个经过身份验证的 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:GetObject”动作不能使用“s3:prefix”条件吗?

是否可以获取公共存储桶的一部分并使其仅供经过身份验证的用户访问?

如果重要,此存储桶将只能通过 api 以只读方式访问。

这个问题类似于Amazon S3 bucket policy for public restrictions only,除了我试图通过不同的方法来解决这个问题。

【问题讨论】:

    标签: amazon-web-services amazon-s3


    【解决方案1】:

    在深入研究 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": [
          "*"
        ]
      }
    }
    

    【讨论】:

    • 它说我要等一天。 :) 无论如何我通常都会这样做,以防有人有更好的答案。
    • 小修正,"NotResource":[...] 属性后面缺少一个逗号才能成为有效的 JSON
    • 如果有人想知道,这也可以与条件结合使用。谢谢!
    • 效果很好。谢谢!
    猜你喜欢
    • 2018-10-26
    • 2020-09-19
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 2011-09-10
    相关资源
    最近更新 更多