Closed ivancortesromero closed 2 years ago
I found this issue on async-busboy
package which seems related to this error, but I'm using the version 0.7.0, a version greater than the one where it is supposed was resolved.
Two things I noticed:
The main one being you're listening for 'finish'
on the writable file stream instead of 'close'
. The latter indicates when the file has actually been closed and would probably be a better signal to wait for.
The following is unnecessary code since file.pipe(writeStream)
already does it for you:
file.on('end', () => {
writeStream.end();
});
On a legacy project, MP4 files are uploaded to a NestJS API with
busboy
on an EC2 instance. Then, the file is uploaded to an S3 bucket.When the AWS S3 library tries to load the file, an apparent "random" error is raised sometimes:
Here are the snippets:
API upload endpoint
BucketService
saveStream
methodAt some moment I thought perhaps this happens because the name of the temporal file on EC2 is always the same:
file.mp4
, and when two files are uploaded at the same time, the first one on finishing removes the file (fs.unlinkSync(upload);
at the endpoint) leaving the other ongoing process without it, so when tries to upload it, this process won't find it. But it is not true because I performed tests where I ensured the files were uploaded one by one. However, I also ensured the name was always different by changing on the controller:const filepath = path.join(fieldname + path.extname(filename));
by
const filepath = path.join(Math.floor(Math.random() * 10000) + path.extname(filename));
but the error is still happening. Another weird thing which is happening is that in my machine I can see (
ls
) the file meanwhile it is uploaded, but in EC2 not.Facts:
Dependencies
Thank guys.