Skip to content

Instantly share code, notes, and snippets.

@thospfuller
Created August 12, 2020 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thospfuller/edb9ddb90b0b29dec84e56598c6346f4 to your computer and use it in GitHub Desktop.
Save thospfuller/edb9ddb90b0b29dec84e56598c6346f4 to your computer and use it in GitHub Desktop.
An example Node script which uploads a file to AWS S3.
const fs = require('fs');
const AWS = require('aws-sdk');
const zlib = require('zlib');
const stream = require('stream');
AWS.config["credentials"] = new AWS.SharedIniFileCredentials({profile: 'some-test-profile'});
AWS.config["logger"] = console;
const s3Obj = new AWS.S3();
const uploadFile = (fileName) => {
const fileStream = fs.createReadStream(fileName).pipe(zlib.createGzip());
fileStream.on('error', function(err) {
console.log('fileStream.createReadStream error.', err);
});
fileStream.on('data', (chunk) => {
console.log(`Received ${chunk.length} bytes of data.`);
});
const params = {
'Bucket': 'some-bucket',
'Key': 'someFile.gzip',
'Body': fileStream
};
/**
* https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3/ManagedUpload.html
*
* "The managed uploader allows for easy and efficient uploading of buffers, blobs, or streams, using a configurable
* amount of concurrency to perform multipart uploads where possible. This abstraction also enables uploading
* streams of unknown size due to the use of multipart uploads."
*/
s3Obj.upload(
params,
function (err, data) {
if (err) {
throw err;
} else if (data) {
console.log("Upload Success; data: ", data);
}
console.log(`File uploaded successfully to ${data.Location}`);
}
).promise()
.then(
function(data) {
console.log("Successfully uploaded.");
},
function(err) {
console.log("There was an error while uploading.", err);
}
);
};
/**
* The example.mp4 file is ~ 265.5MB.
*/
uploadFile('example.mp4');
console.log ('...done!');
// WARNING: Do this and the upload won't happen.
//
// process.exit();
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment