【问题标题】:Azure Storage - download file and put into StorageAzure 存储 - 下载文件并放入存储
【发布时间】:2014-04-06 15:31:40
【问题描述】:

我使用 Azure 移动服务 API 作为后端。我需要将文件从 URL 存储到 Azure 移动服务 API 脚本中的 Azure 存储。

我真的不知道该怎么做:(

存储主题中有一些读数,但不是来自 Azure 移动服务 API。

请帮忙。

-------在cmets之后-----

嗯,我很满意。一切都在朝着正确的方向发展。 可以下载文件,我可以将它存储在 Storage 容器中,但在我的路上我做错了,因为当我从 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);

【问题讨论】:

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我终于实现了我的目标。我使用了这个 thread 中 Valery Jacobs 的代码

    感谢大家的帮助。

    【讨论】:

      猜你喜欢
      • 2021-12-08
      • 1970-01-01
      • 2015-08-22
      • 2020-11-09
      • 2020-08-08
      • 2012-12-08
      • 2018-08-03
      相关资源
      最近更新 更多