Skip to content

Commit

Permalink
feat: added deleteObject to S3 API
Browse files Browse the repository at this point in the history
  • Loading branch information
finnmich committed Dec 20, 2023
1 parent c7d94f9 commit e260138
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/s3/S3Bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,8 @@ export class S3Bucket {
StartAfter: startAfter,
});
}

async deleteObject(key: string) {
return await this.s3.deleteObject({Bucket: this.name, Key: key});
}
}
31 changes: 31 additions & 0 deletions test/s3.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import rand from 'randomstring';
import {S3Bucket, configure} from '../src';
import {NoSuchKey} from '@aws-sdk/client-s3';

const localstackEndpoint =
process.env.LOCALSTACK_ENDPOINT || 'http://localhost:4566';
Expand Down Expand Up @@ -59,6 +60,36 @@ describe('getExistingBucket', () => {
});
});

describe('deleteObject', () => {
it('should be able to delete object', async () => {
const bucket = await S3Bucket.getOrCreateBucket(
generateRandomBucketName(),
localstackEndpoint
);
const buffer = Buffer.from([8, 6, 7, 5, 3, 0, 9]);
await bucket!.putObject('test', buffer, 'image/png');
const buffer2 = Buffer.from([8, 6, 7, 5, 3, 0, 9]);
await bucket!.putObject('test2', buffer2, 'image/png');

let list = await bucket?.listObjects();
expect(list?.Contents?.length).toBe(2);

await bucket!.deleteObject('test');

list = await bucket?.listObjects();
expect(list?.Contents?.length).toBe(1);

expect(async () => {
await bucket?.getObject('test');
}).rejects.toThrow(NoSuchKey);

await bucket!.deleteObject('test2');

list = await bucket?.listObjects();
expect(list?.Contents).toBe(undefined);
});
});

describe('deleteIfExsists', () => {
it('should be able to delete bucket', async () => {
const testBucketName = generateRandomBucketName();
Expand Down

0 comments on commit e260138

Please sign in to comment.