Created
November 20, 2018 10:28
-
-
Save rimiti/377c7d46503f82670faecd0037279d34 to your computer and use it in GitHub Desktop.
NodeJS - Upload local (file) image to AWS S3 from stream with public access.
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'); | |
const fs = require('fs'); | |
AWS.config.update({ accessKeyId: 'ACCESSKEYID', secretAccessKey: 'SECRETACCESSKEY', region: 'eu-west-3'}); | |
const fileStream = fs.createReadStream('/path/to/your/image.png'); | |
fileStream.on('error', function (err) { | |
if (err) { throw err; } | |
}); | |
fileStream.on('open', function () { | |
const s3 = new AWS.S3(); | |
s3.putObject({ | |
Bucket: 'your-bucket', | |
Key: 'file-renamed.png', | |
ACL: 'public-read', | |
Body: fileStream, | |
Metadata: { 'type': 'png', 'user': 'Dimitri DO BAIRRO' } | |
}, function (err) { | |
if (err) { throw err; } | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment