I am using reactjs
and axios
I have this error when fetching the png from S3 to the local environment.
Access to fetch at 'https://xxx-xxx-xxx.s3.ap-northeast-1.amazonaws.com/0.png' from origin 'http://localhost:8021' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
I try to send header
let headers = new Headers();
headers.append('Access-Control-Allow-Origin','*');
headers.append('Access-Control-Allow-Credentials', 'true');
fetch(fileUrl,{headers: headers}).then(function(response){
})
and S3 has this CORS setting.
[
{
"AllowedHeaders": [
"*"
],
"AllowedMethods": [
"GET",
"POST",
"PUT"
],
"AllowedOrigins": [
"*"
],
"ExposeHeaders": [
"ETag"
],
"MaxAgeSeconds": 3000
}
]
Somehow my header is not sent correctly or am I wrong anywhere else?
Another trial
I install npm install http-proxy-middleware
and make src/setupProxy.js
const { createProxyMiddleware } = require("http-proxy-middleware");
console.log("setup is called");
module.exports = function (app) {
app.use(
"/",
createProxyMiddleware({
target: "https://xxx-xxx-xxx.s3.ap-northeast-1.amazonaws.com/0.png",
changeOrigin: true,
})
);
};
then server start with webpack --mode development --watch
However nothing changes, even console.log("setup is called");
is not called
How can I solve this?