Skip to content
Permalink
Browse files

docs: update readme and add story examples

  • Loading branch information...
manekinekko committed Aug 14, 2019
1 parent 4616986 commit 69c41938c61d7d14fbd280d375a65f4b7ae03fc4
Showing with 161 additions and 1 deletion.
  1. +161 −1 README.md
162 README.md
@@ -34,7 +34,7 @@
1. In the [Azure Portal](https://portal.azure.com), go to **Dashboard > Storage > _your-storage-account_**.
2. Note down the "AccountName", "AccountKey" obtained at **Access keys** and "AccountSAS" from **Shared access signature** under **Settings** tab.

## Installation
## (Recommended) Installation and automatic configuration

Using the Nest CLI:

@@ -50,6 +50,9 @@ You can pass additional flags to customize the post-install schematic. For examp
$ nest add @nestjs/azure-storage --rootDir app
```

When requested, provide the `storageAccountName` and `storageAccountSAS` (see below).
```
Other available flags:
- `rootDir` - Application root directory, default: `src`
@@ -60,6 +63,163 @@ Other available flags:
- `storageAccountName` (required) - The Azure Storage account name (see: http://bit.ly/azure-storage-account)
- `storageAccountSAS` (required) - The Azure Storage SAS Key (see: http://bit.ly/azure-storage-sas-key).
## (Option 2) Manual configuration
1. Install the package using NPM:
```bash
$ npm i -S @nestjs/azure-storage
```

2. Create or update your excisting `.env` file with the following content:

```
AZURE_STORAGE_SAS_KEY=
AZURE_STORAGE_ACCOUNT=
```

> The SAS has the following format: `?sv=2018-03-28&ss=bfqt&srt=sco&sp=rwdlacup&se=2019-12-31T22:54:03Z&st=2019-07-11T13:54:03Z&spr=https,http&sig=WmAl%236251oj11biPK2xcpLs254152H9s0%3D`
3. **IMPORTANT: Make sure to add your `.env` file to your `.gitignore`! The `.env` file MUST NOT be versionned on Git.**

4. Make sure to call the following call to your main file:

```
if (process.env.NODE_ENV !== 'production') require('dotenv').config();
```

> This line must be added before any other imports!
5. Import the `AzureStorageModule` with the following configuration:

```
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AzureStorageModule } from '@nestjs/azure-storage';
@Module({
controllers: [AppController],
providers: [AppService],
imports: [
AzureStorageModule.withConfig({
sasKey: process.env['AZURE_STORAGE_SAS_KEY'],
accountName: process.env['AZURE_STORAGE_ACCOUNT'],
containerName: 'nest-demo-container',
}),
],
})
export class AppModule {}
```

> You may provide a default `containerName` name for the whole module, this will apply to all controllers withing this module. You can also provide (override) the `containerName` in the controller, for each route.
## Story examples

### Store a file using the default container name

```
import {
Controller,
Logger,
Post,
UploadedFile,
UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import {
AzureStorageFileInterceptor,
UploadedFileMetadata,
} from '@nestjs/azure-storage';
@Controller()
export class AppController {
@Post('azure/upload')
@UseInterceptors(
AzureStorageFileInterceptor('file'),
)
UploadedFilesUsingInterceptor(
@UploadedFile()
file: UploadedFileMetadata,
) {
Logger.log(`Storage URL: ${file.storageUrl}`, 'AppController');
}
}
```

### Store a file using a specific container name

```
import {
Controller,
Logger,
Post,
UploadedFile,
UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import {
AzureStorageFileInterceptor,
UploadedFileMetadata,
} from '@nestjs/azure-storage';
@Controller()
export class AppController {
@Post('azure/upload')
@UseInterceptors(
AzureStorageFileInterceptor('file', null, {
containerName: 'nest-demo-container-interceptor',
}),
)
UploadedFilesUsingInterceptor(
@UploadedFile()
file: UploadedFileMetadata,
) {
Logger.log(`Storage URL: ${file.storageUrl}`, 'AppController');
}
}
```


### Store a file using a custom file name

```
import {
Controller,
Logger,
Post,
UploadedFile,
UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import {
AzureStorageFileInterceptor,
AzureStorageService,
UploadedFileMetadata,
} from '@nestjs/azure-storage';
@Controller()
export class AppController {
constructor(private readonly azureStorage: AzureStorageService) {}
@Post('azure/upload')
@UseInterceptors(FileInterceptor('file'))
async UploadedFilesUsingService(
@UploadedFile()
file: UploadedFileMetadata,
) {
file = {
...file,
originalname: 'foo-bar.txt',
};
const storageUrl = await this.azureStorage.upload(file);
Logger.log(`Storage URL: ${storageUrl}`, 'AppController');
}
}
```

## Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).

0 comments on commit 69c4193

Please sign in to comment.
You can’t perform that action at this time.