1
votes

I am using Node.js to upload an image to azure storage https://github.com/Azure/azure-storage-node. The upload is successful, but I cannot see the image when I visit the URL.

The upload code looks like.

var file = 'tmp/myimage.png';
var blobService = azure.createBlobService(config.azure.connection_string);

blobService.createBlockBlobFromLocalFile(config.azure.container, 'taskblob', file, function(err, result, response) {
    if(err) return console.log(err);
    console.log(response);
    callback();
});

In azure portal I can see something has been uploaded to my container, visiting the provided URL just loads a blank page.

https://<storage>.blob.core.windows.net/<container>/taskblob

I am also getting a success response back from Azure when logging 'response'

1
Can you please check the source and see if you see any XML there? Other things to see if the container ACL is set as Private.Gaurav Mantri
Hey the publicAccessLevel is set to Blob, if thats what you mean? Although looking into it, I can see that the size is 0B, which would indicate some kind of error in the upload, any ideas?wazzaday
It would be also nice to assign a Content Type to the image so it can be accessed from httpEmmanuel DURIN
You might want to check your permissions of your container azure.microsoft.com/en-us/documentation/articles/…SteveSeow
@wazzaday, This is strange. Could you please share this blockblob properties by click the 'edit ' on Azure portal? Also, you could upload the text file, and access it .Will Shao - MSFT

1 Answers

1
votes

@wazzaday, Generally, we can upload the files into Azure Blob Stroage using the code as you provided.

    var azure = require('azure-storage');
    var blobSvc = azure.createBlobService("**","**");
    var file = 'tmp/1.txt';

    blobSvc.createContainerIfNotExists('mycontainer', function (error, result, response) {
        if (!error) {
        // Container exists and allows
        // anonymous read access to blob
        // content and metadata within this container
            console.log('ok')
        }
    });
    blobSvc.createBlockBlobFromLocalFile('mycontainer', 'myblob1', file, function (error, result, response) {
        if (!error) {
            console.log('file uploaded'+response)
        } else {
            console.log(error);
        }
    });

From above code, we need make sure the file path is right. Because your file size is 0 on Azure Portal, I suggest you can try ReadStream to upload your file and check the file size again. Please refer to this code :

var azure = require('azure-storage');
var fs = require('fs');
var blobSvc = azure.createBlobService("**","**");
var file = 'tmp/1.txt';
var stream = fs.createReadStream(file)
var dataLength = 0;
// using a readStream that we created already
stream
  .on('data', function (chunk) {
    dataLength += chunk.length;
})
  .on('end', function () {  // done
    console.log('The length was:', dataLength);
});
blobSvc.createContainerIfNotExists('mycontainer', function (error, result, response) {
    if (!error) {
    // Container exists and allows
    // anonymous read access to blob
    // content and metadata within this container
        console.log('ok')
    }
});



blobSvc.createBlockBlobFromStream('mycontainer', 'filename', stream,dataLength, function (error) {
    if (!error) {
        console.log('ok Blob uploaded')
    }

});

Please try above code, any update, please let me know.