47 lines
1.4 KiB
TypeScript
47 lines
1.4 KiB
TypeScript
|
|
import { S3Client, PutObjectCommand, GetObjectCommand, DeleteObjectCommand } from "@aws-sdk/client-s3";
|
||
|
|
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
||
|
|
|
||
|
|
const MINIO_ENDPOINT = process.env.MINIO_ENDPOINT || "192.168.68.105";
|
||
|
|
const MINIO_PORT = parseInt(process.env.MINIO_PORT || "9000");
|
||
|
|
const MINIO_ACCESS_KEY = process.env.MINIO_ACCESS_KEY || "minioadmin";
|
||
|
|
const MINIO_SECRET_KEY = process.env.MINIO_SECRET_KEY || "";
|
||
|
|
const MINIO_BUCKET = process.env.MINIO_BUCKET || "echos-ocr";
|
||
|
|
|
||
|
|
export const s3 = new S3Client({
|
||
|
|
endpoint: `http://${MINIO_ENDPOINT}:${MINIO_PORT}`,
|
||
|
|
region: "us-east-1",
|
||
|
|
credentials: {
|
||
|
|
accessKeyId: MINIO_ACCESS_KEY,
|
||
|
|
secretAccessKey: MINIO_SECRET_KEY,
|
||
|
|
},
|
||
|
|
forcePathStyle: true,
|
||
|
|
});
|
||
|
|
|
||
|
|
export const BUCKET = MINIO_BUCKET;
|
||
|
|
|
||
|
|
export async function uploadBuffer(key: string, buffer: Buffer, contentType: string): Promise<string> {
|
||
|
|
await s3.send(
|
||
|
|
new PutObjectCommand({
|
||
|
|
Bucket: BUCKET,
|
||
|
|
Key: key,
|
||
|
|
Body: buffer,
|
||
|
|
ContentType: contentType,
|
||
|
|
})
|
||
|
|
);
|
||
|
|
return key;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function getPresignedUrl(key: string, expiresIn = 3600): Promise<string> {
|
||
|
|
return getSignedUrl(
|
||
|
|
s3,
|
||
|
|
new GetObjectCommand({ Bucket: BUCKET, Key: key }),
|
||
|
|
{ expiresIn }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function deleteObject(key: string): Promise<void> {
|
||
|
|
await s3.send(
|
||
|
|
new DeleteObjectCommand({ Bucket: BUCKET, Key: key })
|
||
|
|
);
|
||
|
|
}
|