Last active
July 27, 2024 12:21
-
-
Save sjukkola/04892a20338115807b8bdc73dcc644ed to your computer and use it in GitHub Desktop.
Extract image from url and upload to AWS s3
This file contains hidden or 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
const AWS = require('aws-sdk'); | |
AWS.config.loadFromPath('./config.json'); | |
const s3 = new AWS.S3(); | |
const uuidV4 = require('uuid'); | |
const request = require('request'); | |
const s3Bucket = new AWS.S3( { params: { Bucket: 'samus-original-bucket' } } ) | |
request.get({ | |
method: 'GET', | |
url: 'http://lorempixel.com/400/200/', | |
encoding: null, // If null, the body is returned as a Buffer. | |
}, (error, response, body) => { | |
if (!error && response.statusCode == 200) { | |
const data = { | |
Key: uuidV4(), | |
Body: body, | |
ContentType: response.headers['content-type'] | |
}; | |
s3Bucket.upload(data, (err, data) => { | |
if (err) { | |
console.log('Error uploading data: ', data); | |
} else { | |
console.log('succesfully uploaded the image!'); | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment