我正在使用fs.readFile读取大约700KB的本地mp4文件,并使用AWS开发工具包将文件加载到我的S3存储桶中。该过程有效,但文件已损坏,可能被截断了,因为S3上生成的文件大小仅为500KB左右。
我使用的代码适用于图像。只是不适用于mp4文件。这是我的代码:
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);
}
});
}
});
我尝试从params对象中省略ContentEncoding和/或ContentType属性。
编辑:
因此,当我对fs.readFile返回的数据长度进行console.log记录时,结果总是很短。我也没有意识到我实际上也没有处理base64数据。因此,如果我在要求使用base64读取文件的地方使用以下代码,则fs.readFile中的data.length似乎正确,并且mp4文件显示在我的S3存储桶中,并且在浏览器中带有播放头(没来得及),但视频无法播放,而且视频是黑色的,因此它似乎仍然损坏。
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);
}
});
}
});
data
从中获取的变量readFile
已经可以使用了。只需将其直接传递给即可Body: data
。ContentEncoding
由于不发送base64数据,因此请完全删除参数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句