diff --git a/.env.example b/.env.example index 5abc8c1..bfb219b 100644 --- a/.env.example +++ b/.env.example @@ -21,6 +21,7 @@ LOCAL_BLOB_DIR=./local-blobs # AWS_SECRET_ACCESS_KEY= # AWS_REGION=us-east-1 # S3_BUCKET=locker +# AWS_ENDPOINT_URL= (optional, for S3-compatible services) # Cloudflare R2 (when provider=r2) # R2_ACCOUNT_ID= diff --git a/packages/storage/src/s3.ts b/packages/storage/src/s3.ts index 5467487..1086e42 100644 --- a/packages/storage/src/s3.ts +++ b/packages/storage/src/s3.ts @@ -23,14 +23,17 @@ export interface S3StorageConfig { export class S3StorageAdapter implements StorageProvider { private client: S3Client; private bucket: string; + private region: string; + private endpoint?: string; readonly supportsPresignedUpload = true; constructor(config?: S3StorageConfig) { - const region = config?.region ?? process.env.AWS_REGION ?? "us-east-1"; + this.region = config?.region ?? process.env.AWS_REGION ?? "us-east-1"; + this.endpoint = config?.endpoint ?? process.env.AWS_ENDPOINT_URL; this.client = new S3Client({ - region, - ...(config?.endpoint ? { endpoint: config.endpoint } : {}), + region: this.region, + ...(this.endpoint ? { endpoint: this.endpoint } : {}), credentials: { accessKeyId: config?.accessKeyId ?? process.env.AWS_ACCESS_KEY_ID!, secretAccessKey: @@ -38,6 +41,7 @@ export class S3StorageAdapter implements StorageProvider { }, requestChecksumCalculation: "WHEN_REQUIRED", responseChecksumValidation: "WHEN_REQUIRED", + forcePathStyle: !!this.endpoint, // Required for S3-compatible services with custom endpoints }); this.bucket = config?.bucket ?? process.env.S3_BUCKET ?? "locker"; } @@ -73,8 +77,11 @@ export class S3StorageAdapter implements StorageProvider { }), ); + const baseURL = this.endpoint ? `${this.endpoint}/${this.bucket}` : + `https://${this.bucket}.s3.${this.region}.amazonaws.com`; + return { - url: `https://${this.bucket}.s3.${process.env.AWS_REGION ?? "us-east-1"}.amazonaws.com/${params.path}`, + url: `${baseURL}/${params.path}`, path: params.path, }; } diff --git a/turbo.json b/turbo.json index aadb05f..783caa6 100644 --- a/turbo.json +++ b/turbo.json @@ -12,6 +12,7 @@ "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_REGION", + "AWS_ENDPOINT_URL", "S3_BUCKET", "R2_ACCOUNT_ID", "R2_ACCESS_KEY_ID",