Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
15 changes: 11 additions & 4 deletions packages/storage/src/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,25 @@ 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:
config?.secretAccessKey ?? process.env.AWS_SECRET_ACCESS_KEY!,
},
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";
}
Expand Down Expand Up @@ -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`;
Comment on lines +80 to +81
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Trailing slash in endpoint URL

If AWS_ENDPOINT_URL is set with a trailing slash (e.g. https://minio.example.com/), the constructed object URL becomes https://minio.example.com//bucket/path, which will produce broken object links. Consider stripping the trailing slash on construction.

Suggested change
const baseURL = this.endpoint ? `${this.endpoint}/${this.bucket}` :
`https://${this.bucket}.s3.${this.region}.amazonaws.com`;
const baseURL = this.endpoint ? `${this.endpoint.replace(/\/$/, '')}/${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,
};
}
Expand Down
1 change: 1 addition & 0 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down