Skip to content

Instantly share code, notes, and snippets.

@andreciornavei
Created March 16, 2022 13:33
Show Gist options
  • Save andreciornavei/083876fff8c89749b758dd9b2839506f to your computer and use it in GitHub Desktop.
Save andreciornavei/083876fff8c89749b758dd9b2839506f to your computer and use it in GitHub Desktop.
AWS S3 STREAM
// IMPORT DEPENDENCIES
const stream = require("stream")
const AWS = require('aws-sdk');
const fs = require('fs');
const s3 = new AWS.S3();
// DOWNLOAD STREAM EXAMPLE
const s3download = (bucketName, keyName, localDest) => {
if (typeof localDest == 'undefined') {
localDest = keyName;
}
let params = {
Bucket: bucketName,
Key: keyName
};
let file = fs.createWriteStream(localDest);
return new Promise((resolve, reject) => {
s3.getObject(params).createReadStream()
.on('end', () => {
return resolve();
})
.on('error', (error) => {
return reject(error);
}).pipe(file);
});
};
// UPLOAD STREAM EXAMPLE
const s3upload = (bucketName, keyName) => {
var pass = new stream.PassThrough();
var params = {Bucket: bucketName, Key: keyName, Body: pass};
s3.upload(params, function(err, data) {
console.log(err, data);
});
return pass;
}
inputStream.pipe(s3upload(s3));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment