feat: add minio util function
This commit is contained in:
parent
5d6b61b46f
commit
f861edc6ba
1 changed files with 37 additions and 0 deletions
|
|
@ -10,6 +10,43 @@ const minio = new Client({
|
||||||
|
|
||||||
export default minio;
|
export default minio;
|
||||||
|
|
||||||
|
// minio typescript does not support include version
|
||||||
|
type BucketItemWithVersion = {
|
||||||
|
name: string;
|
||||||
|
lastModified: string;
|
||||||
|
etag: string;
|
||||||
|
size: number;
|
||||||
|
versionId: string;
|
||||||
|
isLatest: boolean;
|
||||||
|
isDeleteMarker: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function listObjectVersion(bucket: string, obj: string) {
|
||||||
|
return await new Promise<BucketItemWithVersion[]>((resolve, reject) => {
|
||||||
|
const data: BucketItemWithVersion[] = [];
|
||||||
|
// @ts-ignore
|
||||||
|
let stream = minio.listObjects(bucket, obj, true, {
|
||||||
|
IncludeVersion: true, // type error (ts not support) - expected 3 args but got 4
|
||||||
|
});
|
||||||
|
stream.on("data", (obj) => data.push(obj as unknown as BucketItemWithVersion));
|
||||||
|
stream.on("error", (err) => reject(err));
|
||||||
|
stream.on("end", () => resolve(data));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteObjectAllVersion(bucket: string, obj: string) {
|
||||||
|
const item = await listObjectVersion(bucket, obj);
|
||||||
|
|
||||||
|
return await new Promise((resolve, reject) => {
|
||||||
|
minio.removeObjects(
|
||||||
|
bucket,
|
||||||
|
// @ts-ignore
|
||||||
|
item.map(({ name, versionId }) => ({ name, versionId })), // type error (ts not support) - expected "string[]"
|
||||||
|
(e) => (e && reject(e)) || resolve(true),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export async function presignedGetObjectIfExist(bucket: string, obj: string, exp?: number) {
|
export async function presignedGetObjectIfExist(bucket: string, obj: string, exp?: number) {
|
||||||
if (
|
if (
|
||||||
await minio.statObject(bucket, obj).catch((e) => {
|
await minio.statObject(bucket, obj).catch((e) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue