Skip to content

Instantly share code, notes, and snippets.

@DWboutin
Created March 31, 2019 22:33
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • 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
}
@romang288
Copy link

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

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