简体   繁体   English

如何避免 API 在 AWS API 网关中创建的 CORS 错误?

[英]How to avoid the CORS error from an API created in AWS API Gateway?

I have created a React Application to upload a file to the S3 bucket.我创建了一个 React 应用程序来将文件上传到 S3 存储桶。 To do this, I have created a PUT API from the AWS API Gateway and the API is working well in the POSTMAN. The problem is, when I call the API from my React Application locally (http://localhost:3000), I'm getting a CORS error from the PUT request and a 403 error from the OPTIONS request (Preflight) .为此,我从 AWS API 网关创建了一个 PUT API,API 在 POSTMAN 中运行良好。问题是,当我从本地 React 应用程序 (http://localhost:3000) 调用 API 时,我我从 PUT 请求收到 CORS 错误,从 OPTIONS 请求(预检)收到 403 错误 How can I solve this?我该如何解决这个问题?

Here is the bucket policy这是存储桶策略

在此处输入图像描述

Here is the Bucket CORS Policy这是存储桶 CORS 策略

在此处输入图像描述

Here are the Enable CORS settings of the API in the API Gateway下面是API网关中API的Enable CORS设置

在此处输入图像描述

The Output Output

在此处输入图像描述

Your bucket policy does not allow putObject method.您的存储桶策略不允许使用putObject方法。 CORS can be seen when some unrelated errors happen. CORS 当一些不相关的错误发生时可以看到。 I encourage you to enable INFO logging for API Gateway and check what exactly happens.我鼓励您为 API 网关启用 INFO 日志记录并检查到底发生了什么。 Share the logs and it will be easier to tell.共享日志,这样会更容易分辨。

To enable INFO logging for API Gateway, go to API -> Settings -> add CloudWatch log role ARN which would have permission to write into cloudwatch logs.要为 API 网关启用 INFO 日志记录,go 到 API -> 设置 -> 添加 CloudWatch 日志角色 ARN,它有权写入 cloudwatch 日志。 Then enable full INFO logs.然后启用完整的信息日志。 You will find logs inside CloudWatch logs您会在 CloudWatch 日志中找到日志

在此处输入图像描述 在此处输入图像描述

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM