From f0e13349b1c27decc24d1d0676f3e61ca2209809 Mon Sep 17 00:00:00 2001 From: Arturo Jain Date: Fri, 9 Feb 2018 12:04:40 -0600 Subject: [PATCH] use environment variables and add Express.js example (#14) * add Express.js example for data stream pipe * use env-vars * fix error * fix this reference error --- README.md | 20 ++++++++++++++++++++ index.js | 45 ++++++++++++++++++++++++++++++--------------- package.json | 8 ++++---- 3 files changed, 54 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index ab18fd0..bc7cbeb 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ var config ={ var zipper = new S3Zipper(config); ``` + ### Filter out Files ``` zipper.filterOutFiles= function(file){ @@ -63,6 +64,25 @@ zipper.zipToFile ({ }); ``` +### Pipe zip data to stream (using Express.js) +``` +app.all('/', function (request, response) { + response.set('content-type', 'application/zip') // optional + zipper.streamZipDataTo({ + pipe: response + , folderName: 'myBucketFolderName' + , startKey: 'keyOfLastFileIZipped' // could keep null + , recursive, true + } + ,function (err, result) { + if(err) + console.error(err); + else{ + console.log(result) + } + }) +}) +``` ### Zip fragments to local file system with the filename pattern with a maximum file count ``` diff --git a/index.js b/index.js index 57e8c34..e1ee812 100644 --- a/index.js +++ b/index.js @@ -6,12 +6,19 @@ var fs = require('fs'); var s3 = require('s3'); function S3Zipper(awsConfig) { - assert.ok(awsConfig, 'AWS S3 options must be defined.'); - assert.notEqual(awsConfig.accessKeyId, undefined, 'Requires S3 AWS Key.'); - assert.notEqual(awsConfig.secretAccessKey, undefined, 'Requires S3 AWS Secret'); - assert.notEqual(awsConfig.region, undefined, 'Requires AWS S3 region.'); - assert.notEqual(awsConfig.bucket, undefined, 'Requires AWS S3 bucket.'); - this.init(awsConfig); + var self = this + AWS.config.getCredentials(function (err) { + if (err) { + assert.ok(awsConfig, 'AWS S3 options must be defined.'); + assert.notEqual(awsConfig.accessKeyId, undefined, 'Requires S3 AWS Key.'); + assert.notEqual(awsConfig.secretAccessKey, undefined, 'Requires S3 AWS Secret'); + assert.notEqual(awsConfig.region, undefined, 'Requires AWS S3 region.'); + assert.notEqual(awsConfig.bucket, undefined, 'Requires AWS S3 bucket.'); + self.init(awsConfig); + } else { + self.init(awsConfig) + } + }) } @@ -23,16 +30,24 @@ function listObjectInner() { S3Zipper.prototype = { init: function (awsConfig) { this.awsConfig = awsConfig; - AWS.config.update({ - accessKeyId: awsConfig.accessKeyId, - secretAccessKey: awsConfig.secretAccessKey, - region: awsConfig.region - }); - this.s3bucket = new AWS.S3({ - params: { - Bucket: this.awsConfig.bucket + var self = this + AWS.config.getCredentials(function (err) { + + if (err) { + AWS.config.update({ + accessKeyId: awsConfig.accessKeyId, + secretAccessKey: awsConfig.secretAccessKey, + region: awsConfig.region + }); } - }); + + self.s3bucket = new AWS.S3({ + params: { + Bucket: self.awsConfig.bucket + } + }); + + }) } , filterOutFiles: function (fileObj) { diff --git a/package.json b/package.json index 86b41dd..faa9854 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,13 @@ { "name": "aws-s3-zipper", - "version": "1.0.1", + "version": "1.1.0", "description": "Zip files from and to an Amazon AWS S3 Bucket directory as a stream, file or fragments. Allows filtering files.", "main": "index.js", "dependencies": { - "archiver":"0.21.*", + "archiver": "0.21.*", + "async": "1.5.*", "aws-sdk": "2.2.*", - "async" :"1.5.*", - "s3" : "latest" + "s3": "latest" }, "repository": "https://github.com/DanielHindi/aws-s3-zipper.git", "keywords": [