import { S3Client } from '@aws-sdk/client-s3'; import { S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY, S3_BUCKET } from '$env/static/private'; import { ListObjectsV2Command } from '@aws-sdk/client-s3'; import type { ListObjectsV2CommandOutput } from '@aws-sdk/client-s3'; import { HeadObjectCommand } from '@aws-sdk/client-s3'; const s3 = new S3Client({ region: 'us-east-1', // MinIO ignores region but AWS SDK requires it endpoint: S3_ENDPOINT, credentials: { accessKeyId: S3_ACCESS_KEY, secretAccessKey: S3_SECRET_KEY }, forcePathStyle: true // needed for MinIO }); /** * Returns the total storage used by a user (in bytes) by summing all S3 objects under their userId prefix. */ export async function getUserStorageUsage(userId: string): Promise { const BUCKET = S3_BUCKET!; try { const result = await s3.send(new HeadObjectCommand({ Bucket: BUCKET, Key: userId, })); return result.ContentLength || 0; } catch (err: any) { // If the file does not exist, return 0 if (err.name === 'NotFound' || err.$metadata?.httpStatusCode === 404) { return 0; } throw err; } } export default s3;