如何从来自不同区域的多个存储桶支持的 S3 提供静态 Web 内容

How to serve static web content from S3 backed by multiple buckets from different regions

我正在尝试从 S3 存储桶提供静态 Web 内容(HTML、CSS 和 JS 文件)。我知道我可以转到存储桶的属性选项卡并从 Static website hosting 框中选择项目 Use this bucket to host a website。我确信这一步仍将是我正在寻找的解决方案的一部分,但不会是全部。

这是我要实现的目标:

将相同的内容部署到多个区域并根据可用性 and/or 延迟,向客户端提供服务。

至于API网关,我知道怎么做。我应该在所有区域创建相同的 API 网关(连同底层 lambda 函数)和自定义域名。然后在 Route 53(CNAME 类型)上创建相同的域并选择延迟作为路由策略。还可以为记录集设置健康检查,以便定期检查 API 网关和 lambda 函数的可用性。

现在我想对 S3 存储桶和我的静态内容执行相同的操作。即我想将相同的内容部署到不同的区域,并以某种方式使 Route 53 将请求路由到 最近的可用 存储桶。以前,我使用的是 CloudFront,但在我看来,在此设置中,我只能引入一个存储桶。

有谁知道如何从多个存储桶中提供我的静态内容?如果您要推荐 CouldFront,请告诉我您打算如何使用多个存储桶。

您可以生成 a certificate, setup a CloudFront distribution 以从您的存储桶中获取内容,然后使用 Route53 将您的域指向您的分配。您可以获得免费的 https,您还可以添加多个 S3 存储桶作为您的分发源。

来自 AWS 文档:

After you configure CloudFront to deliver your content, here's what happens when users request your objects:

1. A user accesses your website or application and requests one or more objects, such as an image file and an HTML file.

2. DNS routes the request to the CloudFront edge location that can best serve the request—typically the nearest CloudFront edge location in terms of latency—and routes the request to that edge location.

3. In the edge location, CloudFront checks its cache for the requested files. If the files are in the cache, CloudFront returns them to the user. If the files are not in the cache, it does the following:

3a. CloudFront compares the request with the specifications in your distribution and forwards the request for the files to the applicable origin server for the corresponding file type—for example, to your Amazon S3 bucket for image files and to your HTTP server for the HTML files.

3b. The origin servers send the files back to the CloudFront edge location.

3c. As soon as the first byte arrives from the origin, CloudFront begins to forward the files to the user. CloudFront also adds the files to the cache in the edge location for the next time someone requests those files.

P.D。请记住,这仅适用于静态内容!

这可以通过 CloudFront 使用 Lambda@Edge 根据 Route 53 的回答更改来源。

请参阅此博客以获取执行此操作的示例 Lambda@Edge 代码 - https://aws.amazon.com/blogs/apn/using-amazon-cloudfront-with-multi-region-amazon-s3-origins/