无法使用以下代码从 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();
关于node.js - 无法使用 @azure/storage-blob 中的 uploadFile 方法设置内容类型(SDK/NPM),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60222106/