简体   繁体   中英

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

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!

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: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM