Azure存储 - 下载文件并放入存储

时间:2014-04-05 12:03:11

标签: azure azure-storage azure-storage-blobs azure-mobile-services

我使用Azure Mobile Service API作为后端。我需要将一个URL中的文件存储到Azure Mobile Service API脚本中的Azure存储中。

我真的不知道怎么做:(

存储主题中有一些读数,但Azure Mobile Sevice API中没有读数。

请帮忙。

-------评论后------

嗯,我在那里。这一切都朝着正确的方向发展。 可以下载文件,我可以将它存储在存储容器中,但是在我的路上我做错了,因为当我从Sotrage查看文件时,它已经损坏了。

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

2 个答案:

答案 0 :(得分:1)

我认为没有显示图像的原因是因为文件的内容是字符串(您将流转换为base64字符串,然后将图像的内容设置为该字符串)。我建议按原样使用二进制流并调用createBlockBlobFromStream方法上传blob。

答案 1 :(得分:1)

我终于设法实现了我的目标。我使用了Valery Jacobs的代码thread

谢谢大家的帮助。

相关问题