【发布时间】:2017-08-20 01:20:15
【问题描述】:
这是调整图像大小并通过 AWS CloudFront 提供图像的 CDN 架构:
如果在 S3 存储桶中找不到图像,它会发出 307 临时重定向(而不是 404)以通过 API 网关访问 Lambda。 Lambda 调整图像大小(基于 S3 存储桶中的原始图像)并将其上传到 S3 存储桶中。浏览器再次永久重定向到带有新生成图像的 S3 存储桶。
当我想通过 CloudFront 访问同一图像时,我收到 403 Forbidden 错误。它来自 S3 或 CloudFront。如状态所示,这可能与访问权限有关。
为什么将 CloudFront 添加到工作请求链中会导致 403 错误?
什么有效:
https://{bucket}.s3-website-{region}.amazonaws.com/100x100/image.jpg
HTTP/1.1 307 Temporary Redirect
x-amz-id-2: xxxx
x-amz-request-id: xxxx
Date: Sat, 19 Aug 2017 15:37:12 GMT
Location: https://{gateway}.execute-api.{region}.amazonaws.com/prod/resize?key=100x100/image.jpg
Content-Length: 0
Server: AmazonS3
https://{gateway}.execute-api.{region}.amazonaws.com/prod/resize?key=100x100/image.jpg
HTTP/1.1 301 Moved Permanently
Content-Type: application/json
Content-Length: 0
Connection: keep-alive
Date: Sat, 19 Aug 2017 15:37:16 GMT
x-amzn-RequestId: xxxx
location: http://{bucket}.s3-website-eu-west-1.amazonaws.com/100x100/image.jpg
X-Amzn-Trace-Id: xxxx
X-Cache: Miss from cloudfront
Via: 1.1 {distribution}.cloudfront.net (CloudFront)
X-Amz-Cf-Id: xxxx
http://{bucket}.s3-website-{region}.amazonaws.com/100x100/image.jpg
HTTP/1.1 200 OK
x-amz-id-2: xxxx
x-amz-request-id: xxxx
Date: Sat, 19 Aug 2017 15:37:18 GMT
Last-Modified: Sat, 19 Aug 2017 15:37:17 GMT
x-amz-version-id: null
ETag: xxxx
Content-Type: image/png
Content-Length: 20495
Server: AmazonS3
什么不起作用:
https://{distribution}.cloudfront.net/100x100/image.jpg
HTTP/1.1 403 Forbidden
Content-Type: application/xml
Transfer-Encoding: chunked
Connection: keep-alive
Date: Sat, 19 Aug 2017 15:38:24 GMT
Server: AmazonS3
X-Cache: Error from cloudfront
Via: 1.1 {distribution}.cloudfront.net (CloudFront)
X-Amz-Cf-Id: xxxx
我已将 S3 存储桶作为源添加到 CloudFront
【问题讨论】:
-
您是从 CloudFront 获得 S3 存储桶中所有对象的 403 还是仅获得一些(例如调整大小的图像)?
-
只针对需要调整大小的图片。生成它们后(例如,直接调用 https://{bucket}.s3-website-{region}.amazonaws.com/100x100/image.jpg),URL https://{distribution}.cloudfront.net /100x100/image.jpg 开始工作。
标签: amazon-web-services caching amazon-s3 aws-lambda amazon-cloudfront