diff --git a/src/index.ts b/src/index.ts index 168c0207..36fc9fb6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,3 +4,4 @@ export * from './s3.module'; export * from './services'; export * from './types'; export * from './utils'; +export * from './interfaces'; diff --git a/src/interfaces/prefix-algorithm.interface.ts b/src/interfaces/prefix-algorithm.interface.ts index 9283e592..468d1085 100644 --- a/src/interfaces/prefix-algorithm.interface.ts +++ b/src/interfaces/prefix-algorithm.interface.ts @@ -1,3 +1,3 @@ export interface IPrefixAlgorithm { - prefix(remote: string, prefix?: string, bucket?: string): string; + prefix(remote: string, prefix?: string, bucket?: string, context?: any): string; } diff --git a/src/services/objects.service.ts b/src/services/objects.service.ts index 8c99d2a8..940f1ba3 100644 --- a/src/services/objects.service.ts +++ b/src/services/objects.service.ts @@ -46,7 +46,7 @@ export class ObjectsService { new PutObjectCommand({ Bucket: bucket, Body: body, - Key: disableAutoPrefix ? remote : this.prefixService.prefix(remote, bucket), + Key: disableAutoPrefix ? remote : this.prefixService.prefix(remote, bucket, options?.prefixContext), ...preparedOptions, }), ); @@ -73,7 +73,7 @@ export class ObjectsService { return this.client.send( new DeleteObjectCommand({ Bucket: bucket, - Key: disableAutoPrefix ? remote : this.prefixService.prefix(remote, bucket), + Key: disableAutoPrefix ? remote : this.prefixService.prefix(remote, bucket, options?.prefixContext), ...preparedOptions, }), ); @@ -90,7 +90,9 @@ export class ObjectsService { new DeleteObjectsCommand({ Bucket: bucket, Delete: { - Objects: remotes.map((r) => ({ Key: disableAutoPrefix ? r : this.prefixService.prefix(r, bucket) })), + Objects: remotes.map((r) => ({ + Key: disableAutoPrefix ? r : this.prefixService.prefix(r, bucket, options?.prefixContext), + })), }, ...preparedOptions, }), @@ -103,7 +105,7 @@ export class ObjectsService { return this.client.send( new GetObjectCommand({ Bucket: bucket, - Key: disableAutoPrefix ? remote : this.prefixService.prefix(remote, bucket), + Key: disableAutoPrefix ? remote : this.prefixService.prefix(remote, bucket, options?.prefixContext), ...preparedOptions, }), ); diff --git a/src/services/prefix.service.ts b/src/services/prefix.service.ts index 19fcf8d3..d061c129 100644 --- a/src/services/prefix.service.ts +++ b/src/services/prefix.service.ts @@ -10,9 +10,9 @@ export class PrefixService { @Inject(PREFIX_ALGORITHM) private readonly prefixAlgorithm: IPrefixAlgorithm, ) {} - public prefix(remote: string, bucket?: string): string { + public prefix(remote: string, bucket?: string, context?: any): string { const { prefix } = this.config; - return this.prefixAlgorithm.prefix(remote, prefix, bucket); + return this.prefixAlgorithm.prefix(remote, prefix, bucket, context); } } diff --git a/src/services/signed-url.service.ts b/src/services/signed-url.service.ts index 4a23f709..76fe68cc 100644 --- a/src/services/signed-url.service.ts +++ b/src/services/signed-url.service.ts @@ -26,7 +26,7 @@ export class SignedUrlService { options?: PutObjectOptions, ): Promise { const { disableAutoPrefix, options: preparedOptions } = prepareOptions(options); - const key = disableAutoPrefix ? remote : this.prefixService.prefix(remote, bucket); + const key = disableAutoPrefix ? remote : this.prefixService.prefix(remote, bucket, options?.prefixContext); const command = new PutObjectCommand({ Bucket: bucket, @@ -54,7 +54,7 @@ export class SignedUrlService { const command = new GetObjectCommand({ Bucket: bucket, - Key: disableAutoPrefix ? remote : this.prefixService.prefix(remote, bucket), + Key: disableAutoPrefix ? remote : this.prefixService.prefix(remote, bucket, options?.prefixContext), ...preparedOptions, }); @@ -73,7 +73,7 @@ export class SignedUrlService { const command = new DeleteObjectCommand({ Bucket: bucket, - Key: disableAutoPrefix ? remote : this.prefixService.prefix(remote, bucket), + Key: disableAutoPrefix ? remote : this.prefixService.prefix(remote, bucket, options?.prefixContext), ...preparedOptions, }); @@ -93,7 +93,9 @@ export class SignedUrlService { const command = new DeleteObjectsCommand({ Bucket: bucket, Delete: { - Objects: remotes.map((r) => ({ Key: disableAutoPrefix ? r : this.prefixService.prefix(r, bucket) })), + Objects: remotes.map((r) => ({ + Key: disableAutoPrefix ? r : this.prefixService.prefix(r, bucket, options?.prefixContext), + })), }, ...preparedOptions, }); diff --git a/src/types/index.ts b/src/types/index.ts index 2a686270..d3baecd9 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -2,5 +2,5 @@ export * from './download-options.type'; export * from './object-command-options.type'; export * from './s3-config.type'; export * from './signed-url.type'; -export * from './disable-auto-prefix.type'; +export * from './prefix.type'; export * from './prefix-algorithm.type'; diff --git a/src/types/object-command-options.type.ts b/src/types/object-command-options.type.ts index b7b25f4e..d4ecdfe9 100644 --- a/src/types/object-command-options.type.ts +++ b/src/types/object-command-options.type.ts @@ -6,12 +6,16 @@ import { ListObjectsV2CommandInput, PutObjectCommandInput, } from '@aws-sdk/client-s3'; -import { DisableAutoPrefix } from './disable-auto-prefix.type'; +import { DisableAutoPrefix, PrefixContext } from './prefix.type'; -export type GetObjectOptions = Omit & DisableAutoPrefix; -export type DeleteObjectsOptions = Omit & DisableAutoPrefix; -export type PutObjectOptions = Omit & DisableAutoPrefix; -export type DeleteObjectOptions = Omit & DisableAutoPrefix; +export type GetObjectOptions = Omit & DisableAutoPrefix & PrefixContext; +export type DeleteObjectsOptions = Omit & + DisableAutoPrefix & + PrefixContext; +export type PutObjectOptions = Omit & + DisableAutoPrefix & + PrefixContext; +export type DeleteObjectOptions = Omit & DisableAutoPrefix & PrefixContext; export type ListObjectsOptions = Omit; export type ListObjectsV2Options = Omit; export type OptionsWithAutoPrefix = PutObjectOptions | DeleteObjectOptions | DeleteObjectsOptions | GetObjectOptions; diff --git a/src/types/disable-auto-prefix.type.ts b/src/types/prefix.type.ts similarity index 54% rename from src/types/disable-auto-prefix.type.ts rename to src/types/prefix.type.ts index 3eeb42d8..e41b8ce7 100644 --- a/src/types/disable-auto-prefix.type.ts +++ b/src/types/prefix.type.ts @@ -1 +1,3 @@ export type DisableAutoPrefix = { disableAutoPrefix?: boolean }; + +export type PrefixContext = { prefixContext?: any }; diff --git a/src/utils/deletion.service.ts b/src/utils/deletion.service.ts index 352113ba..7bc20337 100644 --- a/src/utils/deletion.service.ts +++ b/src/utils/deletion.service.ts @@ -32,7 +32,7 @@ export class DeletionService { do { data = await this.objectsService.listObjectsV2(bucket, { - Prefix: disableAutoPrefix ? prefix : this.prefixService.prefix(prefix, bucket), + Prefix: disableAutoPrefix ? prefix : this.prefixService.prefix(prefix, bucket, deleteOptions?.prefixContext), ContinuationToken: continuationToken, ...listOptions, });