Skip to content

Instantly share code, notes, and snippets.

@tlaitinen
Created October 11, 2018 12:00
Show Gist options
  • Save tlaitinen/4def0b7ea48867d811527741364c22bc to your computer and use it in GitHub Desktop.
Save tlaitinen/4def0b7ea48867d811527741364c22bc to your computer and use it in GitHub Desktop.
import AWS from 'aws-sdk';
import cfg from '../config';
import archiver from 'archiver';
import {Archiver} from 'archiver';
const s3 = new AWS.S3({
region: cfg.awsRegion,
signatureVersion: 'v4'
});
export type ObjectKeyWithPath = {
path: string;
objectKey: string;
};
export function appendZipStreamEntry(archive:Archiver, entry:ObjectKeyWithPath) {
const objectStream = s3.getObject({
Key: entry.objectKey,
Bucket: cfg.awsS3Bucket
}).createReadStream();
archive.append(objectStream, {name: entry.path});
}
export function createZipStream(entries:ObjectKeyWithPath[]):Archiver {
const archive = archiver('zip', {
zlib: {level: 9}
});
archive.on('error', (err) => {
throw err;
});
function* addEntries() {
for (let i = 0; i < entries.length; i++) {
yield appendZipStreamEntry(archive, entries[0]);
}
archive.finalize();
}
const entryGenerator = addEntries();
entryGenerator.next();
archive.on('entry', () => {
entryGenerator.next();
});
return archive;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment