This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
/
trial.php
233 lines (228 loc) · 8.34 KB
/
trial.php
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
<?php
/*
This code sample is separated into two tiers.
The frontend is built using AngularJs. The AngularJs source code scripts are in `src` folder, and be combined br gulp.
The backend is built using PHP, implementing with Azure SDK for PHP.
All the operations on Azure Blob Storage in the frontend, are called via `$http` module against the functions in PHP backend.
////////////////////////////////////////
////////////////////////////////////////
All the functions called via frontend are listed in this single script.
*/
use MicrosoftAzure\Storage\Blob\Models\Block;
use MicrosoftAzure\Storage\Blob\Models\CommitBlobBlocksOptions;
use MicrosoftAzure\Storage\Blob\Models\CreateBlobOptions;
use MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions;
use MicrosoftAzure\Storage\Blob\Models\GetBlobOptions;
use MicrosoftAzure\Storage\Blob\Models\ListBlobsOptions;
use MicrosoftAzure\Storage\Blob\Models\ListContainersOptions;
use MicrosoftAzure\Storage\Blob\Models\PublicAccessType;
use MicrosoftAzure\Storage\Common\ServiceException;
require_once 'settings.php';
if (isset($_GET['init'])) {
// here are 4 functions totally, for the preparatory work of the complete trial cycle.
switch ($_GET['init']) {
case 'container':
//create the container for the code sample's trial later.
$createContainerOptions = new CreateContainerOptions();
$createContainerOptions->setPublicAccess(PublicAccessType::CONTAINER_AND_BLOBS);
$createContainerOptions->addMetadata('category', 'init-trial-container'); // add a custom metadata
try {
// Create container.
$blobClient->createContainer($containerName, $createContainerOptions);
echo "Create successfully";
} catch (ServiceException $e) {
$code = $e->getCode();
http_response_code($code);
$error_message = $e->getMessage();
echo $error_message;
}
break;
case 'text':
//upload the text blob to the container created above for the code sample's trial later.
$chunckSize = 2 * 1024;
$trialCate = "trial-text-blobs";
$blobName = "trial.md";
$createBlobOptions = new CreateBlobOptions();
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$createBlobOptions->setMetadata(["category" => $trialCate]);
$createBlobOptions->setContentType(finfo_file($finfo, "assets/" . $blobName));
finfo_close($finfo);
try {
$content = fopen("assets/trial.md", "rb");
$counter = 1;
while (!feof($content)) {
$data = fread($content, $chunckSize);
$blobClient->createBlockBlob($containerName, $trialCate . DELIMITER . $blobName . '-part-' . $counter, $data, $createBlobOptions);
$counter++;
}
fclose($content);
// Create container.
echo "successfully create " . ($counter - 1) . " blobs";
} catch (ServiceException $e) {
$code = $e->getCode();
http_response_code($code);
$error_message = $e->getMessage();
echo $error_message;
}
break;
case 'video':
//upload the video blob to the container created above for the code sample's trial later.
try {
$chunkSize = 4 * 1024 * 1024;
$trialCate = "trial-video-blobs";
$blobName = "video.mp4";
$content = fopen("assets/" . $blobName, "rb");
$index = 0;
$continue = True;
$counter = 1;
$blockIds = array();
while (!feof($content)) {
$blockId = str_pad($counter, 6, "0", STR_PAD_LEFT);
$block = new Block();
$block->setBlockId(base64_encode($blockId));
$block->setType("Uncommitted");
array_push($blockIds, $block);
$data = fread($content, $chunkSize);
$blobClient->createBlobBlock($containerName, $trialCate . DELIMITER . $blobName, base64_encode($blockId), $data);
$counter++;
}
fclose($content);
$commitBlobBlocksOptions = new CommitBlobBlocksOptions();
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$commitBlobBlocksOptions->setMetadata(["category" => $trialCate]);
$commitBlobBlocksOptions->setBlobContentType(finfo_file($finfo, "assets/" . $blobName));
finfo_close($finfo);
$blobClient->commitBlobBlocks($containerName, $trialCate . DELIMITER . $blobName, $blockIds, $commitBlobBlocksOptions);
echo "successfully create 1 blob";
} catch (ServiceException $e) {
$code = $e->getCode();
http_response_code($code);
$error_message = $e->getMessage();
echo $error_message;
}
break;
case 'check':
// check if has initialized the test container
try {
$listContainersOptions = new ListContainersOptions();
$listContainersOptions->setPrefix($trialPrefix);
$listContainersOptions->setIncludeMetadata(true);
$result = $blobClient->listContainers($listContainersOptions);
$container = $result->getContainers();
if (count($container) > 0) {
$result = $blobClient->listBlobs($container[0]->getName());
echo count($result->getBlobs()) > 0 ? 'true' : 'false';
} else {
echo 'false';
}
} catch (ServiceException $e) {
$code = $e->getCode();
http_response_code($code);
$error_message = $e->getMessage();
echo $error_message;
}
default:
# code...
break;
}
}
if (isset($_GET['trial'])) {
switch ($_GET['trial']) {
case 'container':
//
try {
$listContainersOptions = new ListContainersOptions();
$listContainersOptions->setPrefix($trialPrefix);
$listContainersOptions->setIncludeMetadata(true);
$result = $blobClient->listContainers($listContainersOptions);
$return = [];
foreach ($containers = $result->getContainers() as $k => $b) {
// $containers is an instance of MicrosoftAzure\Storage\Blob\Models\Container
$tmp = [];
$tmp['name'] = $b->getName();
$tmp['url'] = $b->getUrl();
$tmp['metadata'] = $b->getMetadata();
$properties = $b->getProperties();
// $properties is an instance of MicrosoftAzure\Storage\Blob\Models\ContainerProperties
$tmp['properties']['lastModified'] = $properties->getLastModified()->format('Y-m-d H:i:s');
array_push($return, $tmp);
}
echo json_encode($return);
} catch (ServiceException $e) {
$code = $e->getCode();
http_response_code($code);
$error_message = $e->getMessage();
echo $error_message;
}
break;
case 'blob':
//
$listBlobsOptions = new ListBlobsOptions();
if (isset($_GET['blobPrefixes'])) {
$listBlobsOptions->setPrefix($_GET['blobPrefixes']);
}
if (isset($_GET['next'])) {
$listBlobsOptions->setMarker($_GET['next']);
}
$listBlobsOptions->setMaxResults(5);//pagination
$listBlobsOptions->setIncludeMetadata(true);
$listBlobsOptions->setDelimiter(DELIMITER);
$result = $blobClient->listBlobs($_GET['containerName'], $listBlobsOptions);
$return = [
'blobs' => [],
'delimiter' => $result->getDelimiter(),
'blobPrefixes' => [],
'prefixes' => $result->getPrefix(),
'nextMarker' => $result->getNextMarker(),
];
foreach ($result->getBlobPrefixes() as $k => $b) {
//an instance of MicrosoftAzure\Storage\Blob\Models\BlobPrefix
array_push($return['blobPrefixes'], $b->getName());
}
foreach ($result->getBlobs() as $k => $b) {
//an instance of MicrosoftAzure\Storage\Blob\Models\Blob
$tmp = [];
$tmp['name'] = $b->getName();
$tmp['url'] = $b->getUrl();
$tmp['metadata'] = $b->getMetadata();
$properties = $b->getProperties();
$tmp['properties']['lastModified'] = $properties->getLastModified()->format('Y-m-d H:i:s');
$tmp['properties']['contentType'] = $properties->getContentType();
$tmp['properties']['contentLength'] = $properties->getContentLength();
array_push($return['blobs'], $tmp);
}
echo json_encode($return);
break;
case 'content':
// output the content of blob in stream
$properties = $blobClient->getBlobProperties($containerName, $_GET['blob']);
$size = $properties->getProperties()->getContentLength();
$mime = $properties->getProperties()->getContentType();
$chunk_size = 1024 * 1024;
$index = 0;
//$stream = "";
// header("Pragma: public");
// header('Content-Disposition: attachment; filename="' . $blobName . '"');
// header('Expires: 0');
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Transfer-Encoding: binary");
header("Content-type: $mime");
header("Content-length: $size");
ob_clean();
while ($index < $size) {
$option = new GetBlobOptions();
$option->setRangeStart($index);
$option->setRangeEnd($index + $chunk_size - 1);
$blob = $blobClient->getBlob($containerName, $_GET['blob'], $option);
$stream_t = $blob->getContentStream();
$length = $blob->getProperties()->getContentLength();
$index += $length;
flush();
fpassthru($stream_t);
}
break;
default:
# code...
break;
}
}