nest-azure-storage
v1.0.3
Published
An Azure Storage module for Nest framework (node.js)
Readme
Description
Azure Storage module for Nest framework (node.js)
Tutorial
Learn how to get started with Azure table storage for NestJS
Before Installation
- Create a Storage account and resource (read more)
- In the Azure Portal, go to Dashboard > Storage > your-storage-account.
- Note down the "AccountName", "AccountKey" obtained at Access keys and "AccountSAS" from Shared access signature under Settings tab.
Configuration
- Install the package using NPM:
$ npm i -S nest-azure-storage- Create or update your existing
.envfile with the following content:
# See: http://bit.ly/azure-storage-sas-key
AZURE_STORAGE_SAS_KEY=
# See: http://bit.ly/azure-storage-account
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
IMPORTANT: Make sure to add your
.envfile to your.gitignore! The.envfile MUST NOT be versionned on Git.Make sure to include 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!
- Import the
AzureStorageModulewith 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
containerNamename for the whole module, this will apply to all controllers withing this module. You can also provide (override) thecontainerNamein 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 'nest-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 'nest-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 'nest-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');
}
}Stay in touch
- Author - Vishal Isharani
- LinkedIn - Vishal Isharani
