45 lines
1.5 KiB
TypeScript
45 lines
1.5 KiB
TypeScript
import { Client } from "minio";
|
|
|
|
if (!process.env.BACKUP_MINIO_BUCKET) {
|
|
throw Error("Require MinIO bucket.");
|
|
}
|
|
|
|
const BACKUP_MINIO_BUCKET = process.env.BACKUP_MINIO_BUCKET;
|
|
|
|
const minio = new Client({
|
|
endPoint: process.env.MINIO_HOST ?? "localhost",
|
|
port: process.env.MINIO_PORT ? +process.env.MINIO_PORT : undefined,
|
|
useSSL: /true/.test(process.env.MINIO_SSL || "false"),
|
|
accessKey: process.env.MINIO_ACCESS_KEY ?? "",
|
|
secretKey: process.env.MINIO_SECRET_KEY ?? "",
|
|
});
|
|
|
|
export default minio;
|
|
|
|
export async function listFile(path: string) {
|
|
return await new Promise<string[]>((resolve, reject) => {
|
|
const item: string[] = [];
|
|
|
|
const stream = minio.listObjectsV2(BACKUP_MINIO_BUCKET, path);
|
|
|
|
stream.on("data", (v) => v && v.name && item.push(v.name.split("/").at(-1) || ""));
|
|
stream.on("end", () => resolve(item));
|
|
stream.on("error", () => reject(new Error("MinIO error.")));
|
|
});
|
|
}
|
|
|
|
export async function getPresigned(method: string, path: string, exp = 60 * 60) {
|
|
return await minio.presignedUrl(method, BACKUP_MINIO_BUCKET, path, exp);
|
|
}
|
|
|
|
export async function getFile(path: string, exp = 60 * 60) {
|
|
return await minio.presignedGetObject(BACKUP_MINIO_BUCKET, path, exp);
|
|
}
|
|
|
|
export async function setFile(path: string, exp = 6 * 60 * 60) {
|
|
return await minio.presignedPutObject(BACKUP_MINIO_BUCKET, path, exp);
|
|
}
|
|
|
|
export async function deleteFile(path: string) {
|
|
await minio.removeObject(BACKUP_MINIO_BUCKET, path, { forceDelete: true });
|
|
}
|