gpt4 book ai didi

node.js - 在 JavaScript v10 SDK Nodejs 中将内容类型设置为图像

转载 作者:行者123 更新时间:2023-12-03 05:34:18 25 4
gpt4 key购买 nike

当我尝试使用 Azure JS SDK v10 上传图像时,它会将它们显示为 application/octetstream。

这是我最初根据azure官方文档所做的

 async function uploadLocalFile(
aborter,
containerClient,
filePath,
file_guid,
)

filePath = path.resolve(filePath); //Image file path
const fileName = file_guid; //contains file name
const blobClient = containerClient.getBlobClient(fileName);
const blockBlobClient = blobClient.getBlockBlobClient();


return await blockBlobClient.uploadFile(filePath, aborter);

}

这是上传图像的 application/octetstream

然后我尝试了这个,设置标题并尝试将其设置为 image/jpeg,但这仍然使内容类型为 application/octetstream。


filePath = path.resolve(filePath);
const fileName = file_guid;
const blobClient = containerClient.getBlobClient(fileName);
const blockBlobClient = blobClient.getBlockBlobClient();

const blobOptions = { blobHTTPHeaders: { blobContentType: 'image/jpeg' } };

return await blockBlobClient.uploadFile(filePath, aborter, blobOptions);

}

上传到azure blob存储时,有什么方法可以将图像的内容类型设置为image/jpeg吗?

最佳答案

我用你的代码测试了一下,设置了httpHeaderOptions,你可以引用这个接口(interface)说明:BlobHTTPHeaders ,下面是我的测试代码。

const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob");
const path = require("path");
// Enter your storage account name and shared key
const account = "xxxx";
const accountKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";

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

const containerName = "container";

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

const filePath = path.resolve("C:\\xxxxx\\me.jpg");
const fileName = "bbb";
const blobClient = containerClient.getBlobClient(fileName);
const blockBlobClient = blobClient.getBlockBlobClient();
const blobOptions = { blobHTTPHeaders: { blobContentType: 'image/jpeg' } };
const uploadBlobResponse =await blockBlobClient.uploadFile(filePath,blobOptions);

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

main();

enter image description here

关于node.js - 在 JavaScript v10 SDK Nodejs 中将内容类型设置为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63659297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com