I have a setup for uploading files to AWS S3 via Angular 7 and Node.Js. The upload works fine. But there is an issue with the xhr.upload.onprogress
event.
This event is only triggered when hosting the server via http
. When using an https
connection, the event isn't triggered once.
I tested it on heroku and my EC2 instance and came to the conclusion that only http
works. Therefore, my production app hosted on AWS via https
, doesn't work, either.
Here is the code (write a comment if more details are needed):
Angular Service (Frontend)
const xhr = new XMLHttpRequest();
let progress = 0;
xhr.upload.onprogress = (event: ProgressEvent) => {
console.log('xhr on upload', event);
if (event.lengthComputable) {
progress = 100 * (event.loaded / event.total);
console.log('file upload progress', { progress, file });
}
};
xhr.responseType = 'json';
xhr.open('POST', `${API_URL}/${this.API_PATH}/upload`, true);
xhr.setRequestHeader('authorization', this.authService.getAuthToken());
xhr.send(payload);
xhr.onload = () => {
observer.next(xhr.response);
observer.complete();
alive = false;
};
Node.Js
module.exports = async (req, res) => {
try {
const busboy = new Busboy({ headers: req.headers })
busboy.on('finish', async () => {
const fileData = req.files.file
const fileId = req.body.fileId
await uploadToAws(fileData)
// ...
})
req.pipe(busboy)
} catch (err) { /* ... */ }
}
const uploadToAws = async (fileData, file) => {
return new Promise((resolve, reject) => {
const params = {
Body: fileData.data,
Bucket: awsConfig.buckets.drakery,
ContentType: fileData.mimetype,
Key: `mykey`,
StorageClass: 'ONEZONE_IA',
}
awsConfig.s3
.upload(params, (err, data) => err ? reject(err) : resolve(data.Location))
.on('httpUploadProgress', function (evt) {
console.log('httpUploadProgress', evt);
})
})
I am really confused why this is happening. I couldn't find any resources online on this problem.
I am really thankful for every help!
Thanks