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

Can't set content type from using uploadFile method in @azure/storage-blob (SDK/NPM)

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

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();

您是否尝试设置 blobHttpHeaders 并传递给 uploadFile 方法?

const blobOptions = { blobHTTPHeaders: { blobContentType: 'image/png' } };
const res = await blockBlobClient.uploadFile(filePath, blobOptions);