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)
async init() => ;
destroy?: (() => void | Promise<void>) | undefined;
async writeFileFromBuffer(fileName: string, data: Buffer) => ;
async writeFileFromStream(fileName: string, data: Readable) => ;
async readFileToBuffer(identifier: string) => ;
async readFileToStream(identifier: string) => ;
async deleteFile(identifier: string) => ;
async fileExists(fileName: string) => ;
}
Implements
Members
constructor
(s3Config: S3Config, toAbsoluteUrl: (request: Request, identifier: string) => string) => S3AssetStorageStrategy
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) =>
S3Config
Configuration for connecting to AWS S3.
Signature
interface S3Config {
credentials: AwsCredentialIdentity | AwsCredentialIdentityProvider;
bucket: string;
nativeS3Configuration?: any;
nativeS3UploadConfiguration?: any;
}
Members
credentials
AwsCredentialIdentity | AwsCredentialIdentityProvider
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
string
nativeS3Configuration
any
any
in order to avoid the need to include aws-sdk
dependency in general.
nativeS3UploadConfiguration
any
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 AssetServerOptionsstorageStrategyFactory
property.
Before using this strategy, make sure you have the @aws-sdk/client-s3
and @aws-sdk/lib-storage
package installed:
npm install
*Example*
```TypeScript
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
S3Config