Reputation: 63
I want to retrieve the binary data of an image from a Microsoft Azure blob storage container.
I have successfully uploaded an image to a storage container on Azure from an iOS app using the following function on AZSCloudBlobContainer.
blockBlob.uploadFromData(imageData, completionHandler:
I can also successfully download it straight to the iOS app using the following function on AZSCloudBlob.
blockBlob.downloadToDataWithCompletionHandler({(error: NSError?, data: NSData?)
The problem I have is that I want to be able to retrieve the image from a node.js server side script, not directly from the app. Amongst others, I have tried the following code...
var containerName = 'containerName';
var blobService = storage.createBlobService(accountName, accountKey);
var blobName = 'blobname';
var fs = require('fs');
var stream = fs.createWriteStream('output.txt');
blobService.getBlobToStream(containerName, blobName, stream, function (error, result, response2) {
response.json(result);
});
The response retrieves the blob information but I don't know how to get the actual binary data of the image?
({
blobType = BlockBlob;
container = containername;
contentLength = 14593;
contentSettings = {
contentType = "application/octet-stream";
};
etag = "\"0x8D********\"";
lastModified = "Mon, 05 Sep 2016 18:57:41 GMT";
lease = {
state = available;
status = unlocked;
};
metadata = {
};
name = "blobname";
requestId = "ce3e7************";
serverEncrypted = false;
})
I've tried many other avenues as well but I'm completely new to node.js and have spent far too much time trying to solve this, any direction would be much appreciated, thanks.
Upvotes: 1
Views: 835
Reputation: 13918
As Azure Mobile Apps in Node.js are based on Expressjs, whose response
middleware is a kind of writable stream, you can leverage the function getBlobToStream()
directly send the content stream to the client side. E.G.
module.exports = {
"get": function (req, res, next) {
var azure = require('azure-storage');
var blobsrv = azure.createBlobService(
'<storage_account>',
'<storage_key>'
)
blobsrv.getBlobToStream('<container>', '<blob>', res, function (error, result) {})
}
}
And you can test the functionality by browsing the custom api directly from browser via the URL of the api http://<your_mobile_app_name>.azurewebsites.net/api/<api_name>
Upvotes: 1