使用MulterAzureStorage时req.body为空

huangapple go评论83阅读模式
英文:

Empty req.body when using MulterAzureStorage

问题

我们有一个应用程序,在其中我们使用表单发送信息和文件到我们的 Blob 存储。我们使用了 MulterAzureStorage 包来实现这个功能。同时,我们使用了中间件来对用户进行身份验证。已知身份验证中间件之前已被使用并且能正常工作。当我们按照工作流程进行操作,不使用身份验证和 multer 中间件时,req.body 参数会被表单数据(包括文件)填充。不幸的是,当我们同时添加这两个中间件时,req.body 是空的,且没有文件存储在 blob 存储的容器中。

代码结构如下:

  connectionString: AZURE_CONNECTION_STRING,
  accessKey: AZURE_ACCESS_KEY,
  accountName: AZURE_STORAGE_ACCOUNT_NAME,
  containerName: CONTAINER_NAME,
  blobName: BLOB_NAME,
});

const upload = multer({
  storage: azureStorage
});

router.post('/verzoek',[auth,upload.any()], async (req, result) => {
   console.log(req.body)
   // Our code continues below but it is removed for simplicity
}

由于我们可以在没有中间件的情况下读取 req.body,我们认为请求没问题。但为了确保,post 请求如下:

const request = new FormData();

// 在这一点上,我们填充了变量 request,但出于简单起见我们将其删除

$.ajax({
          async: true,
          method: 'POST',
          url: '/aanvragen/verzoek',
          data: request,
          cache: false,
          processData: false,
          contentType: false,
            
          xhr: function () {
              var xhr = new XMLHttpRequest();
              return xhr;
          }
      }).done(function (result) {
          if (result == "Succes") {
              // Activate some classes
          } else {
              // An alert
          }
      }).fail(function (xhr, status) {
          // An error message
      });

总之,在中间件中出现了问题,导致 req 的 body 没有内容。我们尝试了一些建议,但没有取得任何成功。希望有人能帮我们找出问题所在!谢谢你提前帮助!

英文:

We have an application in which we use a form to send information and files to our blob storage. We are using the MulterAzureStorage package for this. At the same time we are using middleware such that the user can authenticate themself. The authentication middleware has been used before and it is known that it works correctly. When we are following the workflow without the authentication and multer middleware, then the req.body param is filled with the data of the form including the files. Unfortunately, when we add both of the middleware, then the req.body is empty and no file is stored in the container of the blob storage.

The structure of the code looks like this:

const azureStorage = new MulterAzureStorage({
  connectionString: AZURE_CONNECTION_STRING,
  accessKey: AZURE_ACCESS_KEY,
  accountName: AZURE_STORAGE_ACCOUNT_NAME,
  containerName: CONTAINER_NAME,
  blobName: BLOB_NAME,
});

const upload = multer({
  storage: azureStorage
});

router.post('/verzoek',[auth,upload.any()], async (req, result) => {
   console.log(req.body)
   // Our code continues below but it is removed for simplicity
}

Since we can read the req.body without the middleware, we think that the request is alright. However, just to be sure, the post request goes as follows:

const request = new FormData();

// At this point we filled the variable request but we removed it for simplicity

$.ajax({
          async: true,
          method: 'POST',
          url: '/aanvragen/verzoek',
          data: request,
          cache: false,
          processData: false,
          contentType: false,
            
          xhr: function () {
              var xhr = new XMLHttpRequest();
              return xhr;
          }
      }).done(function (result) {
          if (result == "Succes") {
              // Activate some classes
          } else {
              // An alert
          }
      }).fail(function (xhr, status) {
          // An error message
      });

To conclude, there is something going wrong in the middleware such that the body of the req has no content. We tried some suggestions but haven't had any success. Hope somebody can help us with finding the problem! Thank you in advance!

答案1

得分: 0

  • 我在 Azure 门户中创建了存储帐户并创建了一个容器。

  • 请查看下面对我有效的代码。

const express = require('express');
const multer = require('multer');
const { BlobServiceClient } = require('@azure/storage-blob');
const app = express();
const authMiddleware = (req, res, next) => {
  next(); 
};
const multerMiddleware = multer().single('file');
const connectionString ='#######';  
const containerName = 'pavan123';
app.post('/upload', authMiddleware, multerMiddleware, async (req, res) => {
  const file = req.file;
  const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString);
  const containerClient = blobServiceClient.getContainerClient(containerName);
  const blobName = file.originalname;
  const blockBlobClient = containerClient.getBlockBlobClient(blobName);
  const uploadResponse = await blockBlobClient.uploadData(file.buffer);
  res.send('文件上传成功。');
});
const port = 3000;  
app.listen(port, () => {
  console.log(`服务器已启动,端口号为 ${port}`);
});
  • 当我运行上述代码时,我得到了如下所示的结果。

  • 命令需要使用 npm install express multer @azure/storage-blob

  • 我使用 Postman 将表单数据文件 POST 到容器中。

  • 我只是检查文件是否已上传到我的云存储帐户中。

英文:
  • I created storage account in azure portal and create a container.

使用MulterAzureStorage时req.body为空

  • Check the below code that works for me.
const express = require('express');
const multer = require('multer');
const { BlobServiceClient } = require('@azure/storage-blob');
const app = express();
const authMiddleware = (req, res, next) => {
  next(); 
};
const multerMiddleware = multer().single('file');
const connectionString ='#######';  
const containerName = 'pavan123';
app.post('/upload', authMiddleware, multerMiddleware, async (req, res) => {
  const file = req.file;
  const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString);
  const containerClient = blobServiceClient.getContainerClient(containerName);
  const blobName = file.originalname;
  const blockBlobClient = containerClient.getBlockBlobClient(blobName);
  const uploadResponse = await blockBlobClient.uploadData(file.buffer);
  res.send('File uploaded successfully.');
});
const port = 3000;  
app.listen(port, () => {
  console.log(`Server started on port ${port}`);
});

  • When I run the above code I got as given below.
    使用MulterAzureStorage时req.body为空

  • The command need to use npm install express multer @azure/storage-blob.

  • I use the postman to POST form data file to container.
    使用MulterAzureStorage时req.body为空

  • I just checked whether the file is uploaded in my cloud storage account.
    使用MulterAzureStorage时req.body为空

huangapple
  • 本文由 发表于 2023年5月25日 17:46:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定