【问题标题】:How to upload images and files to Azure Blob Node.js如何将图像和文件上传到 Azure Blob Node.js
【发布时间】:2020-06-12 22:59:38
【问题描述】:

我在 Angular 中有带前端的 node.js 应用程序 我需要将文件和图像上传到 Azure blob 我已经根据 MS 文档(https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-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 的文件

【问题讨论】:

    标签: node.js azure azure-blob-storage


    【解决方案1】:

    你快到了:)。

    请更改您的以下代码:

    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);
      });
    

    【讨论】:

    • 不明白这是如何被接受的答案,问题清楚地表明文件和图像来自角度客户端,所以您的解决方案是在 nodejs 服务器中调用需要 filePath 的函数!此文件路径不存在。
    • @VinKrish - 当您使用form 处理文件上传时,它会自动将上传的内容保存在临时文件中,file.path 属性会为您提供该临时文件的完整路径。你不需要做任何特别的事情。试试这个代码。 HTH。
    • 我找到了更好的解决方案,将输入流通过管道传输到 azure 上传功能。当我们的实现涉及容器和 lambda 函数时,获取文件路径很棘手。
    【解决方案2】:
    //Here's my form.parse code I used to upload pictures.
    
    form.parse(req, async (err: any, fields: any, files: any) => {        
        const file = files.file;
        const filePath = file.path;//This is where you get the file path. (this is the file itself)
        const blobName: string = slugify(file.name);
    
        const blockBlobClient = containerClient.getBlockBlobClient(blobName);
    
        const uploadBlobResponse = await blockBlobClient.uploadFile(filePath)
        console.log("Blob was uploaded successfully. requestId: ", uploadBlobResponse)
        
        if (err) return reject(err)
        //write to DB
        //end write to DB
        resolve(fields)
    })
    

    【讨论】:

      猜你喜欢
      • 2021-12-30
      • 2020-01-25
      • 2013-10-17
      • 1970-01-01
      • 2017-04-26
      • 1970-01-01
      • 2017-09-21
      • 1970-01-01
      • 2013-07-29
      相关资源
      最近更新 更多