gpt4 book ai didi

reactjs - Axios AWS S3 放置对象 : Error: Network Error

转载 作者:行者123 更新时间:2023-12-05 06:33:23 25 4
gpt4 key购买 nike

我可以从 AWS 成功获取签名的 url,但我在尝试放入文件时仍然遇到错误。

//服务器.js

server.get('/...', (req, res) => {
const s3 = new AWS.S3();
const fileName = req.query.filename;
const fileType = req.query.filetype;

const s3Params = {
Bucket: '...',
Key: fileName,
Expires: 60,
ContentType: fileType,
ACL: 'public-read'
};

s3.getSignedUrl('putObject', s3Params, (err, signedUrl) => {
if (err) {
console.log('s3 signed url err', err);
return res.end();
}

const returnData = {
signedUrl,
url: `https://....s3.amazonaws.com/${fileName}`
};

res.send(JSON.stringify(returnData));
});

});

// react

class ImageUploader extends React.Component {

onDrop = files => {
// const file = files[0];
// const fileName = file.name;

var file = files[0];

axios.get('/...', {
params: {
filename: 'home/banner-images/' + file.name,
filetype: file.type
}
})
.then(result => {

const options = {
headers: {
'Content-Type': file.type,
'Access-Control-Allow-Origin': '*',
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
"Access-Control-Allow-Headers": "X-Requested-With, content-type, Authorization"
}
};

const instance = axios.create();
return instance.put(result.data.signedUrl, file, options);
})
.then(result => {
console.log('then then result', result);
})
.catch(err => {
console.log('s3 put err', err);
});
};

render() {
return (
<div>
<Dropzone onDrop={this.onDrop}>
<div>
Try dropping a file here, or click to select a file to upload.
</div>
</Dropzone>
</div>
);
}
}

我的 AWS 政策:

{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "AllowPublicRead",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": [
"s3:PutObject",
"s3:PutObjectAcl",
"s3:GetObject"
],
"Resource": "arn:aws:s3:::.../*"
}
]
}



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

get请求成功获取signedUrl,但是put请求返回错误:

Error: Network Error
at createError (createError.js:16)
at XMLHttpRequest.handleError (xhr.js:87)

这是建立在 Next.js 之上的,这给我带来了很多安装 Cognito 仅用于客户端上传的问题。

最佳答案

我在我的 React 应用程序中也遇到了同样的问题。然后我使用“fetch”而不是“axios”,它工作得很好。看起来很奇怪,但在我的情况下有效。

export const uploadImageS3 = (url, file) => { return fetch(
url,
{
'body': file,
'headers': {
'Accept': 'application/json'
},
'method': 'PUT'
}).then(response => {
// Make sure the response was valid
if (response.status >= 200 && response.status < 300) {
return response;
}
const error = new Error(response.statusText);
error.response = response;
throw error;
});
};

关于reactjs - Axios AWS S3 放置对象 : Error: Network Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50783649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com