Bad*_*l S 7 file-upload amazon-s3 amazon-web-services reactjs
您好,我正在尝试从我的 React 前端上传图像,但它无法读取 axios 中的文件,并且图像未上传。
这是我的代码。
<form onSubmit={Upload}>
<input
type='file'
onChange={(e) => {
generate(e.target.files);
}}
/>
<button type='submit'>Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)
const generate = async (data) => {
const payload = {
entity: 'test',
userId: '200',
documentFileName: data[0].name,
action: 'putObject',
};
console.log('DAATA', data[0].name);
await api
.generatePreSignedUrl(payload, token)
.then((res) => {
setUploadUrl(res.data.data);
setImageData(data);
})
.catch((err) => {
console.log('Error', err);
});
};
const Upload = async (e) => {
e.preventDefault();
console.log('IMAGEdata', imageData);
console.log('URL', uploadUrl);
let file = imageData[0];
console.log('THIS IS FILE', file);
console.log('FILENAME', file.name);
console.log('FIELETYPE', file.type);
var options = {
headers: {
'Content-Type': file.type,
},
};
await axios
.put(uploadUrl, file, options)
.then((res) => {
console.log('Response from s3');
console.log(res);
})
.catch((error) => {
alert('ERROR ' + JSON.stringify(error));
});
};
Run Code Online (Sandbox Code Playgroud)
这里 imageUrl 是我用来上传文件的签名 URL。我成功地调用了 put 请求来上传文件,但图像或文件未被读取和上传。
这是我尝试查看上传的文件时的响应。
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Code>NoSuchKey</Code>
<Message>The specified key does not exist.</Message>
<Key>documents/ama/test/200/displaypicture.png</Key>
<RequestId>R7M63AHQKDNC6NH5</RequestId>
<HostId>3u8Jb8Ev4S1s/ODicL3Py4hvrvwKKOs/5zGxUEDYORfN1+U37Zx8PfYvIcGWUIAfeCqHpWqXd8o=</HostId>
</Error>
Run Code Online (Sandbox Code Playgroud)
请参阅 AWS SDK for JavaScript 开发人员指南版本 3。其中有示例演示如何将照片上传到 Amazon S3 存储桶。
| 归档时间: |
|
| 查看次数: |
28540 次 |
| 最近记录: |