hapi-s3

Use Amazon S3 in your hapi server

Usage no npm install needed!

<script type="module">
  import hapiS3 from 'https://cdn.skypack.dev/hapi-s3';
</script>

README

hapi-s3 Build status for hapi S3

Use Amazon S3 in your hapi server

Provides an instance of Scube, a thin wrapper around the S3 client from the AWS SDK, so you can interact with S3 programmatically. It is available at request.server.s3 in route handlers.

Why?

  • Easily implement streaming uploads / downloads.
  • Memory efficient, with one instance of the AWS SDK per server.
  • Loads credentials explicitly.

Install

npm install hapi-s3

Usage

Register the plugin on your server to make request.server.s3 available in route handlers.

const hapi = require('@hapi/hapi');
const s3 = require('hapi-s3');

const server = hapi.server();

const init = async () => {
    await server.register({
        plugin  : s3,
        options : {
            bucket    : 'my-bucket',
            publicKey : process.env.AWS_ACCESS_KEY_ID,
            secretKey : process.env.AWS_SECRET_ACCESS_KEY
        }
    });
    server.route({
        method : 'GET',
        path   : '/',
        async handler(request) {
            const { s3 } = request.server;
            const buckets = await s3.listBuckets();
            return buckets;
        }
    });
    await server.start();
    console.log('Server ready:', server.info.uri);
};

init();

API

Please see Scube for details on the s3 object.

Plugin options

Type: object

The options are passed to new Scube() to configure the S3 client. See Scube for details on the available options, such as bucket, region, and others.

Decorations

For convenience, this plugin adds the following API to the hapi server instance.

server.s3

An instance of Scube, a thin wrapper around the S3 client from the AWS SDK. This is available as request.server.s3 inside of route handlers.

Related

Contributing

See our contributing guidelines for more details.

  1. Fork it.
  2. Make a feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request.

License

MPL-2.0 © Seth Holladay

Go make something, dang it.