为S3存储桶设置防盗链


发布于

|

分类

,

不想每次搬家都带着一大堆附件,于是就把附件放在了S3上。

然后,肯定要设置一下防盗链这件事儿呀。

找了一些资料,但是还是弄不明白。

显式允许

顾名思义,“如果符合一些条件,那么就允许”,相当于数学里面的「当」

{
    "Version":"2012-10-17",
    "Id":"PreventHotLinking",
    "Statement":[
        {
            "Sid":"Allow get requests originated from www.example.com and example.com",
            "Effect":"Allow",
            "Principal":"*",
            "Action":"s3:GetObject",
            "Resource":"arn:aws:s3:::examplebucket/*",
            "Condition":{
                "StringLike":{
                    "aws:Referer":[
                        "http://www.example.com/*",
                        "http://example.com/*"
                    ]
                }
            }
        }
    ]
}

一般来说,这样做就行了。

显式拒绝

{
    "Version":"2012-10-17",
    "Id":"PreventHotLinking",
    "Statement":[
        {
            "Sid":"Explicit deny to ensure requests are allowed only from specific referer.",
            "Effect":"Deny",
            "Principal":"*",
            "Action":"s3:GetObject",
            "Resource":"arn:aws:s3:::examplebucket/*",
            "Condition":{
                "StringNotLike":{
                    "aws:Referer":[
                        "http://www.example.com/*",
                        "http://example.com/*"
                    ]
                }
            }
        }
    ]
}

由于AWS的判断逻辑,这个其实会比「显示允许」更加严格。

显式允许+显式拒绝

{
    "Version":"2012-10-17",
    "Id":"PreventHotLinking",
    "Statement":[
        {
            "Sid":"Allow get requests referred by www.mysite.com and mysite.com",
            "Effect":"Allow",
            "Principal":"*",
            "Action":"s3:GetObject",
            "Resource":"arn:aws:s3:::examplebucket/*",
            "Condition":{
                "StringLike":{
                    "aws:Referer":[
                        "http://www.example.com/*",
                        "http://example.com/*"
                    ]
                }
            }
        },
        {
            "Sid":"Explicit deny to ensure requests are allowed only from specific referer.",
            "Effect":"Deny",
            "Principal":"*",
            "Action":"s3:*",
            "Resource":"arn:aws:s3:::examplebucket/*",
            "Condition":{
                "StringNotLike":{
                    "aws:Referer":[
                        "http://www.example.com/*",
                        "http://example.com/*"
                    ]
                }
            }
        }
    ]
}

相当于数学里面的「当且仅当」。好吧,这个是最严格的了。

后记

在带有「显式拒绝」的逻辑下,我不会配置其他IAM账号的访问策略。

比如,我现在有一个IAM账号MyS3,搞到了AK和SK。然后我想用命令行来上传下载一些东西。在「显式允许」的逻辑下,默认OK。在「显式拒绝」的逻辑下,Fail。在「显式允许+显式拒绝」的逻辑下,Fail。

Emmm,很是痛苦啊~求教!

然后,除此之外,我们还可以加上其他限制,比如说只允许某些IP啊、只允许HTTPS啊什么的。


评论

  1. 刘 的头像

    从图片看 配置 显式允许和显式拒绝 两个一样 没啥不一样的
    我们这边有好多文件都存S3 想做防盗链
    由于文件地址直接暴露在web端 用户copy或者爬虫都能拿到
    有啥好的办法进行统一处理的吗
    给指教下 谢谢

    1. 小金鱼儿 的头像
      小金鱼儿

      我目前想到的只有加Referer。但是看到有些可以加Token,不知道怎么弄的。

回复 取消回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注