0.0.9 • Published 1 year ago

nestjs-blob-storage v0.0.9

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

nestjs-blob-storage

Azure Blob Storage module for Nest.js

Why?

  • Nest.js with @azure/storage-blob

  • @nestjs/azure-storage

    • does not provide the method to upload a file using presigned url
    • does not provide multiple authentication methods
    • does not provide method to upload files directly to Azure without passing through my server

How?

Setup

yarn add nestjs-blob-storage @azure/storage-blob

Options #1

// app.module.ts

import { Module } from '@nestjs/common';
import { BlobStorageModule } from 'nestjs-blob-storage';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [
    BlobStorageModule.forRoot({
      connection: process.env.NEST_AZURE_STORAGE_BLOB_CONNECTION,
      isGlobal: true, // optional
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Option #2

// app.module.ts

import { Module } from '@nestjs/common';
import { BlobStorageModule } from 'nestjs-blob-storage';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [
    BlobStorageModule.forRootAsync({
      useFactory: () => ({
        connection: process.env.NEST_AZURE_STORAGE_BLOB_CONNECTION,
      }),
      isGlobal: true, // optional
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Usage

// app.controller.ts

import { Controller, Get } from '@nestjs/common';
import { BlobStorageService } from 'nestjs-blob-storage';

@Controller()
export class AppController {
  constructor(private readonly blobService: BlobStorageService) {}

  @Get('/')
  async getSas() {
    const containerName = 'mycontainer';
    const fileName = 'test.txt';
    const expiresOn = new Date(new Date().getTime() + 1000 * 60 * 60 * 24);

    const accountSasUrl = await this.blobService.getAccountSasUrl();

    const containerSasUrl = await this.blobService.getContainerSasUrl(
      containerName,
    );

    const blobSasUrl = await this.blobService.getBlockBlobSasUrl(
      containerName,
      fileName,
      { add: true, create: true, read: true, delete: true },
      { expiresOn },
    );

    return { accountSasUrl, containerSasUrl, blobSasUrl };
  }
}
// For example, let's assume that we are in the browser-side.

// Get blob SAS URL which will be endpoint of uploading file
const res = await axios.get('https://example.com/block-blob-sas');
const sasUrl = res.data.blobSasUrl;

// Upload a file directly to Azure Blob Storage which reduces the load on the server
// Make a `FormData` instance and set the file data to upload
const formData = new FormData();
formData.append('file', file);

// Don't forget to set header
const uploadResponse = await axios.put(blobSasUrl, formData, {
  header: { 'x-ms-blob-type': 'BlockBlob' },
});
console.log(uploadReponse.status); // 201 Created

Roadmap

  • add test code
  • add other authentication method link

Reference

Contribution

Install

# to test locally
yarn add link:./path/to/nestjs-blob-storage

Publish

# DO NOT USE YARN: 2FA error occurs when using yarn
npm run release

Test

# set environment variable at ".env.test"
NEST_AZURE_STORAGE_BLOB_CONNECTION="DefaultEndpointsProtocol=https;AccountName=<ACCOUNT_NAME>;AccountKey=<ACCOUNT_KEY>;EndpointSuffix=core.windows.net"