node.js - 无法使用 @azure/storage-blob 中的 uploadFile 方法设置内容类型(SDK/NPM)

标签 node.js azure content-type azure-blob-storage

无法使用以下代码从 Node 设置 azure 内容类型。它始终将内容类型存储为辛烷流。

const { BlobServiceClient } = require('@azure/storage-blob');

const { AZURE_STORAGE_CONNECTION_STRING } = process.env;

let blobServiceClient;

async function getBlobServiceClient() {
  if (!blobServiceClient) {
    blobServiceClient = await BlobServiceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING);
  }

  return blobServiceClient;
}

async function uploadFile(filePath, containerName) {
  const bsClient = await getBlobServiceClient();
  const containerClient = bsClient.getContainerClient(containerName);
  const blockBlobClient = containerClient.getBlockBlobClient('myImag6.png', { blobHTTPHeaders: { blobContentType: 'image/png' } });

  try {
    const res = await blockBlobClient.uploadFile(filePath);
    console.log(res);
  } catch (error) {
    console.log(error);
  }
}

以下问题似乎与此相关,但我不确定。 https://github.com/Azure/azure-sdk-for-js/issues/6192

请向我提供有关此问题的更多信息以及如何解决此问题。

最佳答案

假设是因为你没有在uploadFile方法中设置BlockBlobUploadOptions,你只在getBlockBlobClient方法中使用它,在我的下面代码测试,可以设置内容类型。

    const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");

    // Enter your storage account name and shared key
    const account = "account name";
    const accountKey = "account key";

    const sharedKeyCredential = new StorageSharedKeyCredential(account, accountKey);
    const blobServiceClient = new BlobServiceClient(
      `https://${account}.blob.core.windows.net`,
      sharedKeyCredential
    );

    const containerName = "test";

    async function main() {
  const containerClient = blobServiceClient.getContainerClient(containerName);


  const blockBlobClient = containerClient.getBlockBlobClient('test.txt');
  const blobOptions = { blobHTTPHeaders: { blobContentType: 'text/plain' } };
  const uploadBlobResponse = await blockBlobClient.uploadFile('E:\\project\\jsstorage\\test.txt',blobOptions);

  console.log(`Upload block blob test.txt successfully`, uploadBlobResponse.requestId);
}

main();

enter image description here

关于node.js - 无法使用 @azure/storage-blob 中的 uploadFile 方法设置内容类型(SDK/NPM),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60222106/

相关文章:

node.js - child_process.exec ffmpeg 无限期挂起

node.js - 如何从 .wav 文件转录完整的音频文本?

java - 当 ReSTLet 应该返回 400 Bad Request 时,它是否返回 415 Unsupported Media Type?

带有重定向的 Node.js 包罗万象的路由总是使用 Angular 呈现索引页面,而不管 url

javascript - 从 Node.js 运行 python 脚本

通过应用程序的 Azure block blob 过期

c# - azure 上的 FFMpeg 函数无法处理同时调用(超过 5 个)

javascript - Rails 3 406 尝试使用 format.js 时出现 Not Acceptable 错误,也无法更改内容类型

c# - HttpClient 设置边界与内容类型

javascript - Node 脚本等待异步回调