feat: add util function delete folder minio

This commit is contained in:
Methapon Metanipat 2024-08-20 15:20:14 +07:00
parent 8474a6c869
commit b167a612b4

View file

@ -10,6 +10,28 @@ const minio = new Client({
export default minio;
export async function deleteFolder(bucket: string, path: string, wait = false) {
return new Promise<string[]>((resolve, reject) => {
const item: string[] = [];
const stream = minio.listObjectsV2(bucket, path);
stream.on("data", (v) => v && v.name && item.push(v.name));
stream.on("end", () => resolve(item));
stream.on("error", () => reject(new Error("MinIO error.")));
}).then(async (list) => {
const promises = list.map(async (v) =>
minio.removeObject(bucket, v, {
forceDelete: true,
}),
);
if (wait) await Promise.allSettled(promises);
return list;
});
}
// minio typescript does not support include version
type BucketItemWithVersion = {
name: string;