npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-azure-storage

v1.1.3

Published

<p align="center"> <img src="https://intellipaat.com/blog/wp-content/uploads/2019/06/Graphics-01.jpg" width="250" /> </p>

Downloads

10

Readme

Nestjs-azure-storage is a tiny wrapper on top of azure storage sdks that makes life easier when uploading and deleting files

This module makes it easier to deal with Azure Storage when using Nest.js!

docs: https://github.com/KwobiaMtech/nestjs-azure-storage


Let's get started!


npm i nestjs-azure-storage

yarn add nestjs-azure-storage

You'll need to add nestjs-azure-storage to your module, which you can do like this:

import { Module } from '@nestjs/common';
import { ConfigModule, ConfigType } from '@nestjs/config';
import { AzureStorageModule, AzureStorageOptions } from 'nestjs-azure-storage';

@Module({
  imports: [
      ConfigModule.forRoot({
        envFilePath: ['.env'],
        isGlobal: true,
        load: [globalConfig],
      }),
      AzureStorageModule.forRootAsync({
        useFactory: async (config: ConfigType<typeof globalConfig>) => {
          const opts = {
            containerName: config.azure.container,
            connectionString: config.azure.connectionString,
          } as AzureStorageOptions;
          return opts;
        },
        inject: [globalConfig.KEY],
    }),
  ],

})
export class AppModule {}

Another way to call nestjs-azure-storage in your module can be seen below:

import { Module } from '@nestjs/common';
import { ConfigModule, ConfigType } from '@nestjs/config';
import { AzureStorageModule, AzureStorageOptions } from 'nestjs-azure-storage';

@Module({
  imports: [
      ConfigModule.forRoot({
        envFilePath: ['.env'],
        isGlobal: true,
        load: [globalConfig],
      }),
      AzureStorageModule.forRoot({
        containerName: process.env.containerName,
        connectionString: process.env.connectionString
    })
  ],

})
export class AppModule {}

You can structure your controller as shown below to take advantage of the Azure Storage package:

import { AppService } from './app.service';
import {
  Body,
  Controller,
  Delete,
  Post,
  UploadedFile,
  UseInterceptors,
} from '@nestjs/common';

import { BlobDeleteIfExistsResponse } from '@azure/storage-blob';
import { FileInterceptor } from '@nestjs/platform-express';


@Controller()
export class AppController {
  constructor(private service: AppService) {}

  @Post('upload/file')
  @UseInterceptors(FileInterceptor('file'))
  async uploadFile(
    @UploadedFile() file: Express.Multer.File,
    @Body() req: any,
  ): Promise<{name: string, url: string}> {
    if (!file) throw new Error('File not found');
    file = {
      ...file,
      originalname: file.originalname,
    };
    return await this.service.uploadFile(file);
  }

  @Delete('delete/file')
  async deleteFile(
    @Body() data: {name: string},
  ): Promise<BlobDeleteIfExistsResponse> {
    if (!data.name) throw new Error('File name is required');
    return await this.service.deleteFile(data.name);
  }
}

To use azure storage service, you will need to inject the service into your custom service class as shown below

import { AzureStorageService } from 'nestjs-azure-storage';

constructor(
  private storage: AzureStorageService
) {}

To implement Azure storage service in your custom service class, you can structure your service as show below


import { Injectable } from '@nestjs/common';
import { AzureStorageService } from 'nestjs-azure-storage';
import { BlobDeleteIfExistsResponse } from "@azure/storage-blob";

@Injectable()
export class AppService {
  constructor(private storage: AzureStorageService){}

  async uploadFile(file: Express.Multer.File): Promise<{name: string, url: string}> {
   return this.storage.uploadFile(file);
  }

  async deleteFile(fileName: string): Promise<BlobDeleteIfExistsResponse> {
    return this.storage.deleteFile(fileName);
  }
  
}

Apart from this README, you can find examples of using the library in the following places:

This project is MIT Licensed.