Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

@tus/s3-store: add s3Client option #727

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 5 additions & 0 deletions .changeset/shy-buckets-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tus/s3-store": minor
---

Add `s3Client` option. This allows user to provide existing S3 client to datastore.
4 changes: 4 additions & 0 deletions packages/s3-store/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ Options to pass to the AWS S3 SDK. Checkout the
docs for the supported options. You need to at least set the `region`, `bucket` name, and
your preferred method of authentication.

### `options.s3Client`

Option to pass existing instance of S3 client that should be used. This option has priority over `options.s3ClientConfig`, but you still need to use `options.s3ClientConfig` to set `bucket` name.

#### `options.expirationPeriodInMilliseconds`

Enables the expiration extension and sets the expiration period of an upload url in
Expand Down
3 changes: 2 additions & 1 deletion packages/s3-store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export type Options = {
expirationPeriodInMilliseconds?: number
// Options to pass to the AWS S3 SDK.
s3ClientConfig: S3ClientConfig & {bucket: string}
s3Client?: AWS.S3
}

export type MetadataValue = {
Expand Down Expand Up @@ -127,7 +128,7 @@ export class S3Store extends DataStore {
this.expirationPeriodInMilliseconds = options.expirationPeriodInMilliseconds ?? 0
this.useTags = options.useTags ?? true
this.cache = options.cache ?? new MemoryKvStore<MetadataValue>()
this.client = new S3(restS3ClientConfig)
this.client = options.s3Client ?? new S3(restS3ClientConfig)
this.partUploadSemaphore = new Semaphore(options.maxConcurrentPartUploads ?? 60)
}

Expand Down
20 changes: 20 additions & 0 deletions packages/s3-store/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import assert from 'node:assert/strict'
import {Readable} from 'node:stream'

import sinon from 'sinon'
import { S3 } from '@aws-sdk/client-s3'

import {S3Store} from '../src'
import * as shared from 'test/stores.test'
Expand Down Expand Up @@ -294,6 +295,25 @@ describe('S3DataStore', () => {
assert.equal(store.maxMultipartParts, customMaxParts)
})

it('should use custom S3 client when specified', async () => {
const client = new S3(s3ClientConfig);
const store = new S3Store({
s3Client: client,
s3ClientConfig,
});

const clientMock = sinon.mock(client);
clientMock.expects("putObject").once();

await store.create(new Upload({
id: shared.testId('test-upload'),
size: 10 * 1024 * 1024,
offset: 0,
}))

clientMock.verify();
})

shared.shouldHaveStoreMethods()
shared.shouldCreateUploads()
shared.shouldRemoveUploads() // Termination extension
Expand Down
Loading