如何将图像和文件上载到Azure Blob Node.js
我有node.js应用程序,前端为Angular 我需要上传文件和图像到Azure blob 我已经根据MS文档创建了容器并设置了环境() v12版本 我的函数用于创建并将创建的文件上载到Azure blob,我不知道如何将发布的文件从客户端上载到Azure blob,下面是我在Node.js TypeScript中的代码如何将图像和文件上载到Azure Blob Node.js,node.js,azure,azure-storage-blobs,Node.js,Azure,Azure Storage Blobs,我有node.js应用程序,前端为Angular 我需要上传文件和图像到Azure blob 我已经根据MS文档创建了容器并设置了环境() v12版本 我的函数用于创建并将创建的文件上载到Azure blob,我不知道如何将发布的文件从客户端上载到Azure blob,下面是我在Node.js TypeScript中的代码 import * as formidable from 'formidable'; import * as fs from 'fs'; const { Blob
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 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);
});
致: