-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from LabO8/feature/prefix-algorithm-setting
feat: allow custom prefix algorithm implementation
- Loading branch information
Showing
16 changed files
with
205 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
--- | ||
id: prefixing | ||
title: Prefixing | ||
sidebar_label: Prefixing | ||
slug: /prefixing | ||
--- | ||
|
||
## Introduction | ||
|
||
As our app grows, we might want to store our objects in a more organized way. This is where the prefix comes in. | ||
|
||
The prefix is a string that is prepended to the object key. This allows us to organize our objects in a folder-like structure. | ||
|
||
For example, if we have a bucket called `my-bucket` and we want to store our objects in a folder called `my-folder`, we can do that by prepending the prefix `my-folder/` to the object key. | ||
|
||
## Usage | ||
|
||
By default, the prefix is an empty string. This means that the object key is not modified, but if you set a prefix, when you initialize the module, the prefix will be prepended to the object key. | ||
|
||
The default algorithm for prefixing will just prepend the prefix to the object key, but you can also specify a custom algorithm. | ||
|
||
**All services like the `ObjectService` will use the prefix service by default.** | ||
|
||
## Custom prefixing | ||
|
||
In order to use a custom prefixing algorithm, you need to specify the `prefixingAlgorithm` when initializing the module. | ||
|
||
```typescript | ||
class CustomPrefixService implements IPrefixAlgorithm { | ||
prefix(remote: string, prefix: string, bucket?: string): string { | ||
return `${bucket}/${prefix}${remote}`; | ||
} | ||
} | ||
|
||
S3Module.forRoot({ | ||
region: 'region', | ||
accessKeyId: '***', | ||
secretAccessKey: '***', | ||
prefix: 'test/', | ||
prefixAlgorithm: new CustomPrefixService(), | ||
}) | ||
``` | ||
|
||
you can also use injectables | ||
|
||
```typescript | ||
class CustomPrefixWithDIService implements IPrefixAlgorithm { | ||
public constructor(private readonly globalPrefix: string) {} | ||
|
||
prefix(remote: string, prefix: string, bucket?: string): string { | ||
return `${bucket}/${this.globalPrefix}${prefix}${remote}`; | ||
} | ||
} | ||
|
||
S3Module.forRootAsync({ | ||
imports: [SomeModuleThatProvidesTheGlobalPrefix], | ||
prefixAlgorithmInject: ['GLOBAL_PREFIX'], | ||
prefixAlgorithmFactory: (globalPrefix: string) => new CustomPrefixWithDIService(globalPrefix), | ||
useFactory: () => ({ | ||
region: 'region', | ||
accessKeyId: '***', | ||
secretAccessKey: '***', | ||
prefix: 'test/', | ||
}), | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export const S3_CONFIG = 's3.config'; | ||
export const S3_SERVICE = 's3.service'; | ||
|
||
export const PREFIX_ALGORITHM = 'prefix.algorithm'; | ||
export const DEFAULT_EXPIRES_IN = 3600; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './prefix-algorithm.interface'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface IPrefixAlgorithm { | ||
prefix(remote: string, prefix?: string, bucket?: string): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { IPrefixAlgorithm } from '../interfaces'; | ||
|
||
export class DefaultPrefixAlgorithmService implements IPrefixAlgorithm { | ||
prefix(remote: string, prefix?: string, bucket?: string): string { | ||
if (!prefix) { | ||
return remote; | ||
} | ||
|
||
return `${prefix}${remote}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { S3_CONFIG } from '../constants'; | ||
import { PREFIX_ALGORITHM, S3_CONFIG } from '../constants'; | ||
import { S3Config } from '../types'; | ||
import { IPrefixAlgorithm } from '../interfaces'; | ||
|
||
@Injectable() | ||
export class PrefixService { | ||
public constructor(@Inject(S3_CONFIG) private readonly config: S3Config) {} | ||
public constructor( | ||
@Inject(S3_CONFIG) private readonly config: S3Config, | ||
@Inject(PREFIX_ALGORITHM) private readonly prefixAlgorithm: IPrefixAlgorithm, | ||
) {} | ||
|
||
public prefix(remote: string): string { | ||
public prefix(remote: string, bucket?: string): string { | ||
const { prefix } = this.config; | ||
|
||
if (!prefix) { | ||
return remote; | ||
} | ||
|
||
return `${prefix}${remote}`; | ||
return this.prefixAlgorithm.prefix(remote, prefix, bucket); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type PrefixAlgorithm = (remote: string, prefix?: string, bucket?: string) => string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,20 @@ | ||
import { Abstract, Type } from '@nestjs/common'; | ||
import { ModuleMetadata } from '@nestjs/common/interfaces'; | ||
import { PrefixAlgorithm } from './prefix-algorithm.type'; | ||
import { IPrefixAlgorithm } from '../interfaces'; | ||
|
||
export type S3Config = { | ||
region: string; | ||
accessKeyId: string; | ||
secretAccessKey: string; | ||
prefix?: string; | ||
endPoint?: string; | ||
prefixAlgorithm?: IPrefixAlgorithm; | ||
}; | ||
|
||
export type S3AsyncConfig = Pick<ModuleMetadata, 'imports' | 'providers'> & { | ||
useFactory: (...args: any[]) => Promise<S3Config> | S3Config; | ||
useFactory: (...args: any[]) => Promise<Omit<S3Config, 'prefixAlgorithm'>> | Omit<S3Config, 'prefixAlgorithm'>; | ||
inject?: Array<Type<unknown> | string | symbol | Abstract<unknown>>; | ||
prefixAlgorithmFactory?: (...args: any[]) => Promise<IPrefixAlgorithm> | IPrefixAlgorithm; | ||
prefixAlgorithmInject?: Array<Type<unknown> | string | symbol | Abstract<unknown>>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters