Skip to main content

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/client-s3 and @aws-sdk/lib-storage package installed:

npm install @aws-sdk/client-s3 @aws-sdk/lib-storage

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)
init() => ;
destroy?: (() => void | Promise<void>) | undefined;
writeFileFromBuffer(fileName: string, data: Buffer) => ;
writeFileFromStream(fileName: string, data: Readable) => ;
readFileToBuffer(identifier: string) => ;
readFileToStream(identifier: string) => ;
deleteFile(identifier: string) => ;
fileExists(fileName: string) => ;
}

constructor

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

init

method
() =>

destroy

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

writeFileFromBuffer

method
(fileName: string, data: Buffer) =>

writeFileFromStream

method
(fileName: string, data: Readable) =>

readFileToBuffer

method
(identifier: string) =>

readFileToStream

method
(identifier: string) =>

deleteFile

method
(identifier: string) =>

fileExists

method
(fileName: string) =>

S3Config

Configuration for connecting to AWS S3.

Signature
interface S3Config {
credentials: AwsCredentialIdentity | AwsCredentialIdentityProvider;
bucket: string;
nativeS3Configuration?: any;
nativeS3UploadConfiguration?: any;
}

credentials

property
AwsCredentialIdentity | AwsCredentialIdentityProvider

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 To use a shared credentials file, import the fromIni() function from the "@aws-sdk/credential-provider-ini" or "@aws-sdk/credential-providers" package and supply the profile name (e.g. { profile: 'default' }) as its argument.

bucket

property
string

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

nativeS3Configuration

property
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
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/client-s3 and @aws-sdk/lib-storage package installed:

npm install @aws-sdk/client-s3 @aws-sdk/lib-storage

Example

import { AssetServerPlugin, configureS3AssetStorage } from '@vendure/asset-server-plugin';
import { DefaultAssetNamingStrategy } from '@vendure/core';
import { fromEnv } from '@aws-sdk/credential-providers';

// ...

plugins: [
AssetServerPlugin.init({
route: 'assets',
assetUploadDir: path.join(__dirname, 'assets'),
namingStrategy: new DefaultAssetNamingStrategy(),
storageStrategyFactory: configureS3AssetStorage({
bucket: 'my-s3-bucket',
credentials: fromEnv(), // or any other credential provider
nativeS3Configuration: {
region: process.env.AWS_REGION,
},
}),
}),

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',
forcePathStyle: true,
signatureVersion: 'v4',
// The `region` is required by the AWS SDK even when using MinIO,
// so we just use a dummy value here.
region: 'eu-west-1',
},
}),
}),
Signature
function configureS3AssetStorage(s3Config: S3Config): void

Parameters

s3Config

parameter