📅  最后修改于: 2023-12-03 15:33:00.472000             🧑  作者: Mango
Multer is a middleware for handling multipart/form-data
, which is primarily used for uploading files. It is designed for use with Express and other web frameworks. In combination with a storage service such as Azure Storage, Multer can be used to upload files securely and efficiently.
Azure Storage is a cloud-based storage solution that provides highly scalable and durable storage for data of any size and type. It offers a variety of storage options, including Blob, File, Queue, and Table storage.
Multer can be used with Azure Storage to upload files to the cloud. Here's how it can be done:
npm install multer @azure/storage-blob
const { BlobServiceClient } = require('@azure/storage-blob');
const azureStorageConnection = '<your-azure-storage-connection-string>';
const blobServiceClient = BlobServiceClient.fromConnectionString(azureStorageConnection);
const containerName = '<your-container-name>';
const containerClient = blobServiceClient.getContainerClient(containerName);
const multer = require('multer');
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
const express = require('express');
const app = express();
app.post('/upload', upload.single('file'), async (req, res) => {
const { originalname, buffer } = req.file;
const blobName = originalname;
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const uploadBlobResponse = await blockBlobClient.upload(buffer, buffer.length);
res.send('File uploaded to Azure Storage');
});
This route uses upload.single
to accept a single file with the key file
and then uploads the file to Azure Storage using the Blob storage client.
Using Multer and Azure Storage is a great way to securely and efficiently handle file uploads in your web application. With the right setup, it can be a very powerful tool for managing files in the cloud.