Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maskaravivek/47244cf21c796c2dd0039a3a806f0598 to your computer and use it in GitHub Desktop.
Save maskaravivek/47244cf21c796c2dd0039a3a806f0598 to your computer and use it in GitHub Desktop.
/*global module, require, Promise, console */
var aws = require("aws-sdk"),
path = require("path"),
fs = require("fs"),
os = require("os"),
uuid = require("uuid"),
s3 = new aws.S3(),
downloadFromS3 = function (bucket, fileKey) {
"use strict";
return new Promise(function (resolve, reject) {
var filePath = path.join(os.tmpdir(), uuid.v4() + path.extname(fileKey)),
file = fs.createWriteStream(filePath),
stream = s3
.getObject({
Bucket: bucket,
Key: fileKey,
})
.createReadStream();
stream.setEncoding("utf8");
stream.on("error", reject);
file.on("error", reject);
file.on("finish", function () {
console.log("downloaded", bucket, fileKey);
resolve(filePath);
});
stream.pipe(file);
});
},
uploadToS3 = function (bucket, fileKey, audioBuffers, acl) {
"use strict";
return s3
.upload({
Bucket: bucket,
Key: fileKey,
Body: audioBuffers,
ACL: acl || "private",
})
.promise();
};
module.exports = {
download: downloadFromS3,
upload: uploadToS3,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment