node.js - 如何将图像和文件上传到 Azure Blob Node.js
问题描述
我在 Angular 中有带有前端的 node.js 应用程序 我需要将文件和图像上传到 Azure blob 我已经创建了容器并根据 MS 文档设置了环境(https://docs.microsoft.com/en-us/azure/storage /blob/storage-quickstart-blob-nodejs )
v12 版本。
我的函数用于创建创建的文件并将其上传到 Azure Blob,我不知道如何将发布的文件从客户端上传到 Azure Blob,下面是我在 Node.js TypeScript 中的代码
import * as formidable from 'formidable';
import * as fs from 'fs';
const { BlobServiceClient } = require('@azure/storage-blob');
const uuidv1 = require('uuid/v1');
const dotenv = require('dotenv');
dotenv.config();
class BlobController {
private AZURE_STORAGE_CONNECTION_STRING = process.env.CONSTRINGBlob;
constructor(router) {
router.post('/file', this.uploadFile.bind(this));
}
//----Get Lookup tables dynamically-----------//
async uploadFile(req, res) {
const blobServiceClient = await BlobServiceClient.fromConnectionString(this.AZURE_STORAGE_CONNECTION_STRING);
// Create a unique name for the container
//const containerName = 'quickstart' + uuidv1();
const containerName = blobServiceClient.getContainerClient('mycontainer');
console.log('\t', containerName.containerName);
// Get a reference to a container
const containerClient = await blobServiceClient.getContainerClient(containerName.containerName);
let form = new formidable.IncomingForm();
form.parse(req, async function (err, fields, files) {
const blobName = 'test' + uuidv1() + files.file;
// Get a block blob client
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
console.log('\nUploading to Azure storage as blob:\n\t', blobName);
// Upload data to the blob
const data = 'Hello test';
const uploadBlobResponse = await blockBlobClient.upload(data, data.length);
console.log("Blob was uploaded successfully. requestId: ", uploadBlobResponse.requestId);
});
}
}
module.exports = BlobController
谁能帮助我如何使用 Node.js 上传发布到 Azure blob 的文件
解决方案
你快到了:)。
请更改您的以下代码:
form.parse(req, async function (err, fields, files) {
const blobName = 'test' + uuidv1() + files.file;
// Get a block blob client
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
console.log('\nUploading to Azure storage as blob:\n\t', blobName);
// Upload data to the blob
const data = 'Hello test';
const uploadBlobResponse = await blockBlobClient.upload(data, data.length);
console.log("Blob was uploaded successfully. requestId: ", uploadBlobResponse.requestId);
});
至:
form.parse(req, async function (err, fields, files) {
const file = files.file;
const blobName = 'test' + uuidv1() + files.file;
const contentType = file.type;
const filePath = file.path;//This is where you get the file path.
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const uploadBlobResponse = await blockBlobClient.uploadFile(filePath);
});
推荐阅读
- java - 无法测试没有内容类型的 PUT 请求
- javascript - 我正在尝试使用“导入”,但出现了不和谐的模块错误
- powerquery - Power Query:如何使用左(基于 A4 的右函数)插入列
- vba - 将格式从 Word 保留到 Excel 的问题
- java - 在 PostPersist 事件方法中更新 JPA 实体
- asp.net-core - 上传模式中的文件未通过
- java - 如何使用 Java 读取 XML 文件的所有子元素?
- javascript - 如何使用正则表达式查找字符串是否包含两个特定字符和一个点?
- docker - Docker Compose:如何配置拆分为多个 Dockerfile 的多阶段 Docker 映像?
- asp.net-core - ASP.NET Core 应用程序在 url https://gearssdk.opswat.com/ 之后注册,即使我没有注册它