获取 JSON 格式的 Azure Blob 数据



我有以下代码来获取我的 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;
});
};
}]);

在我获得 XML 的response.data中:

<?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 格式获取所有这些,以便能够在我的角度应用程序中使用它?

基于最新的 Azure列表 BlobsREST API 响应格式(最后更新于 2017 年 8 月 23 日(,response body is in XML format.

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

参考:

Azure List Blobs REST 响应

到目前为止,我的(临时?(解决方案是使用自定义函数在 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;
});
};
}]);

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

我已经在 Mac 上安装了 Microsoft Azure 存储资源管理器。所有文件都已经上传 - 基本上只需全选,右键单击并复制 - 创建一个新的 .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 列表的好解决方案。我的要求是提取我在容器中上传的所有照片的列表,我只需要相对路径。

干杯。。

最新更新