以JSON格式获取Azure Blob数据

时间:2017-08-25 10:18:55

标签: angularjs json azure azure-storage-blobs

我有以下代码来获取我的blob容器(名为id)的blob(上传文件)列表:

app.controller("attachmentsController", ["$scope", "$http", function ($scope, $http) {
    var ct = this;

    $scope.go = function () {
        ct.id = $scope.id;

        var rootUrl = 'https://my-account.blob.core.windows.net';
        var containerPropertiesUrl = `${rootUrl}/${ct.id}?restype=container`;
        var blobListUrl = `${containerPropertiesUrl}&comp=list`;
        // get container's blob list
        $http.get(blobListUrl)
            .then(function (response) {
                ct.blobs = response.data;
            });
    };
}]);

response.data我获取XML:

<?xml version="1.0" encoding="utf-8"?>
<EnumerationResults ContainerName="https://my-account.blob.core.windows.net/000002">
<Blobs>
<Blob>
<Name>logo-separator.svg</Name>
<Url>https://my-account.blob.core.windows.net/000002/logo-separator.svg</Url>
<Properties>
<Last-Modified>Fri, 25 Aug 2017 10:00:01 GMT</Last-Modified>
<Etag>0x8Y4EBAX0D5850C7</Etag><Content-Length>2048</Content-Length>
<Content-Type>application/octet-stream</Content-Type><Content-Encoding />
<Content-Language />
<Content-MD5>BDm9NV0Zn4e6zQO2e/D1Dg==</Content-MD5><Cache-Control />
<BlobType>BlockBlob</BlobType>
<LeaseStatus>unlocked</LeaseStatus>
</Properties>
</Blob>
</Blobs>
<NextMarker />
</EnumerationResults>"

有没有办法以JSON格式获取所有这些,以便能够在我的角度应用程序中使用它?

3 个答案:

答案 0 :(得分:0)

我现在的(临时?)解决方案是使用一个自定义函数来转换JSON中的XML:

app.controller("attachmentsController", ["$scope", "$http", function ($scope, $http) {
    var ct = this;
    // Changes XML to JSON
    ct.xmlToJson = function(xml) {

        // Create the return object
        var obj = {};

        if (xml.nodeType == 1) { // element
            // do attributes
            if (xml.attributes.length > 0) {
                obj["@attributes"] = {};
                for (var j = 0; j < xml.attributes.length; j++) {
                    var attribute = xml.attributes.item(j);
                    obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
                }
            }
        } else if (xml.nodeType == 3) { // text
            obj = xml.nodeValue;
        }

        // do children
        if (xml.hasChildNodes()) {
            for (var i = 0; i < xml.childNodes.length; i++) {
                var item = xml.childNodes.item(i);
                var nodeName = item.nodeName;
                if (typeof (obj[nodeName]) == "undefined") {
                    obj[nodeName] = ct.xmlToJson(item);
                } else {
                    if (typeof (obj[nodeName].push) == "undefined") {
                        var old = obj[nodeName];
                        obj[nodeName] = [];
                        obj[nodeName].push(old);
                    }
                    obj[nodeName].push(ct.xmlToJson(item));
                }
            }
        }
        return obj;
    };

    $scope.go = function () {
        ct.id = $scope.id;

        var rootUrl = 'https://my-account.blob.core.windows.net';
        var containerPropertiesUrl = `${rootUrl}/${ct.id}?restype=container`;
        var blobListUrl = `${containerPropertiesUrl}&comp=list`;
        // get all possible values
        $http.get(blobListUrl)
            .then(function (response) {
                var xmlString = response.data;
                var parser = new DOMParser();
                var xml = parser.parseFromString(xmlString, "text/xml");
                var myObj = ct.xmlToJson(xml);
                ct.blobs = myObj.Blobs;
            });
    };
}]);

答案 1 :(得分:0)

基于最新的Azure 列表Blob REST API响应格式(最后更新时间为2017年8月23日),response body is in XML format

您需要实现自定义函数或使用某些npm包(例如xmltojson)将响应正文从XML转换为JSON格式。

参考:

Azure List Blobs REST Response

答案 2 :(得分:0)

这可能不是最好的解决方案,但这就是我所做的...

我已经在Mac上安装了Microsoft Azure Storage Explorer。所有文件都已经上传-基本上只需选择所有文件,右键单击并复制-创建一个新的.json文件并粘贴!只需整理一下代码,就可以了。

{
    "CloudHub.Azure.Storage.Blobs": 
    {
            "connectionString": "BlobEndpoint=https://mystorage.blob.core.windows.net;SharedAccessSignature=....",
            "containerName": "mycontainer",
            "accountUri": "https://mystorage.blob.core.windows.net",
            "sourceFolder": "",
            "items": [{
              "relativePath": "Image1.JPG",
              "snapshot": ""
            }, {
              "relativePath": "Image2.JPG",
              "snapshot": ""
            }, {
              "relativePath": "Image3.JPG",
              "snapshot": ""
            }],
            "sasToken": "SAS-TOKEN....."
          }
        }

这不是提取动态Blob列表的好方法。我的要求是提取容器中上传的所有照片的列表,我只需要相对路径。

干杯..