Skip to content

Instantly share code, notes, and snippets.

@rimiti
Created November 20, 2018 10:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rimiti/377c7d46503f82670faecd0037279d34 to your computer and use it in GitHub Desktop.
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.
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