import "server-only"; import { S3Client } from "@aws-sdk/client-s3"; function env(name: string): string { const value = process.env[name]; if (!value) throw new Error(`Missing environment variable: ${name}`); return value; } export const BUCKET = process.env.RUSTFS_BUCKET ?? "uploads"; const REGION = process.env.RUSTFS_REGION ?? "us-east-1"; let internalClient: S3Client | null = null; let publicClient: S3Client | null = null; const credentials = () => ({ accessKeyId: env("RUSTFS_ACCESS_KEY"), secretAccessKey: env("RUSTFS_SECRET_KEY"), }); export function getInternalClient(): S3Client { internalClient ??= new S3Client({ endpoint: `http://${env("RUSTFS_INTERNAL_HOST")}:${process.env.RUSTFS_INTERNAL_PORT ?? 9000}`, region: REGION, credentials: credentials(), forcePathStyle: true, requestChecksumCalculation: "WHEN_REQUIRED", responseChecksumValidation: "WHEN_REQUIRED", maxAttempts: 3, requestHandler: { connectionTimeout: 3_000, requestTimeout: 15_000 }, }); return internalClient; } export function getPublicClient(): S3Client { publicClient ??= new S3Client({ endpoint: new URL(env("RUSTFS_PUBLIC_URL")).origin, region: REGION, credentials: credentials(), forcePathStyle: true, requestChecksumCalculation: "WHEN_REQUIRED", responseChecksumValidation: "WHEN_REQUIRED", }); return publicClient; }