diff --git a/README.md b/README.md index 1813dd8..6e33585 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/src/S3Handler.ts b/src/S3Handler.ts index 71e94a4..533f251 100644 --- a/src/S3Handler.ts +++ b/src/S3Handler.ts @@ -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'; @@ -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; } diff --git a/test/src/createHandler.ts b/test/src/createHandler.ts new file mode 100644 index 0000000..9d28230 --- /dev/null +++ b/test/src/createHandler.ts @@ -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(); + }); +});