Add S3 client for Hetzner Object Storage
This commit is contained in:
+2
-2
@@ -6,8 +6,8 @@ BETTER_AUTH_URL="http://localhost:3000"
|
|||||||
RESEND_API_KEY=""
|
RESEND_API_KEY=""
|
||||||
EMAIL_FROM="noreply@school.second-brain.ru"
|
EMAIL_FROM="noreply@school.second-brain.ru"
|
||||||
|
|
||||||
S3_ENDPOINT="https://fsn1.your-objectstorage.com"
|
S3_ENDPOINT="https://nbg1.your-objectstorage.com"
|
||||||
S3_BUCKET="lms-uploads"
|
S3_BUCKET="second-brain-lms"
|
||||||
S3_ACCESS_KEY=""
|
S3_ACCESS_KEY=""
|
||||||
S3_SECRET_KEY=""
|
S3_SECRET_KEY=""
|
||||||
S3_REGION="eu-central"
|
S3_REGION="eu-central"
|
||||||
|
|||||||
@@ -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 }));
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user