Skip to content

Instantly share code, notes, and snippets.

@amulyakashyap09
Created April 8, 2019 06:20
Show Gist options
  • Save amulyakashyap09/d67bba57b2834392a63673d7846df268 to your computer and use it in GitHub Desktop.
Save amulyakashyap09/d67bba57b2834392a63673d7846df268 to your computer and use it in GitHub Desktop.
Pipe download stream directly to s3 upload using nodejs streams
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