Is it possible upload file with metadata by storage nestJS sdk?

Viewed 370

I am using Azure storage and Nestjs. I am using Azure storage to store some static files. I can upload files by Nestjs storage SDK successfully.Now I need to upload a file with some custom blob metadata, I have go through the source code of Nestjs storage SDK, but seems there is no predefined way to do this. So is it possible to upload blobs with custom metadata? Or is there any workarounds?

Thanks!

1 Answers

I also have reviewed the source code of azureStorageService, it not provides useful methods. But the upload operation replys a storageUrl with SAS token, we could use it to make another HTTP request: set-blob-metaData to set blob metadata. This is my test code,name is the metadata in my test:

@Post('azure/upload')
  @UseInterceptors(
    AzureStorageFileInterceptor('file', null),
  )
  async UploadedFilesUsingInterceptor(
    @UploadedFile()
    file: UploadedFileMetadata,
    ) {
      file = {
        ...file,
        buffer : Buffer.from('file'),
        originalname: 'somename.txt'
      };
      const storageUrl = await this.azureStorage.upload(file);
      //call rest api to set metadata
      await this.httpService.put(storageUrl + "&comp=metadata",null,{headers:{'x-ms-meta-name':'orginal name here'}})
      .subscribe((response) => {
        console.log(response.status);
      });
   {
    Logger.log(storageUrl);
  }}
}

Result: enter image description here

Related