Question

I am using CompoundJS for my application and now trying to implement a script which would upload images to azure blob from compoundjs.

I searched web and found that there is a module azure (npm install azure) as specified in this link.

Below is the code snippet i used in my application

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

I am aware that i should configure ACCESS KEY some where to make this work, but not sure where.

Please suggest.

Was it helpful?

Solution

You would need to provide your account name/key like this:

var blobService = azure.createBlobService('accountname', 'accountkey');

You can look at the source code here: https://github.com/WindowsAzure/azure-sdk-for-node/blob/master/lib/azure.js.

OTHER TIPS

There are multiple ways to supply your storage access credentials. I am using environment variables to set the account name and key.

Here is how I set the environment variables using 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

And here is a sample node.js script that I use to generate thumbnails from existing images that are stored as Azure blobs, using 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;

                    }


                }
            }
        });
    }
});

And here is the official link to the Azure module for Node (it contains some samples):

Windows Azure Client Library for node

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top