Have ideas to improve npm?Join in the discussion! »

    @middy/s3-object-response
    TypeScript icon, indicating that this package has built-in type declarations

    2.0.1 • Public • Published

    Middy s3-object-response middleware

    Middy logo

    S3 object response middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda

    ** This middleware is a Proof of Concept and requires real world testing before use, not recommended for production **

    Fetches S3 object as a stream and writes back to s3 object response.

    Install

    To install this middleware you can use NPM:

    npm install --save @middy/s3-object-response

    Options

    • bodyType (string) (required): How to pass in the s3 object through the handler. Can be stream or promise.
    • AwsClient (object) (default AWS.S3): AWS.STS class constructor (e.g. that has been instrumented with AWS XRay). Must be from aws-sdk v2.
    • awsClientOptions (object) (optional): Options to pass to AWS.STS class constructor.
    • awsClientCapture (function) (optional): Enable XRay by passing captureAWSClient from aws-xray-sdk in.
    • disablePrefetch (boolean) (default false): On cold start requests will trigger early if they can. Setting awsClientAssumeRole disables prefetch.

    NOTES:

    • The response from the handler must match the allowed parameters for S3.writeGetObjectResponse, excluding RequestRoute and RequestToken.
    • Lambda is required to have IAM permission for s3-object-lambda:WriteGetObjectResponse

    Sample usage

    Stream

    import zlib from 'zlib'
    import middy from '@middy/core'
    import s3ObjectResponse from '@middy/s3-object-response'
    
    const handler = middy((event, context) => {
      const readStream = context.s3Object
      const transformStream = zlib.createBrotliCompress()
      return {
        Body: readStream.pipe(transformStream)
      }
    })
    
    handler
      .use(s3ObjectResponse({
        bodyType: 'stream'
      }))

    Promise

    import zlib from 'zlib'
    import middy from '@middy/core'
    import s3ObjectResponse from '@middy/s3-object-response'
    
    const handler = middy(async (event, context) => {
      let body = await context.s3Object
      // change body
      return {
        Body: JSON.stringify(body)
      }
    })
    
    handler
      .use(s3ObjectResponse({
        bodyType: 'promise'
      }))

    Middy documentation and examples

    For more documentation and examples, refers to the main Middy monorepo on GitHub or Middy official website.

    Contributing

    Everyone is very welcome to contribute to this repository. Feel free to raise issues or to submit Pull Requests.

    License

    Licensed under MIT License. Copyright (c) 2017-2021 Luciano Mammino, will Farrell, and the Middy team.

    FOSSA Status

    Install

    npm i @middy/s3-object-response

    DownloadsWeekly Downloads

    70

    Version

    2.0.1

    License

    MIT

    Unpacked Size

    9.41 kB

    Total Files

    5

    Last publish

    Collaborators

    • avatar
    • avatar
    • avatar