AWS S3存储桶策略 - 如何仅允许来自我的网站的访问?

12

我有一个纸clip文本文件附件(在Rails中)。

我的存储桶策略是:

{
    "Version": "2008-10-17",
    "Id": "Policy123",
    "Statement": [
        {
            "Sid": "Stmt123",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": [
                "s3:GetObjectVersion",
                "s3:DeleteObject",
                "s3:DeleteObjectVersion",
                "s3:GetObject",
                "s3:PutObject"
            ],
            "Resource": "arn:aws:s3:::my_bucket/*"
        }
    ]
}

我希望限制只有在请求来自我的网站时才允许访问这些操作。是否只需将其更新为:"Principal": {"AWS": "mywebsite.com"}?

2个回答

16

您可以在S3文档中查看一些示例。

为了限制来自您网站的访问,您可以在引用者(Referrer)上使用条件:

{
  "Version":"2008-10-17",
  "Id":"http referer policy example",
  "Statement":[
    {
      "Sid":"Allow get requests referred by www.mysite.com and mysite.com",
      "Effect":"Allow",
      "Principal":"*",
      "Action":"s3:GetObject",
      "Resource":"arn:aws:s3:::example-bucket/*",
      "Condition":{
        "StringLike":{
          "aws:Referer":[
            " http://www.mysite.com/*",
            " http://mysite.com/*"
          ]
        }
      }
    }
  ]
}

这使我无法打开我的网站上的文件,非常遗憾。 - rigyt
请检查您的拼写是否正确:aws:Referer - 与HTTP referer字段相同。还要检查您的站点URL是否以正确的格式发送。删除条件,并检查图像的标头。 - Guy
我该如何检查我的网站URL发送的格式?每当我放置referrer时,我无法从网站打开paperclip附件 :-( - rigyt
使用例如Chrome开发者工具:https://developers.google.com/chrome-developer-tools/ - Guy
这里难道没有一个红旗吗?直接访问网站的流量不会看到任何资产加载(因为此时引用者不会设置为该特定网站)。 - Hassan Baig

6

存储桶策略:

{
    "Version": "2012-10-17",
    "Id": "http referer policy example",
    "Statement": [
        {
            "Sid": "Allow get requests originating from www.example.com and example.com.",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::00000000:user/example-user" // IAM User ARN
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::bucket-example/*", // bucket ARN
            "Condition": {
                "StringLike": {
                    "aws:Referer": [
                        "http://example.com/*" // Website link
                    ]
                }
            }
        }
    ]
}

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