diff --git a/lib/dln-s3.ts b/lib/dln-s3.ts index bce30c9..132465a 100644 --- a/lib/dln-s3.ts +++ b/lib/dln-s3.ts @@ -7,6 +7,7 @@ import { S3Operation } from './helpers/enums/s3-operation.enum'; import * as _ from 'lodash'; import * as fs from 'fs'; import { S3ACL } from './helpers/enums/s3-acl.enum'; +import { S3 } from 'aws-sdk'; export class DlnS3 { private readonly BUCKET: string; @@ -165,6 +166,37 @@ export class DlnS3 { }).promise(); } + async deleteDirectory(directory: string): Promise { + const _s3Agent = new AWS.S3({ + region: this.REGION + }); + + const listParams = { + Bucket: this.BUCKET, + Prefix: `${directory}/` + }; + const objects = await _s3Agent.listObjectsV2(listParams).promise(); + + if (objects?.Contents?.length === 0) return; + + const deleteParams: S3.Types.DeleteObjectsRequest = { + Bucket: this.BUCKET, + Delete: { + Objects: [] + } + }; + + objects?.Contents?.forEach((content) => { + deleteParams.Delete.Objects.push({ + Key: content.Key + }); + }); + + await _s3Agent.deleteObjects(deleteParams).promise(); + + if (objects.IsTruncated) await this.deleteDirectory(directory); + } + private async _upload( data: Buffer | Uint8Array | string | stream.Readable | stream.PassThrough, namespace: string,