@travetto/asset-s3

1.1.1 • Public • Published

S3 Asset Source

AWS S3 backend for the travetto asset module

Install: @travetto/asset-s3

npm install @travetto/asset-s3

This provides a s3 implementation of the AssetSource which is a backend for the Asset module.

Code: S3 backend wiring

import { InjectableFactory } from '@travetto/di';
import { S3AssetSource, S3AssetConfig } from '@travetto/asset-s3';

class AppConfig {
  @InjectableFactory()
  static getSource(cfg: S3AssetConfig) {
    return new S3AssetSource(cfg);
  }
}

There is a default configuration that you can easily use, with some sensible defaults.

Code: S3 Configuration

import { fromIni } from '@aws-sdk/credential-provider-ini';
import * as S3 from '@aws-sdk/client-s3';
import { EnvUtil } from '@travetto/boot';
import { Config } from '@travetto/config';

/**
 * S3 Support as an Asset Source
 */
@Config('s3.asset')
export class S3AssetConfig {
  region = 'us-east-1'; // AWS Region
  namespace = ''; // S3 Bucket folder
  bucket = ''; // S3 bucket

  accessKeyId = EnvUtil.get('AWS_ACCESS_KEY_ID') ?? '';
  secretAccessKey = EnvUtil.get('AWS_SECRET_ACCESS_KEY') ?? '';
  config: S3.S3ClientConfig; // Additional s3 config

  chunkSize = 5 * 2 ** 20; // Chunk size in bytes

  /**
   * Provide host to bucket
   */
  get hostName() {
    return `${this.bucket}.s3.amazonaws.com`;
  }

  /**
   * Produces the s3 config from the provide details, post construction
   */
  async postConstruct() {
    if (!this.accessKeyId && !this.secretAccessKey) {
      const creds = await fromIni({ profile: EnvUtil.get('AWS_PROFILE') })();
      this.accessKeyId = creds.accessKeyId;
      this.secretAccessKey = creds.secretAccessKey;
    }

    this.config = {
      credentials: {
        accessKeyId: this.accessKeyId,
        secretAccessKey: this.secretAccessKey
      }
    };
  }
}

Additionally, you can see that the class is registered with the @Config annotation, and so these values can be overridden using the standard Configuration resolution paths.

Note: Do not commit your accessKeyId or secretAccessKey values to your source repository, especially if it is public facing. Not only is it a security risk, but Amazon will scan public repos, looking for keys, and if found will react swiftly.

Readme

Keywords

Package Sidebar

Install

npm i @travetto/asset-s3

Homepage

travetto.io

Weekly Downloads

3

Version

1.1.1

License

MIT

Unpacked Size

10.8 kB

Total Files

6

Last publish

Collaborators

  • arcsine