最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何使用适当的内容类型将图像上传到Azure Blob存储

IT培训 admin 11浏览 0评论

如何使用适当的内容类型将图像上传到Azure Blob存储

我已经阅读了MS提供的大多数文档,以将文件/图像上传到Blob存储。现在已经两天了,我被困住了。我找不到合适的方式来上传具有适当内容类型的图像。文件/图像已上传,但上传到BLOB存储后的内容类型已更改为“ application / octet-stream”。我希望它是图像的'image / png'或'image / jpg'等。

samples代码中有C#,但它们没有用。我正在尝试使用node.js

使用的SDK库:@azure/storage-blob

参考:

  • /$web/javascript/azure-storage-blob/12.0.1/classes/blockblobclient.html#uploadfile
  • /@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 };

感谢社区!

回答如下:

假设您的httpHeaderOptions格式不正确,您可以参考此接口说明:BlobHTTPHeaders,下面是我的测试代码。

const blobServiceClient = BlobServiceClient.fromConnectionString(connectionstr)

const containerClient=blobServiceClient.getContainerClient('test')
const blobclient=containerClient.getBlockBlobClient('test.jpg')
let fileStream = fs.createReadStream('E:\\dog.jpg');
const blobOptions = { blobHTTPHeaders: { blobContentType: 'image/jpg' } };
blobclient.uploadStream(fileStream,undefined ,undefined ,blobOptions)

如何使用适当的内容类型将图像上传到Azure Blob存储

我已经阅读了MS提供的大多数文档,以将文件/图像上传到Blob存储。现在已经两天了,我被困住了。我找不到合适的方式来上传具有适当内容类型的图像。文件/图像已上传,但上传到BLOB存储后的内容类型已更改为“ application / octet-stream”。我希望它是图像的'image / png'或'image / jpg'等。

samples代码中有C#,但它们没有用。我正在尝试使用node.js

使用的SDK库:@azure/storage-blob

参考:

  • /$web/javascript/azure-storage-blob/12.0.1/classes/blockblobclient.html#uploadfile
  • /@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 };

感谢社区!

回答如下:

假设您的httpHeaderOptions格式不正确,您可以参考此接口说明:BlobHTTPHeaders,下面是我的测试代码。

const blobServiceClient = BlobServiceClient.fromConnectionString(connectionstr)

const containerClient=blobServiceClient.getContainerClient('test')
const blobclient=containerClient.getBlockBlobClient('test.jpg')
let fileStream = fs.createReadStream('E:\\dog.jpg');
const blobOptions = { blobHTTPHeaders: { blobContentType: 'image/jpg' } };
blobclient.uploadStream(fileStream,undefined ,undefined ,blobOptions)

发布评论

评论列表 (0)

  1. 暂无评论