无法流式传输和上传图像

问题描述

我正在尝试将多张图片上传到azure blob服务,一切看起来都很好,尽管我错过了一些事情 我得到的错误

events.js:292 抛出//未处理的“错误”事件 ^

错误:ENOENT:没有此类文件或目录,请打开“ logo192.png” ReadStream实例上的“错误”事件位于: 在internal / fs / streams.js:163:14 在FSReqCallback.oncomplete(fs.js:159:23){errno:-2,代码:'ENOENT',syscall:'open',path:'logo192.png'}

const uuidv1 = require("uuid/v1");
var azure = require("azure-storage");
var fs = require("fs");
const containerName = "image";

const fileUpload = async (req,res,next) => {
  let imageStatus = [];
  console.log(req.files);
  await req.files.forEach((reqfile,i) => {
    const blobName = uuidv1() + "-" + reqfile.originalname;
    const blobSvc = azure.createBlobService();
    const file = reqfile.originalname;

    const stream = fs.createReadStream(file).pipe(blobSvc.createWriteStreamToBlockBlob(containerName,blobName,{ blockIdPrefix: "block" }));
    let dataLength = 0;

    stream
      .on("data",function (chunk) {
        dataLength += chunk.length;
      })
      .on("end",function () {
        console.log("The length was:",dataLength);
      });

    blobSvc.createBlockBlobFromStream(containerName,stream,dataLength,function (error,result,response) {
      if (!error) {
        console.log("ok Blob uploaded");
        imageStatus.push({
          imageName: result.name,imagePath: result.name,});
      } else {
        console.log(error);
      }
    });
  });
  res.status(200).json({ imageStatus });
};
module.exports = fileUpload;

控制台输出要求文件

[
  {
    fieldname: 'files',originalname: 'logo192.png',encoding: '7bit',mimetype: 'image/png',buffer: <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 c0 00 00 00 c0 08 03 00 00 00 65 02 9c 35 00 00 00 87 50 4c 54 45 00 00 00 64 da fb 61 da fc ... 5297 more bytes>,size: 5347
  }
]

解决方法

如果要将图像上载到节点应用程序中的Azure blob,建议您使用新的SDK @azure/storage-blob。 SDk是azure-storage是旧版。

例如

  1. SDK
npm install into-stream @azure/storage-blob multer

PS:

  • sdk into-stream用于将Buffer转换为流
  • sdk multer用于处理文件数据。它将读取文件数据作为Buffer。
  1. 代码
const {
  BlobServiceClient,StorageSharedKeyCredential,newPipeline,} = require("@azure/storage-blob");

//define multer
const multer = require("multer");
const inMemoryStorage = multer.memoryStorage();
const uploadStrategy = multer({ storage: inMemoryStorage }).array("images");

// configure storage
const accountNmae = "jimtestdiag924";
const accountKey =
  "";
const sharedKeyCredential = new StorageSharedKeyCredential(
  accountNmae,accountKey,);
const pipeline = newPipeline(sharedKeyCredential);
const blobServiceClient = new BlobServiceClient(
  `https://${accountNmae}.blob.core.windows.net`,pipeline,);
const uploadOptions = { bufferSize: 4 * 1024 * 1024,maxConcurrency: 20 };
const getStream = require("into-stream");
const contanierName = "images";

const fileUpload = async (req,res,next) => {
  let imageStatus = [];
  console.log(req.files);
 try {
    await req.files.forEach(async (reqfile,i) => {
      const blobName = reqfile.originalname;
      const stream = getStream(reqfile.buffer);
      const containerClient = blobServiceClient.getContainerClient(
        contanierName,);
      const blockBlobClient = containerClient.getBlockBlobClient(blobName);

      await blockBlobClient.uploadStream(
        stream,uploadOptions.bufferSize,uploadOptions.maxConcurrency,{ blobHTTPHeaders: { blobContentType: reqfile.mimetype } },);
    });
    res.status(200).json({ imageStatus });
    res.render("success",{ message: "File uploaded to Azure Blob storage." });
  } catch (err) {
    throw err;
  }
};
module.exports = fileUpload;

有关更多详细信息,请参阅here