Created
March 31, 2019 22:33
-
-
Save DWboutin/5314172f65c9dab32c663faab8a42de3 to your computer and use it in GitHub Desktop.
Direct image url to S3 wiht axios and nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
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
Thank you for this. It is a great solution.