Add S3 client for Hetzner Object Storage

This commit is contained in:
2026-04-07 11:27:18 +05:00
parent b480b56caf
commit da46f84ab8
2 changed files with 40 additions and 2 deletions
+38
View File
@@ -0,0 +1,38 @@
import { S3Client, PutObjectCommand, DeleteObjectCommand } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
export const s3 = new S3Client({
endpoint: process.env.S3_ENDPOINT!,
region: process.env.S3_REGION ?? "eu-central",
credentials: {
accessKeyId: process.env.S3_ACCESS_KEY!,
secretAccessKey: process.env.S3_SECRET_KEY!,
},
forcePathStyle: true,
});
const BUCKET = process.env.S3_BUCKET!;
export function getPublicUrl(key: string): string {
return `${process.env.S3_ENDPOINT}/${BUCKET}/${key}`;
}
export async function uploadFile(
key: string,
body: Buffer | Uint8Array,
contentType: string
): Promise<string> {
await s3.send(
new PutObjectCommand({
Bucket: BUCKET,
Key: key,
Body: body,
ContentType: contentType,
})
);
return getPublicUrl(key);
}
export async function deleteFile(key: string): Promise<void> {
await s3.send(new DeleteObjectCommand({ Bucket: BUCKET, Key: key }));
}