Search code examples
nestjsazure-blob-storage

How to upload files(images, pdf) using Azure blob storage with standard account?


I am using azure blob storage standard account. When I try to upload files, it throws the error 'BlobTypeNotSupported'. I know standard account doesn't support 'Block Blobs'. Is there any other way to upload files using standard account?


Solution

  • I tried in my environment and got below results:

    I created a standard storage account through azure portal :

    enter image description here

    I used the below code to upload images using nest.js

    Code:

    import { BlobServiceClient, BlockBlobClient } from  '@azure/storage-blob';
    import { Injectable } from  '@nestjs/common';
    
    @Injectable()
    export  class  AppService {
    azureConnection = "< Connection string >";
    containerName = "test";
    
    getBlobClient(imageName:string):BlockBlobClient{
    const  blobClientService = BlobServiceClient.fromConnectionString(this.azureConnection);
    const  containerClient = blobClientService.getContainerClient(this.containerName);
    const  blobClient = containerClient.getBlockBlobClient(imageName);
    return  blobClient;
    }
    
    async  upload(file:Express.Multer.File){
    const  blobClient = this.getBlobClient(file.originalname);
    await  blobClient.uploadData(file.buffer);
    }
    }
    

    console:

    enter image description here

    Postman: You can use the post request to upload image via postman.

    enter image description here

    Portal:

    The above request worked successfully and uploaded file through azure storage standard account using nest.js with Block-blob type.

    enter image description here

    Reference:

    NestJS API File Operations Using Azure Blob Storage (learmoreseekmore.com) by Naveen.