🚨 Announcing Vendure v2 Beta

S3AssetStorageStrategy

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.

Use with S3-compatible services (MinIO)

This strategy will also work with any S3-compatible object storage solutions, such as MinIO. See the configureS3AssetStorage for an example with MinIO.

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>

S3Config

Configuration for connecting to AWS S3.

Signature

interface S3Config {
  credentials?: S3Credentials | S3CredentialsProfile;
  bucket: string;
  nativeS3Configuration?: any;
  nativeS3UploadConfiguration?: 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.

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.

nativeS3UploadConfiguration

property
type:
any
Configuration object passed directly to the AWS SDK. ManagedUpload.ManagedUploadOptions 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

import { AssetServerPlugin, configureS3AssetStorage } from '@vendure/asset-server-plugin';
import { DefaultAssetNamingStrategy } from '@vendure/core';

// ...

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

Usage with MinIO

Reference: How to use AWS SDK for Javascript with MinIO Server

Example

import { AssetServerPlugin, configureS3AssetStorage } from '@vendure/asset-server-plugin';
import { DefaultAssetNamingStrategy } from '@vendure/core';

// ...

plugins: [
  AssetServerPlugin.init({
    route: 'assets',
    assetUploadDir: path.join(__dirname, 'assets'),
    namingStrategy: new DefaultAssetNamingStrategy(),
    storageStrategyFactory: configureS3AssetStorage({
      bucket: 'my-minio-bucket',
      credentials: {
        accessKeyId: process.env.MINIO_ACCESS_KEY_ID,
        secretAccessKey: process.env.MINIO_SECRET_ACCESS_KEY,
      },
      nativeS3Configuration: {
        endpoint: process.env.MINIO_ENDPOINT ?? 'http://localhost:9000',
        s3ForcePathStyle: true,
        signatureVersion: 'v4',
      },
    }),
}),

Signature

function configureS3AssetStorage(s3Config: S3Config): void

Parameters

s3Config

parameter
type:
S3Config