Reputation: 53
I can successfully get the signed url from AWS but I still get an error trying to put a file in.
// Server.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>
);
}
}
My AWS Policies:
{
"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>
The get request is successfully fetching a signedUrl, but the put request returns the error:
Error: Network Error
at createError (createError.js:16)
at XMLHttpRequest.handleError (xhr.js:87)
This is built on top of Next.js, which was giving me a lot of problems install Cognito for client only uploads.
Upvotes: 3
Views: 4592
Reputation: 111
I was also facing the same issue in my react app. Then I used "fetch" instead of "axios", it worked perfectly. Seems weird but worked in my case.
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;
});
};
Upvotes: 1
Reputation: 1
Open the signedURL in browser and check the response
Probable errors:
Upvotes: 0
Reputation: 681
Not sure where the problem is here but this is what I've got working for React using a pre-signed AWS S3 link to upload a file.
uploadFile (file, uploadUrl, fileKey, tags) {
const config = {
headers: {
'Content-Type': file.type,
'x-amz-tagging': getTagging(tags)
}
}
axios.put(uploadUrl, file, config)
.then( response => {
console.log('file upload response', response)
this.createDocument(file, fileKey)
})
.catch( e => {
console.log('file upload ERROR:', e)
console.log(' e.response', e.response)
this.updateStatusOnFile(file.userInterfaceId, DOCUMENT_UPLOAD_STATUS.FAILED)
})
}
Upvotes: 0