Question

I am using Azure Mobile Service API as a backend. I need to store a file from an URL to the Azure Storage in Azure Mobile Service API script.

Really I have no idea how to do it :(

There are some readings in Storage subject, but not from Azure Mobile Sevice API.

Please help.

------- After comments------

Well I'm quite there. It's all going in the right direction. Can download the file, I can store it in Storage container, but on my way I am doing something wrong, because when I view the file from Sotrage, it is corrupted.

http://screencast.com/t/evBpmAVHQrbb http://screencast.com/t/H5Z0xzzsaH

var requestImageCallback = function (err, resp2, body) {
if (err || resp2.statusCode !== 200) {
    console.error('Error sending data to the provider: ', err);
    response.send(statusCodes.INTERNAL_SERVER_ERROR, body);
} else {
    var type = resp2.headers["content-type"],
        prefix = "data:" + type + ";base64,";

    resp2.setEncoding('binary');
    var base64 = new Buffer(body, 'binary').toString('base64'),
        item = prefix + base64;
    console.log(item);


    var blobService = azure.createBlobService(accountName, accountKey, host);
    blobService.createContainerIfNotExists("avatars", {
        publicAccessLevel: 'blob'
    }, function (error) {
        if (!error) {
            var sharedAccessPolicy = {
                AccessPolicy: {
                    Permissions: azure.Constants.BlobConstants.SharedAccessPermissions.WRITE,
                    Expiry: new Date(new Date().getTime() + 5 * 60 * 1000)
                }
            };
            var now = new Date();
            var filename = now.getTime() + ".jpg";
            var sasQueryUrl =
                blobService.generateSharedAccessSignature("avatars",
                    filename, sharedAccessPolicy);

            console.log(qs.stringify(sasQueryUrl.queryString));

            console.log(sasQueryUrl.baseUrl + sasQueryUrl.path);

            var options = {
                contentType: type,
                contentTypeHeader: type
            };
            blobService.createBlockBlobFromText('avatars', filename, item, options,    function (error) {
                if (!error) {
                    // Blob uploaded
                }
            });
        } else {
            console.error(error);
        }
    });

    }
}

var http = require('request');
var reqOptions = {
    uri: userData.picture.data.url,
    headers: {
        Accept: "image/jpeg, image/png"
    }
};
http(reqOptions, requestImageCallback);
Was it helpful?

Solution 2

I finally managed to achieve my goal. I used the code by Valery Jacobs from this thread

Thanks all for help.

OTHER TIPS

I believe the reason why the image is not being displayed is because the contents of the file are string (you're converting the stream into base64 string and then setting the content of the image as that string). I would recommend taking the binary stream as is and call createBlockBlobFromStream method to upload the blob.

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