Last active
July 14, 2022 19:43
-
-
Save shentonfreude/19d78c626a1745af950ad08e3d0ab1da to your computer and use it in GitHub Desktop.
Use NodeJS AWS SDK to upload a file to S3 with server-side encryption; uses environment for AWS creds
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
var AWS = require('aws-sdk'); | |
var fs = require('fs'); | |
var bucketName = 'my-bucket-name'; | |
var bucketRegion = 'us-gov-west-1'; | |
var file = 'PDFSCANS/AGILE_5_page.pdf'; | |
var key = 'doc_pdf/CHRISJSnocreds.pdf'; | |
AWS.config.update({ | |
region: bucketRegion | |
}); | |
var fileStream = fs.createReadStream(file); | |
fileStream.on('error', function (err) { | |
if (err) { throw err; } | |
}); | |
fileStream.on('open', function () { | |
var s3 = new AWS.S3(); | |
s3.putObject({ | |
Bucket: bucketName, | |
Key: key, | |
Body: fileStream, | |
ServerSideEncryption: 'AES256', | |
Metadata: { 'docId': 'DOCID', 'docKey': 'XYZZY', 'index': 'pdf' } | |
}, function (err) { | |
if (err) { throw err; } | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment