无法使用以下代码从节点设置天蓝色的内容类型。它总是将内容类型存储为辛烷值流。
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
请给我更多有关此以及如何解决此问题的信息。
假设是因为您没有在BlockBlobUploadOptions
方法中设置uploadFile
,而仅在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);