Search code examples
nestjsazure-blob-storage

Azure Blob Storage and NestJs: The requested URI does not represent any resource on the server


Issue When sending request to create blob files, I got this error message:

The requested URI does not represent any resource on the server.

Background

I have Azure Blob Storage set up and have the following data:

  • Account name
  • Access key
  • Connection string
  • Connection URL
  • Container. Example: Container name: "TestContainer"

The above was also used to configure Azure Blob Storage connection in my NestJS project.

I want to create files inside the "TestContainer".

Currently, I'm using ContainerClient from @azure/storage-blob to create blob files. Here is the code

import { ContainerClient } from '@azure/storage-blob';

@Injectable()
export class BlobStorageService {
  constructor(
    @Inject('CONTAINER_CLIENT') private readonly containerClient: ContainerClient
  ) {}

  async create(key: string, message: any) {
    const content = JSON.stringify(message);
    try {
      const blobName = key;
      const appendBlobClient = this.containerClient.getAppendBlobClient(blobName);
      await appendBlobClient.createIfNotExists();

      await appendBlobClient.appendBlock(content, content.length);
    } catch (error) {
      throw error
    }
  }
}

Please help if you have experienced this before. Thank you!


Solution

  • The requested URI does not represent any resource on the server.

    The above error occurs either you are passing Invalid URI or incorrect URI.

    As a workaround to upload files to azure container.I tried in my environment with blobserviceclient and Blockblobclient using following Document. by Naveen.

    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:

    enter image description here

    Portal:

    enter image description here

    Reference: How to Upload Files to Azure using NestJS and typeORM with MySQL (freecodecamp.org) by Destiny Erhabor.