Skip to content
Merged
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

[![Coverage Status](https://coveralls.io/repos/github/Sagacify/s3-handler/badge.svg?branch=master)](https://coveralls.io/github/Sagacify/s3-handler?branch=master)
[![npm version](https://img.shields.io/npm/v/@sagacify/s3-handler.svg)](https://www.npmjs.com/package/@sagacify/s3-handler)
[![Issues](https://img.shields.io/github/issues/Sagacify/s3-handler)](https://www.npmjs.com/package/@sagacify/s3-handler)
[![License](https://img.shields.io/github/license/Sagacify/s3-handler)](https://www.npmjs.com/package/@sagacify/s3-handler)
[![Stars](https://img.shields.io/github/stars/Sagacify/s3-handler)](https://www.npmjs.com/package/@sagacify/s3-handler)

## Description

Expand Down Expand Up @@ -50,6 +53,15 @@ await s3Handler.deleteObjects([{ Key: 'my-key1' }, { Key: 'my-key2' }]);
await s3Handler.copyObject('bucket/my-key', 'my-new-key');
```

There is a second way to create your handler.
The static method `createHandler` will allow you to remove the instantiation of the Client on your code. This will use `@aws-sdk/client-s3` under the hood.

```js
import { S3Handler } from '@sagacify/s3-handler';

const s3Handler = new S3Handler.createHandler({ region: 'eu-west-1' }, 'my-bucket-name');
```

### Readable Stream Usage

```js
Expand Down
13 changes: 12 additions & 1 deletion src/S3Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
ObjectIdentifier,
PutObjectCommand,
PutObjectCommandInput,
S3Client
S3Client,
S3ClientConfig
} from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { Configuration, Upload } from '@aws-sdk/lib-storage';
Expand All @@ -31,6 +32,16 @@ export class S3Handler {
this.bucket = bucket;
}

/**
* **Note:** *Credentials can be loaded from the* `~/.aws/config` *file in development*
* @param options - Optional configuration for the S3 client
* @param bucket - The name of the S3 bucket
* @returns an instance of `S3Handler`
*/
static createHandler(options: S3ClientConfig = {}, bucket: string): S3Handler {
return new this(new S3Client(options), bucket);
}

getClient() {
return this.client;
}
Expand Down
18 changes: 18 additions & 0 deletions test/src/createHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect } from 'chai';
import { mockClient } from 'aws-sdk-client-mock';
import { S3Handler } from '../../src/S3Handler';
import { S3Client } from '@aws-sdk/client-s3';

describe('S3Handler.createHandler (static)', () => {
const s3ClientMock = mockClient(S3Client);

beforeEach(() => {
s3ClientMock.reset();
});

it('should succeed when all parameters are provided', async () => {
const create = () => S3Handler.createHandler({}, 'my-bucket');

expect(create).not.throw();
});
});