【问题标题】:Can't set content type from using uploadFile method in @azure/storage-blob (SDK/NPM)无法使用 @azure/storage-blob (SDK/NPM) 中的 uploadFile 方法设置内容类型
【发布时间】:2020-05-30 00:05:48
【问题描述】:

无法使用以下代码从节点设置 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

请给我更多关于这个以及如何解决这个问题的信息。

【问题讨论】:

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


【解决方案1】:

假设是因为你没有在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();

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-27
      • 2021-09-17
      • 2021-04-20
      • 2017-03-17
      • 1970-01-01
      • 2017-09-21
      • 1970-01-01
      相关资源
      最近更新 更多