我正在开发一个 Express.js 服务器,该服务器将被容器化并部署在 Azure 中。该服务器旨在存储 SVG 和 PNG 文件,我正在考虑使用 Docker Volumes 进行此存储。考虑到 Azure 中跨不同主机的容器部署的动态特性,我面临着跨不同实例或主机的 Docker 卷的可访问性和一致性的挑战。
我对使用 Azure Blob 存储作为替代方案持保留态度,因为我相信它会使我的服务变慢且不那么通用。以下是我的具体技术疑问:
我正在寻找解决这些问题的详细见解或配置示例,尤其是那些保持服务的通用性和性能性质的配置。
提前感谢您的帮助!
要访问 Docker 中的文件,可以使用 Azure 存储。与 Azure Blob 存储相比,在 Azure 中使用 Docker 卷进行文件存储的局限性在于性能和可扩展性。
// app.js
const express = require('express');
const multer = require('multer');
const { BlobServiceClient, StorageSharedKeyCredential } = require('@azure/storage-blob');
// Set up Express
const app = express();
const port = 3000;
// Azure Storage configuration
const accountName = process.env.AZURE_STORAGE_ACCOUNT_NAME;
const accountKey = process.env.AZURE_STORAGE_ACCOUNT_KEY;
const sharedKeyCredential = new StorageSharedKeyCredential(accountName, accountKey);
const blobServiceClient = new BlobServiceClient(`https://${accountName}.blob.core.windows.net`, sharedKeyCredential);
const containerName = 'certificate122';
// Set up Multer for file uploads
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
// Serve static files
app.use(express.static('public'));
// Upload route
// Upload route
app.post('/upload', upload.array('files'), async (req, res) => {
try {
const promises = req.files.map(async file => {
const containerClient = blobServiceClient.getContainerClient(containerName);
const blobName = `${Date.now()}-${file.originalname}`;
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
await blockBlobClient.uploadData(file.buffer, { blobHTTPHeaders: { blobContentType: file.mimetype } });
return blobName;
});
const uploadedFiles = await Promise.all(promises);
console.log("Uploaded"); // Print "uploaded" if successfully uploaded
res.json({ files: uploadedFiles });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Display all files route
app.get('/files', async (req, res) => {
try {
const containerClient = blobServiceClient.getContainerClient(containerName);
const files = [];
for await (const blob of containerClient.listBlobsFlat()) {
files.push(blob.name);
}
res.json({ files });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Start the server
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Upload</title>
</head>
<body>
<h1>File Upload</h1>
<form id="uploadForm" enctype="multipart/form-data">
<input type="file" id="fileInput" name="files" multiple>
<button type="submit">Upload</button>
</form>
<h2>Uploaded Files</h2>
<ul id="fileList"></ul>
<script>
async function fetchFiles() {
const response = await fetch('/files');
const data = await response.json();
const fileList = document.getElementById('fileList');
fileList.innerHTML = '';
data.files.forEach(file => {
const li = document.createElement('li');
li.textContent = file;
fileList.appendChild(li);
});
}
fetchFiles();
document.getElementById('uploadForm').addEventListener('submit', async (e) => {
e.preventDefault();
const formData = new FormData(e.target);
const response = await fetch('/upload', {
method: 'POST',
body: formData
});
const data = await response.json();
if (response.ok) {
const uploadedMessage = document.createElement('p');
uploadedMessage.textContent = 'Uploaded';
document.body.appendChild(uploadedMessage);
fetchFiles();
} else {
console.error('Upload failed:', data.error);
}
});
</script>
</body>
</html>
使用此 doc 使用 Azure Web App for Containers 服务启动 Dockerized Node.js 应用程序。
在包含代码存储库的目录中使用以下命令构建 Docker 映像:
docker build -t demoacrjagord.azurecr.io/demo-app:latest .
将 Docker 映像推送到 Azure 容器注册表 (ACR):
docker login demoacrjagord.azurecr.io --username demoacrjagord
将 Docker 镜像推送到 ACR:
docker push demoacrjagord.azurecr.io/demo-app:latest
为 Azure 中的容器创建 Web 应用程序: