Corrupt/truncated mp4 upload to S3 bucket using NodeJS and AWS S3

北城以北 提交于 2019-12-24 05:24:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!