【问题标题】:Uploading files to Azure storage using NodeJS使用 NodeJS 将文件上传到 Azure 存储
【发布时间】:2022-01-26 08:03:31
【问题描述】:
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();
} 

以上是我的代码,我收到如下错误:

2021-12-27T09:49:25.507 [错误] 执行“Functions.dbfilesupload” (失败,id=fa953980-82b2-4c95-a13e-13988fd1c67e, 持续时间 = 240 毫秒)结果:FailureException:TypeError: blobSerivceClienttop.fromConnectionString 不是函数堆栈: TypeError:blobSerivceClienttop.fromConnectionString 不是 功能模块.exports (D:\home\site\wwwroot\dbfilesupload\index.js:13:58)在 t.WorkerChannel.invocationRequest (D:\Program Files (x86)\SiteExtensions\Functions\4.0.1\workers\node\worker-bundle.js:2:16866) 在 C。 (D:\程序文件 (x86)\SiteExtensions\Functions\4.0.1\workers\node\worker-bundle.js:2:13767) 在 c.emit (events.js:400:28) 在 addChunk (internal/streams/readable.js:293:12) 在 readableAddChunk (internal/streams/readable.js:267:9) 在 c.Readable.push (internal/streams/readable.js:206:10) 在 Object.onReceiveMessage (D:\程序文件 (x86)\SiteExtensions\Functions\4.0.1\workers\node\worker-bundle.js:2:66126) 在 Object.onReceiveMessage (D:\Program Files (x86)\SiteExtensions\Functions\4.0.1\workers\node\worker-bundle.js:2:58414) 在 D:\程序文件 (x86)\SiteExtensions\Functions\4.0.1\workers\node\worker-bundle.js:2:32555。 我还为@azure/storage-blob 和 解析多部分

【问题讨论】:

  • 我不知道出了什么问题。具有 blobSerivceClienttop 的堆栈跟踪与您的代码不匹配: blobSerivceClienttop.fromConnectionString 不是函数,但在您的代码中您有 BlobSerivceClient.fromConnectionString const blobSerivceClient = await BlobSerivceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING);其中await 没有用顺便说一句,因为该函数不返回承诺 const blobSerivceClient = BlobSerivceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING);

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


【解决方案1】:

我尝试读取本地系统的多个文件以进行测试,您可以尝试传递数据并尝试在代码中删除 blobSerivceClienttop

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

【讨论】:

  • 如果答案有帮助,您可以接受它作为答案(单击答案旁边的复选标记将其从灰色切换为已填充。)。这对其他社区成员可能是有益的。谢谢
猜你喜欢
  • 2020-09-04
  • 1970-01-01
  • 2021-06-20
  • 2018-08-01
  • 2017-01-24
  • 2017-08-19
  • 2017-01-04
  • 1970-01-01
  • 2019-12-01
相关资源
最近更新 更多