Created
March 16, 2022 13:33
-
-
Save andreciornavei/083876fff8c89749b758dd9b2839506f to your computer and use it in GitHub Desktop.
AWS S3 STREAM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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