Last active
December 12, 2021 08:16
-
-
Save aug2uag/d276c0337597de0465890fe5380c8ca4 to your computer and use it in GitHub Desktop.
NodeJS S3 upload image from URL
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
// https://stackoverflow.com/a/25564742/1546710 | |
var AWS = require('aws-sdk'); | |
var request = require('request'); | |
AWS.config.loadFromPath('./config.json'); | |
var s3 = new AWS.S3(); | |
function put_from_url(url, bucket, key, callback) { | |
request({ | |
url: url, | |
encoding: null | |
}, function(err, res, body) { | |
if (err) | |
return callback(err, res); | |
s3.upload({ | |
Bucket: bucket, | |
Key: key, | |
ContentType: res.headers['content-type'], | |
ContentLength: res.headers['content-length'], | |
Body: body // buffer | |
}, callback); | |
}) | |
} | |
put_from_url('https://public.nftstatic.com/static/nft/res/f2a4326f37474c4eb216df45995fa4ee.png', 'buck37', 'uniqu3K3y', function(err, res) { | |
if (err) | |
throw err; | |
console.log('Uploaded data successfully!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment