feat: add minio package
This commit is contained in:
parent
1899fa06e7
commit
5b07a2989b
4 changed files with 779 additions and 30 deletions
45
src/services/minio.ts
Normal file
45
src/services/minio.ts
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
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 });
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue