Skip to content

Instantly share code, notes, and snippets.

@DWboutin
Created March 31, 2019 22:33
Show Gist options
  • Save DWboutin/5314172f65c9dab32c663faab8a42de3 to your computer and use it in GitHub Desktop.
Save DWboutin/5314172f65c9dab32c663faab8a42de3 to your computer and use it in GitHub Desktop.
Direct image url to S3 wiht axios and nodejs
import AWS from 'aws-sdk';
import stream from 'stream'
import axios from 'axios';
export default async (url, filename, callback) => {
const s3 = new AWS.S3({ params: { Bucket: process.env.STATIC_MAPS_BUCKET }});
let contentType = 'application/octet-stream'
let promise = null
const uploadStream = () => {
const pass = new stream.PassThrough();
promise = s3.upload({
Key: filename,
Body: pass,
ACL: 'public-read',
ContentType: contentType,
}).promise();
return pass;
}
const imageRequest = axios({
method: 'get',
url: url,
responseType: 'stream'
}).then( (response) => {
console.log('STREAM.then', response)
if(response.status===200){
contentType = response.headers['content-type'];
response.data.pipe(uploadStream());
}
});
return promise
}
@venkatgreat309
Copy link

Thankyou very much.It's working fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment