简体   繁体   中英

Uploading files to Azure storage using NodeJS

var BlobSerivceClient = require('@azure/storage-blob');
var multipart = require('parse-multipart');
const AZURE_STORAGE_CONNECTION_STRING = process.env["connectionstringstoragepath"]

module.exports = async function (context, req) {
    context.log('Javascript HTTP trigger function processed a request '+multipart+" "+AZURE_STORAGE_CONNECTION_STRING+" "+blobSerivceClienttop);
    var bodyBuffer = Buffer.from(req.body);
    var boundary = multipart.getBoundary(req.headers['content-type']);

    var parts = multipart.Parse(bodyBuffer, boundary);
    const blobSerivceClient = await BlobSerivceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING);

    const container = "bankfeedsdbfiles";
    const containerClient = await blobSerivceClient.getContainerClient(container);
    
    const blobName = parts[0].filename;

    const blockBlobClient = containerClient.getBlockBlobClient(blobName);
    const uploadblobResponse = await blockBlobClient.upload(parts[0].data, parts[0].data.length);

    context.res = {body: {name: parts[0].filename, type: parts[0].type, data: parts[0].data.length}};
    context.done();
} 

The above is my code and I get error like below:

2021-12-27T09:49:25.507 [Error] Executed 'Functions.dbfilesupload' (Failed, Id=fa953980-82b2-4c95-a13e-13988fd1c67e, Duration=240ms)Result: FailureException: TypeError: blobSerivceClienttop.fromConnectionString is not a functionStack: TypeError: blobSerivceClienttop.fromConnectionString is not a functionat module.exports (D:\home\site\wwwroot\dbfilesupload\index.js:13:58)at t.WorkerChannel.invocationRequest (D:\Program Files (x86)\SiteExtensions\Functions\4.0.1\workers\node\worker-bundle.js:2:16866)at c. (D:\Program Files (x86)\SiteExtensions\Functions\4.0.1\workers\node\worker-bundle.js:2:13767)at c.emit (events.js:400:28)at addChunk (internal/streams/readable.js:293:12)at readableAddChunk (internal/streams/readable.js:267:9)at c.Readable.push (internal/streams/readable.js:206:10)at Object.onReceiveMessage (D:\Program Files (x86)\SiteExtensions\Functions\4.0.1\workers\node\worker-bundle.js:2:66126)at Object.onReceiveMessage (D:\Program Files (x86)\SiteExtensions\Functions\4.0.1\workers\node\worker-bundle.js:2:58414)at D:\Program Files (x86)\SiteExtensions\Functions\4.0.1\workers\node\worker-bundle.js:2:32555. Also I have install pakages for @azure/storage-blob and parse-multipart

I wonder what went wrong. The stacktrace doesn't match your code:

blobSerivceClienttop.fromConnectionString is not a function

but in your code you have BlobSerivceClient.fromConnectionString

const blobSerivceClient = await BlobSerivceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING);

where await is not useful BTW as the function does not return a promise

const blobSerivceClient = BlobSerivceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING);

I tried this read the multiple files local system for testing purpose you can try with passing your data and try with removing blobSerivceClienttop in your code

const { BlobServiceClient } = require('@azure/storage-blob');


const blobServiceClient = BlobServiceClient.fromConnectionString("Connection string");

// Create a unique name for the container


console.log('\nCreating container...');


var fs = require('fs');
async = require('async');
  
var dirPath = local file path'; //provice here your path to dir

fs.readdir(dirPath, function (err, filesPath) {
    if (err) throw err;
    filesPath = filesPath.map(function(filePath){ //generating paths to file
        console.log(dirPath+filePath);
       
       
        return dirPath + filePath;
        
    });
    async.map(filesPath, function(filePath, cb){ //reading files or dir
        fs.readFile(filePath, 'utf8', cb);
    }, function(err, results) {
        for (var i = 0; i < results.length; i++){
            const containerClient = blobServiceClient.getContainerClient("test");
            const blockBlobClient = containerClient.getBlockBlobClient(filesPath[i]);
            blockBlobClient.upload(results[i],results[i].length);
            console.log("HIIIIIIIIIIIIIIIiiiiii")
        console.log(results[i]);
        } //this is state when all files are completely read
       
    });
});

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM