【问题标题】:error on azure table storage upload file using nodejs expreesjs使用nodejs expreesjs在azure表存储上传文件时出错
【发布时间】:2023-02-10 11:37:06
【问题描述】:

我正在尝试使用以下方法将图像/视频文件上传到 azure blob 存储 节点。但是在通过邮递员上传时我收到错误,即使 我可以在控制台中看到文件对象。下面是它的代码。

const addProductImage = async (req: any, res: Response) => {
    try {
        console.log(req.files.file);

        if (!req.files) {
            res.status(400).send({ status: Status.ERROR, error: "No file uploaded" });
        }

        let file = req.files.file;
        const sharedKeyCred = new StorageSharedKeyCredential(accName, acckey)
        const blobServClient1 = new BlobServiceClient(`https://${accName}.blob.core.windows.net`, sharedKeyCred)
        const containerClient1 = blobServClient1.getContainerClient(containerName)
        const blockBlobClient = containerClient1.getBlockBlobClient(file.name);
        await blockBlobClient.upload(file, file.size);

        res.status(200).send({ status: Status.SUCCESS, });
    }
    catch (error: any) {
        res.status(500).send({ status: Status.ERROR, error });
    }
}

上面的代码控制台是 -

{
  name: 'hclTech.png',
  data: <Buffer 81 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 02 7u 00 00 00 75 08 06 00 00 00 3f 4a 88 b4 00 00 00 01 73 52 47 42 00 ae ce 1c e9 00 00 00 07 ... 54120 more bytes>,
  size: 54170,
  encoding: '7bit',
  tempFilePath: '',
  truncated: false,
  mimetype: 'image/png',
  md5: 'd64ae80f1625e3c766b67bcf05a152a8',
  mv: [Function: mv]
}

在以“文件”为键的表单数据下在邮递员中上传文件 击中帖子,它给出了错误

【问题讨论】:

  • 你能包括你在点击帖子时遇到的错误吗?
  • @Sridevi 捕获时出现 500 状态错误

标签: node.js azure express ecmascript-6 azure-blob-storage


【解决方案1】:
  • 这里如果使用multerpackage读取form-data中的文件。

  • 然后你可以设置 Multer 来读取文件使用 .single('&lt;Name of the Feild&gt;') 的表单数据。这里字段的名称必须与您添加文件的名称相同。在这里我使用字段名称为test的邮递员

  • 现在您可以像这样设置 multer
var  multer = require('multer');
var  upload = multer();
app.use(upload.single('test'));

完整的 API 代码:

var  express = require('express');
var  multer = require('multer');
const { BlobServiceClient } = require("@azure/storage-blob");

  

var  app = express();
var  upload = multer();

  

app.use(express.json());
app.use(express.urlencoded({ extended:  false }));
app.use(upload.single('test'));

  

const  blobServiceClient = BlobServiceClient.fromConnectionString('<Connection String From Portal>');

const  containerClient = blobServiceClient.getContainerClient('< Name of the Container>');

app.get('/', async (req,res)=>{
    const  blockBlobClient = containerClient.getBlockBlobClient(req.file.originalname.toString());
    await  blockBlobClient.upload(file.toString() , file.size);
    res.send(req.file.size.toString());
});

app.listen(3000);

邮递员输出:

门户网站:

【讨论】:

  • 你能完整地添加图像吗,因为我只能看到 alt
猜你喜欢
  • 2022-01-26
  • 1970-01-01
  • 2020-09-04
  • 2021-11-30
  • 2021-06-10
  • 1970-01-01
  • 2017-10-09
  • 1970-01-01
  • 2017-04-11
相关资源
最近更新 更多