如何在两个 CloudFront 分配之间设置 CORS?

How to setup CORS between two CloudFront Distributions?

我有两个 CloudFront 分配:

JS 代码托管在 S3 和 First Cloudfront Distribution 上。 https://example.com/test.html 正在呼叫 https://api.com/api?<queryParameters>

但是我收到错误提示: Access to XMLHttpRequest at 'https://api.com/api?<queryParameters>' from origin 'https://example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

我尝试在 API CloudFront 行为的 Cache Based on Selected Request Headers 白名单中添加 Access-Control-Request-HeadersAccess-Control-Request-MethodOrigin。仍然没有运气

此问题已通过在 Lambda 函数响应中添加 Header 消息得到解决。 类似这样:

   response["headers"] = {
       'Content-Type': 'application/json', 
       'Access-Control-Allow-Origin': '*' 
   }

参考:https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html