Skip to content

Commit

Permalink
Merge pull request #54 from LabO8/feature/prefix-context
Browse files Browse the repository at this point in the history
feat: add prefix context
  • Loading branch information
MartinAndreev committed Jan 5, 2024
2 parents a2835c7 + 9ce5f20 commit 96d8394
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './s3.module';
export * from './services';
export * from './types';
export * from './utils';
export * from './interfaces';
2 changes: 1 addition & 1 deletion src/interfaces/prefix-algorithm.interface.ts
Original file line number Diff line number Diff line change
@@ -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;
}
10 changes: 6 additions & 4 deletions src/services/objects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
);
Expand All @@ -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,
}),
);
Expand All @@ -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,
}),
Expand All @@ -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,
}),
);
Expand Down
4 changes: 2 additions & 2 deletions src/services/prefix.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
10 changes: 6 additions & 4 deletions src/services/signed-url.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class SignedUrlService {
options?: PutObjectOptions,
): Promise<PutSignedUrl> {
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,
Expand Down Expand Up @@ -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,
});

Expand All @@ -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,
});

Expand All @@ -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,
});
Expand Down
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
14 changes: 9 additions & 5 deletions src/types/object-command-options.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<GetObjectCommandInput, 'Bucket' | 'Key'> & DisableAutoPrefix;
export type DeleteObjectsOptions = Omit<DeleteObjectsCommandInput, 'Bucket' | 'Delete'> & DisableAutoPrefix;
export type PutObjectOptions = Omit<PutObjectCommandInput, 'Bucket' | 'Body' | 'Key'> & DisableAutoPrefix;
export type DeleteObjectOptions = Omit<DeleteObjectCommandInput, 'Bucket' | 'Key'> & DisableAutoPrefix;
export type GetObjectOptions = Omit<GetObjectCommandInput, 'Bucket' | 'Key'> & DisableAutoPrefix & PrefixContext;
export type DeleteObjectsOptions = Omit<DeleteObjectsCommandInput, 'Bucket' | 'Delete'> &
DisableAutoPrefix &
PrefixContext;
export type PutObjectOptions = Omit<PutObjectCommandInput, 'Bucket' | 'Body' | 'Key'> &
DisableAutoPrefix &
PrefixContext;
export type DeleteObjectOptions = Omit<DeleteObjectCommandInput, 'Bucket' | 'Key'> & DisableAutoPrefix & PrefixContext;
export type ListObjectsOptions = Omit<ListObjectsCommandInput, 'Bucket'>;
export type ListObjectsV2Options = Omit<ListObjectsV2CommandInput, 'Bucket'>;
export type OptionsWithAutoPrefix = PutObjectOptions | DeleteObjectOptions | DeleteObjectsOptions | GetObjectOptions;
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export type DisableAutoPrefix = { disableAutoPrefix?: boolean };

export type PrefixContext = { prefixContext?: any };
2 changes: 1 addition & 1 deletion src/utils/deletion.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down

0 comments on commit 96d8394

Please sign in to comment.