-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
252 lines (218 loc) · 11.7 KB
/
index.html
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Azure Storage Client</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h3>Select Storage Account</h3>
<p>
<label><b>Storage account:</b> </label> <input type="text" id="account" value="mfowptestimages"/>
<label><b>SAS Token:</b> </label> <input type="text" id="sas" value="?sv=2018-03-28&ss=b&srt=sco&sp=rwdlac&se=2020-01-31T23:28:59Z&st=2019-07-14T14:28:59Z&spr=https&sig=9TbINuyUJ75kB451RGiOnB8vaTWmIXdmzxygd%2FUqmYA%3D"/>
</p>
<p>List of containers in the storage account:</p>
<ul>
<li><p> Click <button class="btn btn-xs btn-primary" onclick="refreshContainer()">ListContainers</button> button to view the container list under your Azure Storage account</p></li>
<li><p> Click "<b>Select</b>" button to select and enable blob operations</p></li>
</ul>
<div id="containers"></div>
<h3>Blob Operations</h3>
<p><b><label>Selected container name: </label></b> <input type="text" id="container" disabled=true/></p>
<ul>
<li><p>Click <button class="btn btn-xs btn-primary" onclick="refreshBlobList()">ListBlobs</button> button to view the blobs under your selected container</p></li>
<li>
<p>
<input type="file" id="files" name="file" onclick="displayProcess(0)" />
</p>
<p>
Click <button id="upload-button" class="btn btn-xs btn-primary" onclick="uploadBlobByStream(false)">UploadBlob</button> button to upload a local file to current container after selecting a file:
</p>
</li>
<li><p> Click "<b>Delete</b>" button to delete the blob</p></li>
<li><p> Click "<b>Download</b>" link to download a blob to local</p></li>
</ul>
<hr/>
<div>
<p><b><label>Last File Uploaded: </label></b> <input type="text" size="150" id="lastupload" disabled=true/></p>
</div>
<div> Uploaded Bytes: <font id="read"> </font> </div>
<div class="progress">
<div id="progress" class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 0%;">
0%
</div>
</div>
<div id="result"></div>
</div>
<script src="./bundle/azure-storage.blob.js"></script>
<script>
var account = document.getElementById('account').value;
var sas = document.getElementById('sas').value;
var container = '';
var blobUri = '';
function checkParameters() {
account = document.getElementById('account').value;
sas = document.getElementById('sas').value;
if (account == null || account.length < 1)
{
alert('Please enter a valid storage account name!');
return false;
}
if (sas == null || sas.length < 1)
{
alert('Please enter a valid SAS Token!');
return false;
}
return true;
}
function getBlobService() {
if (!checkParameters())
return null;
blobUri = 'https://' + account + '.blob.core.windows.net';
var blobService = AzureStorage.Blob.createBlobServiceWithSas(blobUri, sas).withFilter(new AzureStorage.Blob.ExponentialRetryPolicyFilter());
return blobService;
}
function refreshContainer() {
var blobService = getBlobService();
if (!blobService)
return;
document.getElementById('containers').innerHTML = 'Loading...';
blobService.listContainersSegmented(null, function (error, results) {
if (error) {
alert('List container error, please open browser console to view detailed error');
console.log(error);
} else {
var output = [];
output.push('<tr>',
'<th>ContainerName</th>',
'<th>Operations</th>',
'</tr>');
if (results.entries.length < 1) {
output.push('<tr><td>Empty results...</td></tr>');
}
for (var i = 0, container; container = results.entries[i]; i++) {
output.push('<tr>',
'<td>', container.name, '</td>',
'<td>', '<button class="btn btn-xs btn-danger" onclick="deleteContainer(\'', container.name ,'\')">Delete</button> ',
'<button class="btn btn-xs btn-success" onclick="viewContainer(\'', container.name ,'\')">Select</button>', '</td>',
'</tr>');
}
document.getElementById('containers').innerHTML = '<table class="table table-condensed table-bordered">' + output.join('') + '</table>';
}
});
}
function viewContainer(selectedContainer) {
container = selectedContainer;
document.getElementById('container').value = container;
// alert('Selected ' + container + ' !');
refreshBlobList();
}
function refreshBlobList() {
var blobService = getBlobService();
if (!blobService)
return;
document.getElementById('result').innerHTML = 'Loading...';
blobService.createContainerIfNotExists(container, function(error, result) {
if (error) {
alert('createContainerIfNotExists error, please open browser console to view detailed error');
console.log(error);
} else {
blobService.listBlobsSegmented(container, null, function (error, results) {
if (error) {
alert('List blob error, please open browser console to view detailed error');
console.log(error);
} else {
var output = [];
output.push('<tr>',
'<th>BlobName</th>',
'<th>ContentLength</th>',
'<th>LastModified</th>',
'<th>Operations</th>',
'</tr>');
if (results.entries.length < 1) {
output.push('<tr><td>Empty results...</td></tr>');
}
for (var i = 0, blob; blob = results.entries[i]; i++) {
output.push('<tr>',
'<td>', blob.name, '</td>',
'<td>', blob.contentLength, '</td>',
'<td>', blob.lastModified, '</td>',
'<td>', '<button class="btn btn-xs btn-danger" onclick="deleteBlob(\'', blob.name ,'\')">Delete</button> ',
'<a class="btn btn-xs btn-success" href="', blobUri + '/' + container + '/' + blob.name + sas, '" download>Download</a>' , '</td>',
'</td>',
'</tr>');
}
document.getElementById('result').innerHTML = '<table class="table table-condensed table-bordered">' + output.join('') + '</table>';
}
});
}
})
}
function deleteBlob(blob) {
var blobService = getBlobService();
if (!blobService)
return;
blobService.deleteBlobIfExists(container, blob, function(error, result) {
if (error) {
alert('Delete blob failed, open browser console for more detailed info.');
console.log(error);
} else {
alert('Delete ' + blob + ' successfully!');
refreshBlobList();
}
});
}
function displayProcess(process) {
document.getElementById('progress').style.width = process + '%';
document.getElementById('progress').innerHTML = process + '%';
}
function uploadBlobByStream(checkMD5) {
var files = document.getElementById('files').files;
if (!files.length) {
alert('Please select a file!');
return;
}
var identifier = Math.random().toString().replace(/0\./, '');
var file = files[0];
var blobService = getBlobService();
if (!blobService)
return;
var btn = document.getElementById('upload-button');
btn.disabled = true;
btn.innerHTML = 'Uploading';
// Make a smaller block size when uploading small blobs
var blockSize = file.size > 1024 * 1024 * 32 ? 1024 * 1024 * 4 : 1024 * 512;
var options = {
storeBlobContentMD5 : checkMD5,
blockSize : blockSize
};
blobService.singleBlobPutThresholdInBytes = blockSize;
var finishedOrError = false;
var filename = identifier + '-' + file.name;
filename = filename.replace(/ /g,"_");
var speedSummary = blobService.createBlockBlobFromBrowserFile(container, filename, file, options, function(error, result, response) {
finishedOrError = true;
btn.disabled = false;
btn.innerHTML = 'UploadBlob';
if (error) {
alert('Upload failed, open browser console for more detailed info.');
console.log(error);
displayProcess(0);
} else {
displayProcess(100);
setTimeout(function() { // Prevent alert from stopping UI progress update
// alert('Upload successfully!');
}, 1000);
document.getElementById('lastupload').value = blobUri + '/' + container + '/' + filename;
refreshBlobList();
}
});
speedSummary.on('progress', function () {
var process = speedSummary.getCompletePercent();
displayProcess(process);
});
}
</script>
</body>
</html>