2014-01-20 28 views
0

我正在爲我的應用程序使用CompoundJS,現在嘗試實現一個腳本,它將 將圖像從複合j上傳到azure blob。使用NodeJS(CompoundJS)將blob上載到Windows Azure容器

我搜索了網頁,發現在this link中有一個模塊azure(npm install azure) 。

下面是代碼片段我在應用程序中使用

var azure = require("azure"); 
var blobService = azure.createBlobService(); 
blobService.createContainerIfNotExists('container_name', {publicAccessLevel : 'blob'}, function(error){ 
    if(!error){ 
     // Container exists and is public 
     console.log("Container Exists"); 
    } 
}); 

我知道我應該配置ACCESS KEY一些地方,使這項工作,但不知道在哪裏。

請建議。

回答

0

有多種方式可以提供您的存儲訪問憑據。我正在使用環境變量來設置帳戶名稱和密鑰。

這是我如何使用bash設置環境變量:

echo Exporting Azure Storage variables ... 

export AZURE_STORAGE_ACCOUNT='YOUR_ACCOUNT_NAME' 

export AZURE_STORAGE_ACCESS_KEY='YOUR_ACCESS_KEY' 

echo Done exporting Azure Storage variables 

這裏是一個樣本的node.js腳本,我用它來生成存儲爲Azure的斑點現有圖像的縮略圖,使用ImageMagick:

var azure = require('azure'); 
var im = require('imagemagick'); 
var fs = require('fs'); 
var rt = require('runtimer'); 
//Blobservice init 

var blobService = azure.createBlobService(); 

var convertPath = '/usr/bin/convert'; 

var identifyPath = '/usr/bin/identify'; 

global.once = false; 


var blobs = blobService.listBlobs("screenshots", function (error, blobs) { 

    if (error) { 
     console.log(error); 
    } 

    if (!error) { 
     blobs.forEach(function (item) { 

      if (item.name) { 

       if (item.name.length == 59) { 

        //Create the name for the thum     
        var thumb = item.name.substring(0, item.name.indexOf('_')) + '_thumb.png'; 
        if (!global.once) { 
         console.log(global.once); 
         var info = blobService.getBlobToFile("YOUR CONTAINER", item.name, item.name, 
          function (error, blockBlob, response) { 
           im.resize({ 
            srcPath: item.name, 
            dstPath: thumb, 
            width: 100, 
            height: 200 
           }, 
            function (err, sdout, stderr) { 
             if (err) throw err; 
             console.log("resized"); 
             //Delete the downloaded BIG one 
             fs.unlinkSync(item.name); 


             //Upload the thumbnail 
             blobService.putBlockBlobFromFile("YOUR CONTAINER", thumb, thumb, 
                 function (error, blockBlob, response) { 

                  if (!error) { 
                   console.log("blob uploaded: " + thumb); 
                   fs.unlinkSync(thumb); 
                  } 

                 }); 
            }); 

          }); 

         //DEBUG: Uncomment to test only with one file 
         //global.once = true; 

        } 


       } 
      } 
     }); 
    } 
}); 

這裏是爲節點在Azure模塊的官方鏈接(它包含了一些樣品):

Windows Azure Client Library for node

相關問題