azure blob storage node.js backend

2.3k Views Asked by At

I'm following this article on how to upload a blob into a container. This is what I've have

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] Logs this

   { 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] Logs no response to logs

[3] Logs no result to logs

The container is empty when I check on the azure management portal.

How can this be done correctly?

1

There are 1 best solutions below

2
On

I'm now able to upload a blob to a container. This is what I have

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