fix(media): defer Bun S3 client loading

This commit is contained in:
2026-08-29 05:42:32 +00:00 Unverified
parent cf71099b69
commit 9fe9adc6f2
3 changed files with 12 additions and 9 deletions
+8 -5
View File
@@ -1,8 +1,9 @@
import "server-only";
import { S3Client } from "bun";
import type { S3Client } from "bun";
let client: S3Client | undefined;
let loading: Promise<S3Client> | undefined;
function required(name: string): string {
const value = process.env[name];
@@ -10,16 +11,18 @@ function required(name: string): string {
return value;
}
export function getMediaStorage(): S3Client {
if (!client) {
client = new S3Client({
export async function getMediaStorage(): Promise<S3Client> {
if (client) return client;
if (!loading) {
loading = import("bun").then(({ S3Client: BunS3Client }) => new BunS3Client({
accessKeyId: required("S3_ACCESS_KEY_ID"),
secretAccessKey: required("S3_SECRET_ACCESS_KEY"),
bucket: required("S3_BUCKET"),
endpoint: required("S3_ENDPOINT"),
region: process.env.S3_REGION || "auto",
virtualHostedStyle: process.env.S3_VIRTUAL_HOSTED_STYLE === "true",
});
}));
}
client = await loading;
return client;
}