【问题标题】:azure blob storage node.js backend天蓝色 blob 存储 node.js 后端
【发布时间】:2015-09-09 01:23:22
【问题描述】:

我正在关注this article,了解如何将 blob 上传到容器中。这就是我所拥有的

var azure = require('azure-storage');
var blobSvc = azure.createBlobServiceAnonymous('https://<storage-name>.blob.core.windows.net/');

exports.post = function(request, response) {
    console.log(request.files); // [1] 

    blobSvc.createBlockBlobFromLocalFile('dubfiles', 'myblob', request.files.file.name, function(error, result, response){
        if(!error){
             // file uploaded
             console.log(response); //[2]
             console.log(result); // [3]
        }
    });

};

[1] 记录这个

   { file: 
       { domain: null, 
         _events: null,
         _maxListeners: 10, 
         size: 859552,
         path: 'D:\\local\\Temp\\155976e3a6b8e0aa871c5deee05af9f2',
         name: 'heuristic-serch.pdf', 
         type: 'application/pdf',
         hash: false, 
         lastModifiedDate: Tue Jun 23 2015 08:43:55 GMT+0000 (Coordinated Universal Time),
        _writeStream: { 
                         domain: null,
                         _events: null,
                         _maxListeners: 10,
                         path: 'D:\\local\\Temp\\155976e3a6b8e0aa871c5deee05af9f2',
                         fd: 3, 
                         writable: false, 
                         flags: 'w', 
                         encoding: 'binary',
                         mode: 438,
                         bytesWritten: 859552, 
                         busy: false,
                         _queue: [], 
                         _open: [Function], 
                         drainable: true, 
                         flush: [Function],
                         write: [Function],
                         end: [Function],
                         destroy: [Function],
                         destroySoon: [Function],
                         pipe: [Function], 
                         setMaxListeners: [Function],
                         emit: [Function], 
                         addListener: [Function],
                         on: [Function], 
                         once: [Function],
                         removeListener: [Function],
                         removeAllListeners: [Function],
                         listeners: [Function] },
                         open: [Function],
                         toJSON: [Function], 
                         write: [Function],
                         end: [Function], 
                         setMaxListeners: [Function],
                         emit: [Function],
                         addListener: [Function],
                         on: [Function], once: [Function],   
                         removeListener: [Function],
                         removeAllListeners: [Function],
                         listeners: [Function]
                } 
         }

[2] 记录对日志没有响应

[3] 没有结果记录到日志中

当我在 azure 管理门户上查看时,容器是空的。

如何正确地做到这一点?

【问题讨论】:

  • 您的文件似乎没有保存在 Blob 存储中。请把if(!error){改成if(error){看看有没有错误。
  • 看起来您正在创建一个未经身份验证的存储客户端(匿名)。这不会阻止您创建容器和 blob 吗?
  • @GauravMantri 我把它改成了if(error),我有这个错误-{ [Error: ENOENT, stat 'D:\home\site\wwwroot\heuristics-search.pdf'] errno: 34, code: 'ENOENT', path: 'D:\\home\\site\\wwwroot\\heuristics-search.pdf' }
  • 感谢您的帮助@GauravMantri
  • 感谢您的帮助@DavidMakogon

标签: node.js azure azure-storage


【解决方案1】:

我现在可以将 blob 上传到容器。这就是我所拥有的

var azure = require('azure-storage');

exports.post = function(request, response) {
   var accountName = request.service.config.appSettings.STORAGE_ACCOUNT_NAME; // storage account name at appSettings
   var accountKey = request.service.config.appSettings.STORAGE_ACCOUNT_ACCESS_KEY; //storage account key at appSettings
   var host = accountName + '.blob.core.windows.net';
   var container = 'container_name';

   var blobSvc = azure.createBlobService(accountName, accountKey, host); 

   blobSvc.createContainerIfNotExists(container, {publicAccessLevel : 'blob'}, function(error, result, response){
       if(!error){
            console.log('no error occurred!'); // logs if no error occurred
            console.log(result); // logs false if container already exists
            console.log(response); // logs response including the etag
          //Container exists and allows 
          //anonymous read access to blob 
          //content and metadata within this container

          blobSvc.createBlockBlobFromLocalFile(container, request.files.file.name, request.files.file.path, function(error, result, response){
              if(!error){
                // file uploaded
                // console.log(error);
                console.log(response); // logs response
                console.log(result); // logs result
              }
          });
       }
    });
};

【讨论】:

  • 嗨!你是如何完成这项工作的?我收到此错误{errno: 34, code: "ENOENT", path: "/var/www/html/foo-project/api/controllers/ragnar.jpg"}
  • 检查身份验证
猜你喜欢
  • 1970-01-01
  • 2022-01-08
  • 2019-08-11
  • 2015-12-04
  • 2019-02-24
  • 2022-08-02
  • 1970-01-01
  • 2017-05-30
  • 1970-01-01
相关资源
最近更新 更多