Skip to content

Commit

Permalink
use environment variables and add Express.js example (#14)
Browse files Browse the repository at this point in the history
* add Express.js example for data stream pipe

* use env-vars

* fix error

* fix this reference error
  • Loading branch information
arturojain authored and DanielHindi committed Feb 9, 2018
1 parent dd41278 commit f0e1334
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var config ={
var zipper = new S3Zipper(config);
```


### Filter out Files
```
zipper.filterOutFiles= function(file){
Expand Down Expand Up @@ -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
```
Expand Down
45 changes: 30 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}


Expand All @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down

0 comments on commit f0e1334

Please sign in to comment.