S3AssetStorageStrategy

S3AssetStorageStrategy

S3Config

Configuration for connecting to AWS S3.

Signature

interface S3Config {
  credentials: S3Credentials | S3CredentialsProfile;
  bucket: string;
  region?: string;
  nativeS3Configuration?: any;
}

Members

credentials

property
type:
S3Credentials | S3CredentialsProfile
The credentials used to access your s3 account. You can supply either the access key ID & secret, or you can make use of a shared credentials file and supply the profile name (e.g. 'default')

bucket

property
type:
string
The S3 bucket in which to store the assets. If it does not exist, it will be created on startup.

region

property
type:
string
The AWS region in which to host the assets.

nativeS3Configuration

property
type:
any
Configuration object passed directly to the AWS SDK. S3.Types.ClientConfiguration can be used after importing aws-sdk. Using type any in order to avoid the need to include aws-sdk dependency in general.

configureS3AssetStorage

Returns a configured instance of the S3AssetStorageStrategy which can then be passed to the AssetServerOptions storageStrategyFactory property.

Before using this strategy, make sure you have the aws-sdk package installed:

npm install aws-sdk

Example

plugins: [
  AssetServerPlugin.init({
    route: 'assets',
    assetUploadDir: path.join(__dirname, 'assets'),
    port: 5002,
    namingStrategy: new DefaultAssetNamingStrategy(),
    storageStrategyFactory: configureS3AssetStorage({
      bucket: 'my-s3-bucket',
      credentials: {
        accessKeyId: process.env.AWS_ACCESS_KEY_ID,
        secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
      },
    }),
}),

Signature

function configureS3AssetStorage(s3Config: S3Config): void

Parameters

s3Config

parameter
type:
S3Config

S3AssetStorageStrategy

An AssetStorageStrategy which uses Amazon S3 object storage service. To us this strategy you must first have access to an AWS account. See their getting started guide for how to get set up.

Before using this strategy, make sure you have the aws-sdk package installed:

npm install aws-sdk

Note: Rather than instantiating this manually, use the configureS3AssetStorage function.

Signature

class S3AssetStorageStrategy implements AssetStorageStrategy {
  constructor(s3Config: S3Config, toAbsoluteUrl: (request: Request, identifier: string) => string)
  async init() => ;
  destroy?: (() => void | Promise<void>) | undefined;
  async writeFileFromBuffer(fileName: string, data: Buffer) => Promise<string>;
  async writeFileFromStream(fileName: string, data: Stream) => Promise<string>;
  async readFileToBuffer(identifier: string) => Promise<Buffer>;
  async readFileToStream(identifier: string) => Promise<Stream>;
  async deleteFile(identifier: string) => Promise<void>;
  async fileExists(fileName: string) => Promise<boolean>;
}

Implements

Members

constructor

method
type:
(s3Config: S3Config, toAbsoluteUrl: (request: Request, identifier: string) => string) => S3AssetStorageStrategy

init

async method
type:
() =>

destroy

property
type:
(() => void | Promise<void>) | undefined

writeFileFromBuffer

async method
type:
(fileName: string, data: Buffer) => Promise<string>

writeFileFromStream

async method
type:
(fileName: string, data: Stream) => Promise<string>

readFileToBuffer

async method
type:
(identifier: string) => Promise<Buffer>

readFileToStream

async method
type:
(identifier: string) => Promise<Stream>

deleteFile

async method
type:
(identifier: string) => Promise<void>

fileExists

async method
type:
(fileName: string) => Promise<boolean>