-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCod50101.BlobServiceAPI.al
176 lines (163 loc) · 8.37 KB
/
Cod50101.BlobServiceAPI.al
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
codeunit 50101 "MME Blob Service API"
{
procedure ListContainers(var containers: Record "MME BlobStorage Container" temporary): Boolean
var
blobStorageAccount: Record "MME BlobStorage Account";
client: HttpClient;
response: HttpResponseMessage;
xmlContent: Text;
xmlDoc: XmlDocument;
root: XmlElement;
nodes: XmlNodeList;
node: XmlNode;
i: Integer;
begin
if not blobStorageAccount.FindFirst() then exit;
//GET https://<accountname>.blob.core.windows.net/?comp=list&<sastoken>
if not client.Get(StrSubstNo('%1?comp=list&%2', blobStorageAccount."Account Url", blobStorageAccount."SaS Token"), response) then begin
response.Content.ReadAs(xmlContent);
exit;
end;
if not response.Content().ReadAs(xmlContent) then exit;
if not XmlDocument.ReadFrom(xmlContent, xmlDoc) then exit;
xmlDoc.GetRoot(root);
root.WriteTo(xmlContent);
if not root.SelectNodes('/*/Containers/Container/Name', nodes) then exit;
for i := 1 to nodes.Count() do begin
nodes.Get(i, node);
containers.Init();
containers.Name := node.AsXmlElement().InnerText();
if root.SelectSingleNode(StrSubstNo('/*/Containers/Container[%1]/Properties/Last-Modified', i), node) then
containers."Last Modified" := node.AsXmlElement().InnerText();
if root.SelectSingleNode(StrSubstNo('/*/Containers/Container[%1]/Properties/LeaseStatus', i), node) then
containers."Lease Status" := node.AsXmlElement().InnerText();
if root.SelectSingleNode(StrSubstNo('/*/Containers/Container[%1]/Properties/LeaseState', i), node) then
containers."Lease State" := node.AsXmlElement().InnerText();
containers.Insert();
end;
exit(true);
end;
procedure ListBlobs(containerName: Text; var blobs: Record "MME BlobStorage Blob" temporary): Boolean
var
blobStorageAccount: Record "MME BlobStorage Account";
client: HttpClient;
response: HttpResponseMessage;
xmlContent: Text;
xmlDoc: XmlDocument;
root: XmlElement;
nodes: XmlNodeList;
node: XmlNode;
i: Integer;
len: Integer;
begin
if not blobStorageAccount.FindFirst() then exit;
//GET https://<accountname>.blob.core.windows.net/<container>?restype=container&comp=list&<sastoken>
if not client.Get(StrSubstNo('%1/%2?restype=container&comp=list&%3', blobStorageAccount."Account Url", containerName, blobStorageAccount."SaS Token"), response) then exit;
if not response.Content().ReadAs(xmlContent) then exit;
if not XmlDocument.ReadFrom(xmlContent, xmlDoc) then exit;
xmlDoc.GetRoot(root);
root.WriteTo(xmlContent);
if not root.SelectNodes('/*/Blobs/Blob/Name', nodes) then exit;
for i := 1 to nodes.Count() do begin
nodes.Get(i, node);
blobs.Init();
blobs.Container := containerName;
blobs.Name := node.AsXmlElement().InnerText();
if root.SelectSingleNode(StrSubstNo('/*/Blobs/Blob[%1]/Properties/Last-Modified', i), node) then
blobs."Last Modified" := node.AsXmlElement().InnerText();
if root.SelectSingleNode(StrSubstNo('/*/Blobs/Blob[%1]/Properties/LeaseStatus', i), node) then
blobs."Lease Status" := node.AsXmlElement().InnerText();
if root.SelectSingleNode(StrSubstNo('/*/Blobs/Blob[%1]/Properties/LeaseState', i), node) then
blobs."Lease State" := node.AsXmlElement().InnerText();
if root.SelectSingleNode(StrSubstNo('/*/Blobs/Blob[%1]/Properties/Creation-Time', i), node) then
blobs."Creation Time" := node.AsXmlElement().InnerText();
if root.SelectSingleNode(StrSubstNo('/*/Blobs/Blob[%1]/Properties/BlobType', i), node) then
blobs."Blob Type" := node.AsXmlElement().InnerText();
if root.SelectSingleNode(StrSubstNo('/*/Blobs/Blob[%1]/Properties/Content-Type', i), node) then
blobs."Content Type" := node.AsXmlElement().InnerText();
if root.SelectSingleNode(StrSubstNo('/*/Blobs/Blob[%1]/Properties/Content-Length', i), node) then
if Evaluate(len, node.AsXmlElement().InnerText()) then
blobs."Content Length" := len;
blobs.Insert();
end;
exit(true);
end;
procedure GetBlob(containerName: Text; blobName: Text; var text: Text): Boolean
var
blobStorageAccount: Record "MME BlobStorage Account";
client: HttpClient;
response: HttpResponseMessage;
begin
if not blobStorageAccount.FindFirst() then exit(false);
//GET https://<accountname>.blob.core.windows.net/<container>/<blob>?<sastoken>
if not client.Get(StrSubstNo('%1/%2/%3?%4', blobStorageAccount."Account Url", containerName, blobName, blobStorageAccount."SaS Token"), response) then exit(false);
exit(response.Content().ReadAs(text));
end;
procedure GetBlob(containerName: Text; blobName: Text; var stream: InStream): Boolean
var
blobStorageAccount: Record "MME BlobStorage Account";
client: HttpClient;
response: HttpResponseMessage;
begin
if not blobStorageAccount.FindFirst() then exit(false);
//GET https://<accountname>.blob.core.windows.net/<container>/<blob>?<sastoken>
if not client.Get(StrSubstNo('%1/%2/%3?%4', blobStorageAccount."Account Url", containerName, blobName, blobStorageAccount."SaS Token"), response) then exit(false);
exit(response.Content().ReadAs(stream))
end;
procedure PutBlob(containerName: Text; blobName: Text; var text: Text): Boolean
var
blobStorageAccount: Record "MME BlobStorage Account";
memoryStream: Codeunit "MemoryStream Wrapper";
client: HttpClient;
response: HttpResponseMessage;
content: HttpContent;
headers: HttpHeaders;
len: Integer;
begin
if not blobStorageAccount.FindFirst() then exit;
// Write the test into HTTP Content and change the needed Header Information
content.WriteFrom(text);
content.GetHeaders(headers);
headers.Remove('Content-Type');
headers.Add('Content-Type', 'application/octet-stream');
headers.Add('Content-Length', StrSubstNo('%1', StrLen(text)));
headers.Add('x-ms-blob-type', 'BlockBlob');
//PUT https://<accountname>.blob.core.windows.net/<container>/<blob>?<sastoken>
exit(client.Put(StrSubstNo('%1/%2/%3?%4', blobStorageAccount."Account Url", containerName, blobName, blobStorageAccount."SaS Token"), content, response));
end;
procedure PutBlob(containerName: Text; blobName: Text; var stream: InStream): Boolean
var
blobStorageAccount: Record "MME BlobStorage Account";
memoryStream: Codeunit "MemoryStream Wrapper";
client: HttpClient;
response: HttpResponseMessage;
content: HttpContent;
headers: HttpHeaders;
len: Integer;
begin
if not blobStorageAccount.FindFirst() then exit;
client.SetBaseAddress(blobStorageAccount."Account Url");
// Load the memory stream and get the size
memoryStream.Create(0);
memoryStream.ReadFrom(stream);
len := memoryStream.Length();
memoryStream.SetPosition(0);
memoryStream.GetInStream(stream);
// Write the Stream into HTTP Content and change the needed Header Information
content.WriteFrom(stream);
content.GetHeaders(headers);
headers.Remove('Content-Type');
headers.Add('Content-Type', 'application/octet-stream');
headers.Add('Content-Length', StrSubstNo('%1', len));
headers.Add('x-ms-blob-type', 'BlockBlob');
//PUT https://<accountname>.blob.core.windows.net/<container>/<blob>?<sastoken>
exit(client.Put(StrSubstNo('%1/%2/%3?%4', blobStorageAccount."Account Url", containerName, blobName, blobStorageAccount."SaS Token"), content, response));
end;
procedure GetBlobUrl(containerName: Text; blobName: Text): Text
var
blobStorageAccount: Record "MME BlobStorage Account";
begin
if not blobStorageAccount.FindFirst() then exit;
exit(StrSubstNo('%1/%2/%3?%4', blobStorageAccount."Account Url", containerName, blobName, blobStorageAccount."SaS Token"));
end;
}