Skip to content

Commit

Permalink
chore: update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinAndreev committed Jan 5, 2024
1 parent 6997712 commit 5c4e8ba
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
66 changes: 66 additions & 0 deletions site/docs/prefixing.md
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/',
}),
})
```
2 changes: 1 addition & 1 deletion site/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
const sidebars = {
docsSidebar: {
Introduction: ['getting-started'],
Services: ['buckets', 'objects', 'signed-url', 'download-helper', 'deletion-helper'],
Services: ['buckets', 'objects', 'signed-url', 'download-helper', 'prefix', 'deletion-helper'],
API: [
{
type: 'autogenerated',
Expand Down

0 comments on commit 5c4e8ba

Please sign in to comment.