Skip to content

Instantly share code, notes, and snippets.

@sjukkola
Last active July 16, 2023 17:31
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sjukkola/04892a20338115807b8bdc73dcc644ed to your computer and use it in GitHub Desktop.
Save sjukkola/04892a20338115807b8bdc73dcc644ed to your computer and use it in GitHub Desktop.
Extract image from url and upload to AWS s3
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