简体   繁体   English

S3 Lambda API错误-Access-Control-Allow-Origin不允许

[英]S3 Lambda API Error - not allowed by Access-Control-Allow-Origin

I have a Lambda Function that returns text from a script, I set up an API to get the data using a GET Request and deployed it for testing. 我有一个Lambda函数,可以从脚本返回文本,我设置了一个API以使用GET Request获取数据,并将其部署进行测试。

When I click on test API I get my results successfully. 当我单击测试API时,我会成功获得结果。

I have this code in my index.html 我的index.html中有此代码

function myFunction() {
                var xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function(e) {
                    console.log(e)
                    if (this.readyState == 4 && this.status == 200) {
                    document.getElementById("my-demo").innerHTML = this.responseText;
                    }
                };
                xhttp.open("GET", "[link to get API]", true);
                xhttp.send();

            }

On my S3, the file is public and my CORS is 在我的S3上,该文件是公开的,而我的CORS是

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedOrigin>http://*</AllowedOrigin>
    <AllowedOrigin>https://*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

The webpage loads, although I keep getting these errors 虽然我不断收到这些错误,但网页已加载

[Error] Origin https://s3.amazonaws.com is not allowed by Access-Control-Allow-Origin.
[Error] Failed to load resource: Origin https://s3.amazonaws.com is not allowed by Access-Control-Allow-Origin. (myServerlessWebpage, line 0)
[Error] XMLHttpRequest cannot load [myapiwebsite] due to access control checks.

I've encountered the same issue and find out that I can "fix" this behavior by proxying my requests through my server. 我遇到了同样的问题,发现可以通过服务器代理请求来“修复”此问题。

So in front I make request to express server and then on the server side make request to AWS API. 所以,在前面我提出请求, 表示服务器,然后在服务器端也求AWS API。
For example with node-fetch module for requests: 例如,带有用于请求的节点获取模块:

//front
var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function(e) {
                console.log(e)
                if (this.readyState == 4 && this.status == 200) {
                document.getElementById("my-demo").innerHTML = this.responseText;
                }
            };
            xhttp.open("GET", "[link to get your server]", true);
            xhttp.send();


// backend 
var fetch = require('node-fetch'):
// your server code here....

app.get('/[link to get your server]', async (request, response) => {
  // parse data from request and 
  var apiRequest = await fetch([link to get API]);
  var result = await fetch.json();
  // send result from AWS api to front.
  res.send(result);
})

So, the solution is to proxy your AWS API request through your server. 因此,解决方案是通过服务器代理您的AWS API请求。

Also, you can find this article extremely helpful and maybe find another solution for your problem. 另外,您会发现本文非常有帮助,并且可能会为您的问题找到另一种解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Amazon S3:访问控制允许来源 - Amazon S3: Access-Control-Allow-Origin S3 - 访问控制允许来源 Header - S3 - Access-Control-Allow-Origin Header API网关POST请求返回“ Access-Control-Allow-Origin不允许的错误http:// localhost:3000” - API Gateway POST request returns "error http://localhost:3000 is not allowed by Access-Control-Allow-Origin” 请求的资源上不存在“Access-Control-Allow-Origin”header(AWS、API 网关、S3、CORS) - No 'Access-Control-Allow-Origin' header is present on the requested resource (AWS, API Gateway, S3, CORS) 没有“访问控制允许来源”AWS Lambda - No 'Access-Control-Allow-Origin' AWS Lambda 在 JavaScript 中从 S3 Bucket 中读取 JSON 时没有“Access-Control-Allow-Origin”错误 - No 'Access-Control-Allow-Origin' error when reading in JSON from S3 Bucket in JavaScript AWS S3 存储桶 CORS 策略错误:请求的资源上不存在“Access-Control-Allow-Origin”标头 - AWS S3 Bucket CORS Policy Error: No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源上不存在“Access-Control-Allow-Origin”header。 使用 aws lambda 将图像上传到 s3 - no 'Access-Control-Allow-Origin' header is present on the requested resource. while uploading image to s3 using aws lambda 使用 AWS API Gateway 时出现错误“访问控制允许来源” - Error 'Access-Control-Allow-Origin' using AWS API Gateway XMLHttpRequest无法加载https://s3.amazonaws.com/。 Access-Control-Allow-Origin不允许起源 - XMLHttpRequest cannot load https://s3.amazonaws.com/. Origin is not allowed by Access-Control-Allow-Origin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM