# s3-express-zip

> Download multiple objects from S3 bucket to zip archive through stream

Latest version **1.1.0** (published 2021-06-27) · ISC license · 0 weekly downloads

## Install

```sh
npm install s3-express-zip
pnpm add s3-express-zip
yarn add s3-express-zip
bun add s3-express-zip
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2021-06-27 |
| First published | 2021-05-30 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 24.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Romain PIVETEAU |
| Maintainers | rpiveteau |
| Keywords | s3, stream, zip, express, bucket, download, zipping, Content-Length, headers |

## Links

- npm: https://www.npmjs.com/package/s3-express-zip
- Repository: https://github.com/rpiveteau/S3-Express-Zip
- Issues: https://github.com/rpiveteau/S3-Express-Zip/issues
- npm.io page: https://npm.io/package/s3-express-zip

## Dependencies (4)

- [async](https://npm.io/package/async.md) ^3.2.0
- [zip-stream](https://npm.io/package/zip-stream.md) ^4.1.0
- [@types/node](https://npm.io/package/@types/node.md) ^15.3.0
- [@types/async](https://npm.io/package/@types/async.md) ^3.2.6

## Alternatives

- [lodash.startswith](https://npm.io/package/lodash.startswith.md) — 769.7K weekly downloads
- [@tarojs/service](https://npm.io/package/@tarojs/service.md) — 33.9K weekly downloads
- [io.extendreality.tilia.indicators.spatialtargets.unity](https://npm.io/package/io.extendreality.tilia.indicators.spatialtargets.unity.md) — 131 weekly downloads
- [@rtarojs/taro](https://npm.io/package/@rtarojs/taro.md) — 90 weekly downloads
- [node-branch-io](https://npm.io/package/node-branch-io.md) — 50 weekly downloads

## Recent versions

- 1.1.0 (latest) — 2021-06-27
- 1.0.6 — 2021-06-27
- 1.0.4 — 2021-05-31
- 1.0.3 — 2021-05-31
- 1.0.2 — 2021-05-30
- 1.0.1 — 2021-05-30
- 1.0.0 — 2021-05-30

## README

# S3-Express-Zip
```typescript
import S3ExpressZip, {getS3ObjectsList, getS3ObjectStream, getHTTPHeaders} from 's3-express-zip';
import {PassThrough} from 'stream';
import https from "https";
import {S3} from 'aws-sdk';

const configS3 = {
    endpoint: 'BUCKET_ENDPOINT',
    region: 'BUCKET_REGION',
    accessKeyId: 'ACCESS_KEY_ID',
    secretAccessKey: 'SECRET_ACCESS_KEY'
};

const s3 = new S3({
    endpoint: 'BUCKET_ENDPOINT',
    accessKeyId: 'ACCESS_KEY_ID',
    secretAccessKey: 'SECRET_ACCESS_KEY',
});

s3.config.update(configS3);
const Bucket = 'BUCKET_NAME';
const Prefix = 'BUCKET_PREFIX';

function cb(error: any, data: any) {
    console.log(
      '-----------------',
      `|ZIP CB ${error ? 'ERROR' : 'SUCCES'}|`,
      error || `${data}o archived`,
      `|END CB ${error ? 'ERROR' : 'SUCCES'}|`,
      '-----------------',
    )
}
/**
  res should be a writable stream
*/
async function downloadFromS3KeepingPrefix(req, res) {
    // Default archive name:
    const archiveName = `archive-${(new Date()).getTime()}.zip`;
    // Retrieving objects from Bucket
    const objects = await getS3ObjectsList(s3, Bucket, Prefix);
    // Getting headers for HTTP Response if needed
    const headers: {[key: string]: string} = getHTTPHeaders(objectsList, archiveName);
    // Setting headers for HTTP Response if needed
    (Object.keys(headers))
      .forEach((headerKey: string) => res.setHeader(headerKey, headers[headerKey]));
  
    // Finally send stream archive to client (or any writable stream)  
    S3ExpressZip({res})(objects.map((f:any) => getS3ObjectStream(s3, f)), cb)
}

/**
  res should be a writable stream
*/
async function downloadFromS3RemovingPrefix(req, res) {
    // Default archive name:
    const archiveName = `archive-${(new Date()).getTime()}.zip`;
    /**
      Retrieving objects from Bucket
      You can remove prefix from filename in archive, or filter the objects..
      If you use the Content-Length property header, 
      you have to remove Prefix from filename before getting httpHeaders
      else, the Content-Length will missmatch and HTTP Request fail
    */
    const objects = (await getS3ObjectsList(s3, Bucket, Prefix))
        .map((obj: any) => ({
            ...obj,
            Name: obj.Key.split(`${Prefix}/`).join('')
        }));
    // Getting headers for HTTP Response if needed
    const headers: {[key: string]: string} = getHTTPHeaders(objectsList, archiveName);
    // Setting headers for HTTP Response if needed
    (Object.keys(headers))
      .forEach((headerKey: string) => res.setHeader(headerKey, headers[headerKey]));

    // Finally send stream archive to client (or any writable stream)
    S3ExpressZip({res})(objects.map((f:any) => getS3ObjectStream(s3, f)), cb)
}

```

---
_Source: https://npm.io/package/s3-express-zip · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
