Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
}
@knihit
Copy link

knihit commented Jul 26, 2020

Is there a unit test as well that you have for the above code. I am struggling with mocking axios to return streams.

@pomSense
Copy link

pomSense commented Aug 13, 2020

thanks for this man!

@romang288
Copy link

romang288 commented Nov 10, 2020

Great script, it does work with a single URL. Do you have a version which will work with an ARRAY of URLs running all requests concurrently and then returning the result / error to a calling module?

@eliastg
Copy link

eliastg commented Jul 23, 2021

Thank you for this. It is a great solution.

@venkatgreat309
Copy link

venkatgreat309 commented Feb 17, 2022

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