问题
I'm using fs.readFile to read in a local mp4 file of around 700KB, and AWS SDK to load the file onto my S3 bucket. The process works, but the file is corrupt, presumably truncated because the size of the resulting file on S3 is only around 500KB.
The code I'm using works for images just fine. Just not working for mp4 files. Here's my code:
fs.readFile(file_location, function (err, data) {
if (err) {
console.log('fs error': err);
} else {
var base64data = new Buffer(data, 'binary');
var params = {
Bucket: config.settings.bucket,
Key: 'file.mp4',
Body: base64data,
ContentEncoding: 'base64',
ContentType: 'video/mp4'
};
s3.putObject(params, function(err, data) {
if (err) {
console.log('Error putting object on S3: ', err);
} else {
console.log('Placed object on S3: ', object_key);
}
});
}
});
I've tried omitting the ContentEncoding and/or the ContentType properties from the params object.
EDIT:
So when I did a console.log of the length of the data that fs.readFile returned, it consistently comes up short. I didn't realize I wasn't actually handling base64 data, either. So if I use the following code where I ask to read the file in using base64, the data.length from fs.readFile appears to be correct, and an mp4 file shows up on my S3 bucket with a playhead in the browser (which I wasn't getting before), but the video won't play & it's black, so it appears to still be corrupt.
fs.readFile(file_location, 'base64', function (err, data) {
if (err) {
console.log('fs error': err);
} else {
var params = {
Bucket: config.settings.bucket,
Key: 'file.mp4',
Body: data,
ContentEncoding: 'base64',
ContentType: 'video/mp4'
};
s3.putObject(params, function(err, data) {
if (err) {
console.log('Error putting object on S3: ', err);
} else {
console.log('Placed object on S3: ', object_key);
}
});
}
});
回答1:
The data
variable you get back from readFile
is already good to go. Just pass that directly on with Body: data
. Drop the ContentEncoding
param entirely as you are not sending base64 data.
来源:https://stackoverflow.com/questions/33248514/corrupt-truncated-mp4-upload-to-s3-bucket-using-nodejs-and-aws-s3