Skip to content

Instantly share code, notes, and snippets.

@parkerproject
Forked from DWboutin/directUploadToS3.js
Created December 26, 2020 01:04
Show Gist options
  • Save parkerproject/4ec98f8bfc8964c624fdbf4241d4675f to your computer and use it in GitHub Desktop.
Save parkerproject/4ec98f8bfc8964c624fdbf4241d4675f 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment