【发布时间】:2020-08-01 16:57:33
【问题描述】:
我已经阅读了 MS 提供的将文件/图像上传到 Blob 存储的大部分文档。现在已经两天了,我被困住了。我找不到合适的方法来上传具有正确内容类型的图像。文件/图像已上传,但上传到 BLOB 存储后的内容类型更改为“应用程序/八位字节流”。我希望它是 'image/png' 或 'image/jpg' 等。
有samples 的C# 代码但它们没有用。 我正在尝试使用 node.js
使用的 SDK 库:@azure/storage-blob
参考资料:
- https://azuresdkdocs.blob.core.windows.net/$web/javascript/azure-storage-blob/12.0.1/classes/blockblobclient.html#uploadfile
- https://docs.microsoft.com/en-us/javascript/api/@azure/storage-blob/blockblobclient?view=azure-node-latest
示例代码:
const bc = new BlockBlobClient(
rhcConfig.STORAGE_CONNECTION_STRING,
rhcConfig.CONTAINER_NAME,
`IMAGES/${fileName}`
);
// let result = await bc.uploadFile(_file);
// console.log(result);
const buff = Buffer.from(file, "base64");
const stream = getStream(buff);
const streamLength = buff.length;
await bc.uploadStream(stream, streamLength, 1, { httpHeaderOptions });
httpHeaderOptions:
const httpHeaders = {
"x-ms-blob-cache-control": "1000",
"x-ms-blob-content-type": "image/png",
"x-ms-blob-content-md5": `${md5Hash}`,
"x-ms-blob-content-encoding": "compress",
"x-ms-blob-content-language": "en",
"x-ms-blob-content-disposition": "multipart/form-data",
};
const httpHeaderOptions = { blobHTTPHeaders: httpHeaders };
感谢社区!!
【问题讨论】:
标签: node.js file-upload azure-blob-storage