Skip to content

Latest commit

 

History

History
120 lines (82 loc) · 2.57 KB

README.md

File metadata and controls

120 lines (82 loc) · 2.57 KB

s3-zip

npm version Build Status Coverage Status JavaScript Style Guide

Download selected files from an Amazon S3 bucket as a zip file.

Install

npm install s3-zip

AWS Configuration

Refer to the AWS SDK for authenticating to AWS prior to using this plugin.

Usage

Zip specific files

var fs = require('fs')
var join = require('path').join
var s3Zip = require('s3-zip')

var region = 'bucket-region'
var bucket = 'name-of-s3-bucket'
var folder = 'name-of-bucket-folder/'
var file1 = 'Image A.png'
var file2 = 'Image B.png'
var file3 = 'Image C.png'
var file4 = 'Image D.png'

var output = fs.createWriteStream(join(__dirname, 'use-s3-zip.zip'))

s3Zip
  .archive({ region: region, bucket: bucket}, folder, [file1, file2, file3, file4])
  .pipe(output)

Zip files with AWS Lambda

Example of s3-zip in combination with AWS Lambda.

Zip a whole bucket folder

var fs = require('fs')
var join = require('path').join
var AWS = require('aws-sdk')
var s3Zip = require('s3-zip')
var XmlStream = require('xml-stream')

var region = 'bucket-region'
var bucket = 'name-of-s3-bucket'
var folder = 'name-of-bucket-folder/'
var s3 = new AWS.S3({ region: region })
var params = {
  Bucket: bucket,
  Prefix: folder
}

var filesArray = []
var files = s3.listObjects(params).createReadStream()
var xml = new XmlStream(files)
xml.collect('Key')
xml.on('endElement: Key', function(item) {
  filesArray.push(item['$text'].substr(folder.length))
})

xml
  .on('end', function () {
    zip(filesArray)
  })

function zip(files) {
  console.log(files)
  var output = fs.createWriteStream(join(__dirname, 'use-s3-zip.zip'))
  s3Zip
   .archive({ region: region, bucket: bucket }, folder, files)
   .pipe(output)
}

Testing

Tests are written in Node Tap, run them like this:

npm t

If you would like a more fancy coverage report:

npm run coverage