Skip to content

Commit

Permalink
feat(s3bucket): added support for listing objects in a bucket via lis…
Browse files Browse the repository at this point in the history
…tObjects
  • Loading branch information
finnmich committed Jul 9, 2021
1 parent 8b3ad3f commit 5c300e9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/s3/S3Bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,14 @@ export class S3Bucket {
return false;
}
}

async listObjects(prefix?: string, startAfter?: string) {
return await this.s3
.listObjectsV2({
Bucket: this.name,
Prefix: prefix,
StartAfter: startAfter,
})
.promise();
}
}
37 changes: 33 additions & 4 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ configure({region: 'eu-west-1'});
const testBucketName = 'tibber-tibber-ftw-123321';

describe('getBucket', () => {
beforeEach(async () => {
await S3Bucket.deleteIfExsists(testBucketName);
});

it('should be able to create bucket', async () => {
const result = await S3Bucket.getBucket(testBucketName);
expect(typeof result).toBe('object');
Expand Down Expand Up @@ -88,6 +84,39 @@ describe('getBucket', () => {
result = await bucket.objectAvailable(name);
expect(result).toBe(false);
});

it('should be able to list objects', async () => {
const bucket = await S3Bucket.getBucket(testBucketName);

const res = await bucket.listObjects();

const contents = res.Contents || [];

expect(contents.length).toBeGreaterThan(10);
});

it('should be able to list objects with prefix', async () => {
const bucket = await S3Bucket.getBucket(testBucketName);

const res = await bucket.listObjects('test');

const contents = res.Contents || [];

expect(contents).toHaveLength(2);
});

it('should be able to list after a given key', async () => {
const bucket = await S3Bucket.getBucket(testBucketName);

const buffer = Buffer.from([8, 6, 7, 5, 3, 0, 9]);
await bucket.putObject('test2', buffer);

const res = await bucket.listObjects('test', 'test');

const contents = res.Contents || [];

expect(contents).toHaveLength(1);
});
});

it('should be able to assign several topics to builder', () => {
Expand Down

0 comments on commit 5c300e9

Please sign in to comment.