Azure SDK Javascript dynamic module for NestJS
Let's build a Azure Storage Blob client and inject it into the nest app.
npm install @imaness/azure-sdk-nest @azure/storage-blob
- Register the module with a Blob Storage container client, in
app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AzureSdkModule } from '@imaness/azure-sdk-nest';
import { BlobServiceClient } from '@azure/storage-blob';
import { DefaultAzureCredential } from '@azure/identity';
@Module({
imports: [
// register Blob Service client
AzureSdkModule.register({
client: new ContainerClient(
'https://myaccount.blob.core.windows.net/mycontainer',
new DefaultAzureCredential()
),
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
- use the Blob Storage Container client in
app.controller.ts
import { ContainerClient } from '@azure/storage-blob';
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { InjectAzure } from '@imaness/azure-sdk-nest';
@Controller()
export class AppController {
constructor(
private readonly appService: AppService,
// inject the client
@InjectAzure(ContainerClient) private readonly containerClient: ContainerClient
) {}
@Get()
async helloAzure(): Promise<any> {
const content = "Hello world!";
const blobName = "newblob" + new Date().getTime();
const blockBlobClient = await this.containerClient.getBlockBlobClient('asdsad');
const uploadBlobResponse = await blockBlobClient.upload(content, content.length);
console.log(`Upload block blob ${blobName} successfully`, uploadBlobResponse.requestId);
return uploadBlobResponse;
}
}
- done!
Register a client in any module, you can use any client you want. As long as it's a Azure SDK client
AzureSdkModule.register({
client: new new ContainerClient(
'https://myaccount.blob.core.windows.net/mycontainer',
new DefaultAzureCredential()
),
});
AzureSdkModule.registerAsync({
clientType: ContainerClient,
useFactory: async () => {
const containerClient = new ContainerClient(
'https://myaccount.blob.core.windows.net/mycontainer',
new DefaultAzureCredential()
);
return containerClient;
},
});
Make sure the Client
is the type you registered in module.
import { ContainerClient } from '@azure/storage-blob';
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { InjectAzure } from '@imaness/azure-sdk-nest';
@Controller()
export class AppController {
constructor(
private readonly appService: AppService,
@InjectAzure(ContainerClient) private readonly containerClient: ContainerClient
) {}
@Get()
async helloAzure(): Promise<any> {
const content = "Hello world!";
const blobName = "newblob" + new Date().getTime();
const blockBlobClient = await this.containerClient.getBlockBlobClient('asdsad');
const uploadBlobResponse = await blockBlobClient.upload(content, content.length);
console.log(`Upload block blob ${blobName} successfully`, uploadBlobResponse.requestId);
return uploadBlobResponse;
}
}
Please use key
attribute as the identifier for each Client
Example:
- register the Container Client with a unique
key
AzureSdkModule.register({
// register the Blob container client with key MYACCOUNT-MYCONTAINER-1`
key: 'MYACCOUNT-MYCONTAINER-1',
client: new new ContainerClient(
'https://myaccount.blob.core.windows.net/mycontainer-1',
new DefaultAzureCredential()
),
}),
AzureSdkModule.register({
// register the Blob container client with key MYACCOUNT-MYCONTAINER-2`
key: 'MYACCOUNT-MYCONTAINER-2',
client: new new ContainerClient(
'https://myaccount.blob.core.windows.net/mycontainer-2',
new DefaultAzureCredential()
),
}),
- refer the Blob Storage Container client use
@InjectAzure(Client, key)
@InjectAzure(ContainerClient, 'MYACCOUNT-MYCONTAINER-1') private readonly containerClient1: ContainerClient,
@InjectAzure(ContainerClient, 'MYACCOUNT-MYCONTAINER-2') private readonly containerClient2: ContainerClient,
Set the option isGlobal: true
to enable it
AzureSdkModule.register({
isGlobal: true,
client: new ContainerClient(
'https://myaccount.blob.core.windows.net/mycontainer',
new DefaultAzureCredential()
),
});
Contributor: @imaness
Inspired by: (https://github.com/deligenius/aws-sdk-v3-nest)