Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions lib/dln-s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -165,6 +166,37 @@ export class DlnS3 {
}).promise();
}

async deleteDirectory(directory: string): Promise<void> {
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format file

Delete: {
Objects: []
}
};

objects?.Contents?.forEach((content) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will have to check if the content is of type file or directory.

deleteParams.Delete.Objects.push(<S3.ObjectIdentifier>{
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,
Expand Down