Created
April 8, 2019 06:20
-
-
Save amulyakashyap09/d67bba57b2834392a63673d7846df268 to your computer and use it in GitHub Desktop.
Pipe download stream directly to s3 upload using nodejs streams
This file contains 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
const http = require('http'), | |
stream = require('stream'), | |
AWS = require('aws-sdk'), | |
http = require('http'); | |
const uploadStream = ({ Bucket, Key }) => { | |
const s3 = new AWS.S3(); | |
const pass = new stream.PassThrough(); | |
return { | |
writeStream: pass, | |
promise: s3.upload({ Bucket, Key, Body: pass }).promise(), | |
}; | |
} | |
const download = async function (url) { | |
return new Promise(function (resolve, reject) { | |
const filename = yourfile.mp3; | |
const { writeStream, promise } = uploadStream({ Bucket: 'your-bucket-name', Key: filename }); | |
http.get(url, function (response) { | |
response.pipe(writeStream); | |
promise.then().catch(function (reason) { | |
reject(reason); | |
}); | |
writeStream.on('finish', function () { | |
resolve("completed job.... ") | |
}); | |
}).on('error', function (err) { // Handle errors | |
reject(err); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment