gpt4 book ai didi

amazon-s3 - 带有 CloudFlare 的 S3 存储桶 : Prevent Hotlinking

转载 作者:行者123 更新时间:2023-12-02 04:03:24 32 4
gpt4 key购买 nike

我正在使用名为 images.example.com 的 Amazon S3 存储桶,该存储桶使用如下 URL 成功通过 Cloudflare CDN 提供内容:

https://images.example.com/myfile.jpg

我想防止热链接到图像和其他内容,购买限制仅访问引用域:example.com 以及可能是我用作开发服务器的另一个域。

我尝试了一种存储桶策略,该策略既允许来自特定域,又拒绝来自任何非特定域的域:

 {
"Version": "2012-10-17",
"Id": "http referer policy example",
"Statement": [
{
"Sid": "Allow get requests referred by www.example.com",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::images.example.com/*",
"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:::images.example.com/*",
"Condition": {
"StringNotLike": {
"aws:Referer": [
"http://www.example.com/*",
"http://example.com/*"
]
}
}
}
]

}

测试一下。我在不同的服务器上上传了一个小网页:www.notExample.com,我尝试使用以下方法热链接图像:

 <img src="https://images.example.com/myfile.jpg">

但无论如何都会出现热链接图像。

我还尝试了以下 CORS 规则

 <CORSConfiguration>
<CORSRule>
<AllowedOrigin>http://www.example.com</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

这些都无法阻止盗链。我尝试使用存储桶策略和 CORS(其中之一或两者加两者)的组合来清除 CloudFlare 中的缓存文件,但没有任何效果。

这似乎是一件相对简单的事情。我做错了什么?

最佳答案

Cloudflare 是一个内容分发网络,可缓存更靠近最终用户的信息。

当用户通过 Cloudflare 访问内容时,该内容将从 Cloudflare 的缓存中提供。如果内容不在缓存中,Cloudflare 将检索内容,将其存储在缓存中并将其返回给原始请求。

因此,您的 Amazon S3 存储桶策略将无法与 Cloudflare 配合使用,因为页面请求要么来自 Cloudflare(不是生成 Referrer 的用户浏览器),要么直接从 Cloudflare 的缓存提供服务(因此请求永远不会到达 S3) .

您需要使用引荐来源网址规则来配置 Cloudflare,而不是 S3。

参见:What does enabling CloudFlare Hotlink Protection do?

一些替代方案:

  • 使用 Amazon CloudFront - 它能够 serve private content通过使用签名 URL - 您的应用程序可以通过在创建 HTML 页面时生成特殊 URL 来授予访问权限。
  • 直接从 Amazon S3 提供内容,可以使用 referer 规则。 Amazon S3 还支持 pre-signed URLs您的应用程序可以生成。

关于amazon-s3 - 带有 CloudFlare 的 S3 存储桶 : Prevent Hotlinking,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40819783/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com