1.0.1 • Published 8 years ago

s3-proxy-stream v1.0.1

Weekly downloads
5
License
MIT
Repository
github
Last release
8 years ago

s3-proxy-stream

NPM version Build status Test coverage Dependency Status License Downloads

Simplified s3.getObject() designed for proxying.

API

const get = require('s3-proxy-stream')(s3, options)

const s3 = new AWS.S3({
  // access keys and stuff
  params: {
    Bucket: 'my-bucket', // whichever bucket you are using
  }
})

const get = require('s3-proxy-stream')(s3, {
  // default `s3.getObject()` params
  // you can also set `Bucket` here
  Bucket: 'my-bucket',
})

All options are defaults for every request.

get(key, options).then(stream => {}).catch(err => {})

Get an object at key. options is passed to s3.getObject(), except we have an extra field called .headers. Simply pass request.headers and the appropriate s3.getObject() params will be set.

app.use((req, res, next) => {
  get(req.path, {
    headers: req.headers,
  })
  .then(stream => {
    // set the status code
    res.statusCode = stream.statusCode

    // set the headers
    for (const key of stream.headers) {
      res.setHeader(key, stream.headers[key])
    }

    // no body to pipe
    if (res.statusCode === 304) return

    // pipe the response
    stream.on('error', next).pipe(res)
  })
  // handle errors
  .catch(next)
})

stream

The stream has the following properties:

  • .status and .statusCode - the status code of the response
  • .headers - headers of the response

error

The error has the following properties:

  • .status and .statusCode - the status code of the response
1.0.1

8 years ago

1.0.0

8 years ago

0.0.0

8 years ago