I need to convert base64 data to '.mp4' file and then upload it to the Amazon S3 bucket. When I am trying to access the path, err "file cannot be played because it is corrupted" is shown. I have checked through DragonDisk that my file is uploaded to the S3 bucket.
Below is my code:
var accessKeyId = config.s3.S3_KEY;
var secretAccessKey = config.s3.S3_SECRET;
AWS.config.update({
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
apiVersion: {
s3: "2006-03-01"
}
});
AWS.config.region = "us-east-1";
var s3 = new AWS.S3();
var video = "user_" + Math.floor(Date.now() / 1000) + ".mp4";
path = '';
var bitmap = new Buffer(data.message, 'base64');
fs.writeFile(video, bitmap, function (err) {
if (err) {
console.log("write file error")
console.log(err);
callback(err, {});
} else {
var bodyStream = fs.createReadStream(video);
var params = {Bucket: 'bucket', Key: "users_videos/" + video, Body: bodyStream, ACL: 'public-read',ContentType: "video/mp4"};
s3.putObject(params, function (err, dataa) {
if (err) {
console.log("inside putobject error");
console.log(err);
path = '';
callback(err, {});
}
else {
path = "http://xx.s3.amazonaws.com/users_videos/" + video;
console.log("Successfully uploaded data to Bucket");
console.log(path);
fs.unlink(video, function (err) {
if (err)
{
console.log(err);
}
console.log("deleted file");
});
}
}
}