-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwindows-azure-storage-util.php
627 lines (578 loc) · 21.1 KB
/
windows-azure-storage-util.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
<?php
/**
* windows-azure-storage-util.php
*
* Various utility functions for accessing Windows Azure Storage
*
* Version: 2.0
*
* Author: Microsoft Open Technologies, Inc.
*
* Author URI: http://www.microsoft.com/
*
* License: New BSD License (BSD)
*
* Copyright (c) Microsoft Open Technologies, Inc.
* All rights reserved.
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* PHP Version 5
*
* @category WordPress_Plugin
* @package Windows_Azure_Storage_For_WordPress
* @author Microsoft Open Technologies, Inc. <[email protected]>
* @copyright Microsoft Open Technologies, Inc.
* @license New BSD license, (http://www.opensource.org/licenses/bsd-license.php)
* @link http://www.microsoft.com
*/
require_once "library/WindowsAzure/WindowsAzure.php";
// include path to dependencies in the include_path
$path = dirname(__FILE__) . '/library/dependencies';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
// import namepaces required for consuming Azure Blob Storage
use WindowsAzure\Blob\BlobService;
use WindowsAzure\Blob\BlobSettings;
use WindowsAzure\Blob\Models\CreateContainerOptions;
use WindowsAzure\Blob\Models\PublicAccessType;
use WindowsAzure\Common\ServiceException;
use WindowsAzure\Common\ServicesBuilder;
use windowsazure\common\internal\resources;
use WindowsAzure\Blob\Models\Block;
use WindowsAzure\Blob\Models\BlobBlockType;
use WindowsAzure\Blob\Models\CreateBlobOptions;
use WindowsAzure\Blob\Models\CommitBlobBlocksOptions;
use WindowsAzure\Blob\Models\ContainerAcl;
use WindowsAzure\Common\Internal\IServiceFilter;
/**
* Used for performing operations on Windows Azure Blob Storage
*
* @category WordPress_Plugin
* @package Windows_Azure_Storage_For_WordPress
* @author Microsoft Open Technologies, Inc. <[email protected]>
* @copyright Microsoft Open Technologies, Inc.
* @license New BSD license, (http://www.opensource.org/licenses/bsd-license.php)
* @link http://www.microsoft.com
*/
class WindowsAzureStorageUtil
{
/**
* Maximal blob size (in bytes)
*/
const MAX_BLOB_SIZE = 67108864;
/**
* Maximal block blob transfer size (in bytes)
*/
const MAX_BLOB_TRANSFER_SIZE = 4194304;
/**
* Get Windows Azure Storage host name defined as per plugin settings
*
* @return string host Name
*/
public static function getHostName()
{
$storageAccountName = WindowsAzureStorageUtil::getAccountName();
if ($storageAccountName == 'devstoreaccount1') {
// Use development storage
$hostName = Resources::EMULATOR_BLOB_URI;
} else {
// Use cloud storage
$hostName = Resources::BLOB_BASE_DNS_NAME;
}
// Remove http/https from the beginning
if (substr($hostName, 0, 4) == "http") {
$parts = parse_url($hostName);
$hostName = $parts["host"];
if (!empty($parts["port"])) {
$hostName = $hostName . ":" . $parts["port"];
}
}
return $hostName;
}
/**
* Get Windows Azure Storage account name defined in plugin settings
*
* @return string Account Name
*/
public static function getAccountName()
{
return get_option('azure_storage_account_name');
}
/**
* Get Windows Azure Storage account key defined in plugin settings
*
* @return string Account Key
*/
public static function getAccountKey()
{
return get_option('azure_storage_account_primary_access_key');
}
/**
* Get default container name defined in plugin settings
*
* @return string Default container name
*/
public static function getDefaultContainer()
{
return get_option('default_azure_storage_account_container_name');
}
/**
* Get CNAME to be used for blob URL instead of blob.windows.net
*
* @return string CNAME
*/
public static function getCNAME()
{
return get_option('cname');
}
/**
* Get HTTP proxy host if the web server needs http proxy for internet
*
* @return string HTTP proxy host name
*/
public static function getHttpProxyHost()
{
return get_option('http_proxy_host');
}
/**
* Get HTTP proxy port if the web server needs http proxy for internet
*
* @return string HTTP proxy port number
*/
public static function getHttpProxyPort()
{
return get_option('http_proxy_port');
}
/**
* Get HTTP proxy user-name
*
* @return string HTTP proxy user-name
*/
public static function getHttpProxyUserName()
{
return get_option('http_proxy_username');
}
/**
* Get HTTP proxy password
*
* @return string HTTP proxy password
*/
public static function getHttpProxyPassword()
{
return get_option('http_proxy_password');
}
/**
* Create blob storage client using Azure SDK for PHP
*
* @param string $accountName Windows Azure Storage account name
*
* @param string $accountKey Windows Azure Storage account primary key
*
* @param string $proxyHost Http proxy host
*
* @param string $proxyPort Http proxy port
*
* @param string $proxyUserName Http proxy user name
*
* @param string $proxyPassword Http proxy password
*
* @return BlobRestProxy Blob storage client
*/
public static function getStorageClient(
$accountName = null, $accountKey = null,
$proxyHost = null, $proxyPort = null,
$proxyUserName = null, $proxyPassword = null
) {
// Storage Account Settings from db
$storageAccountName = WindowsAzureStorageUtil::getAccountName();
$storageAccountKey = WindowsAzureStorageUtil::getAccountKey();
$httpProxyHost = WindowsAzureStorageUtil::getHttpProxyHost();
$httpProxyPort = WindowsAzureStorageUtil::getHttpProxyPort();
$httpProxyUserName = WindowsAzureStorageUtil::getHttpProxyUserName();
$httpProxyPassword = WindowsAzureStorageUtil::getHttpProxyPassword();
// Parameters take precedence over settings in the db
if ($accountName) {
$storageAccountName = $accountName;
$storageAccountKey = $accountKey;
$httpProxyHost = $proxyHost;
$httpProxyPort = $proxyPort;
$httpProxyUserName = $proxyUserName;
$httpProxyPassword = $proxyPassword;
}
$azureServiceConnectionString = null;
if ($storageAccountName == 'devstoreaccount1') {
// Use development storage
$azureServiceConnectionString = "UseDevelopmentStorage=true";
} else {
// Use cloud storage
$azureServiceConnectionString = "DefaultEndpointsProtocol=http"
. ";AccountName=" . $storageAccountName
. ";AccountKey=" . $storageAccountKey;
}
$blobRestProxy = ServicesBuilder::getInstance()->createBlobService($azureServiceConnectionString);
$httpProxyHost = $httpProxyHost;
if (!empty($httpProxyHost)) {
$proxyFilter = new WindowsAzureStorageProxyFilter($httpProxyHost,
$httpProxyPort,
$httpProxyUserName,
$httpProxyPassword
);
$blobRestProxy = $blobRestProxy->withFilter($proxyFilter);
}
return $blobRestProxy;
}
/**
* Delete a blob from specified container
*
* @param string $containerName Name of the parent container
*
* @param string $blobName Name of the blob to be deleted
*
* @return void
*/
public static function deleteBlob($containerName, $blobName)
{
$blobRestProxy = WindowsAzureStorageUtil::getStorageClient();
if (self::blobExists($containerName, $blobName)) {
$blobRestProxy->deleteBlob($containerName, $blobName);
}
}
/**
* Check if a blob exists
*
* @param string $containerName Name of the parent container
*
* @param string $blobName Name of the blob to be checked
*
* @return boolean
*/
public static function blobExists($containerName, $blobName)
{
try {
$blobRestProxy = WindowsAzureStorageUtil::getStorageClient();
$blobRestProxy->getBlobMetadata($containerName, $blobName);
} catch(ServiceException $e){
return false;
}
return true;
}
/**
* Creates a public container
*
* @param string $containerName Name of the container to create
*
* @param BlobRestProxy $storageClient Reference of storage client to use
*
* @throws ServiceException
*/
public static function createPublicContainer($containerName, $storageClient = null)
{
$containerOptions = new CreateContainerOptions();
$containerOptions->setPublicAccess(PublicAccessType::CONTAINER_AND_BLOBS);
$blobRestProxy = $null;
try
{
if ($storageClient) {
$blobRestProxy = $storageClient;
} else {
$blobRestProxy = WindowsAzureStorageUtil::getStorageClient();
}
$blobRestProxy->createContainer($containerName, $containerOptions);
} catch(ServiceException $e){
throw $e;
}
}
/**
* Get prefix for the blob URL
*
* @param boolean $appendContainer Wheather to append container name at the end
*
* @return string Prefix for the blob URL
*/
public static function getStorageUrlPrefix($appendContainer = true)
{
$azure_storage_account_name = WindowsAzureStorageUtil::getAccountName();
$default_azure_storage_account_container_name
= WindowsAzureStorageUtil::getDefaultContainer();
// Get CNAME if defined
$cname = WindowsAzureStorageUtil::getCNAME();
if (!(empty($cname))) {
if ($appendContainer) {
return $cname . "/" . $default_azure_storage_account_container_name;
} else {
return $cname;
}
} else {
$blobStorageHostName = WindowsAzureStorageUtil::getHostName();
$storageAccountName = WindowsAzureStorageUtil::getAccountName();
if ($storageAccountName == 'devstoreaccount1') {
// Use development storage
if ($appendContainer) {
return 'http://' . $blobStorageHostName
. '/'. $azure_storage_account_name
. '/' . $default_azure_storage_account_container_name;
} else {
return 'http://' . $blobStorageHostName
. '/'. $azure_storage_account_name;
}
} else {
// Use cloud storage
if ($appendContainer) {
return 'http://' . $azure_storage_account_name
. '.' . $blobStorageHostName
. '/' . $default_azure_storage_account_container_name;
} else {
return 'http://' . $azure_storage_account_name
. '.' . $blobStorageHostName;
}
}
}
}
/**
* Upload the given file to an azure storage container as a block blob.
* Block blobs let us upload large blobs efficiently. Block blobs are comprised of blocks,
* each of which is identified by a block ID. This allows create (or modify) a block blob
* by writing a set of blocks and committing them by their block IDs.
* If we are writing a block blob that is no more than 64 MB in size, you can upload it
* in its entirety with a single write operation.
* When you upload a block to a blob in your storage account, it is associated with the
* specified block blob, but it does not become part of the blob until you commit a list
* of blocks that includes the new block's ID.
*
* @param string $containerName Container name
*
* @param string $blobName Blob name
*
* @param string $localFileName Path to local file to be uploaded
*
* @param string $blobContentType Content type of the blob
*
* @param array $metadata Array of metadata
*
* @return void
*
* @throws ServiceException
*/
public static function putBlockBlob($containerName, $blobName, $localFileName, $blobContentType = null, $metadata = array())
{
$copyBlobResult = null;
// Open file
$handle = fopen($localFileName, 'r');
if ($handle === false) {
throw new Exception('Could not open the local file ' . localFileName);
}
$blobRestProxy = WindowsAzureStorageUtil::getStorageClient();
try {
if (filesize($localFileName) < self::MAX_BLOB_SIZE) {
$createBlobOptions = new CreateBlobOptions();
$createBlobOptions->setBlobContentType($blobContentType);
$createBlobOptions->setMetadata($metadata);
$blobRestProxy->createBlockBlob($containerName, $blobName, $handle, $createBlobOptions);
fclose($handle);
} else {
// Determine number of page blocks
$numberOfBlocks = ceil( filesize($localFileName) / self::MAX_BLOB_TRANSFER_SIZE );
// Generate block id's
$blocks = array();
for ($i = 0; $i < $numberOfBlocks; $i++) {
$blocks[$i] = new Block();
$blocks[$i]->setBlockId(self::_generateBlockId($i));
$blocks[$i]->setType(BlobBlockType::LATEST_TYPE);
}
// Upload blocks
for ($i = 0; $i < $numberOfBlocks; $i++) {
// Seek position in file
fseek($handle, $i * self::MAX_BLOB_TRANSFER_SIZE);
// Read contents
$fileContents = fread($handle, self::MAX_BLOB_TRANSFER_SIZE);
// Put block
$blobRestProxy->createBlobBlock($containerName, $blobName, $blocks[$i]->getBlockId(), $fileContents);
// Dispose file contents
$fileContents = null;
unset($fileContents);
}
// Close file
fclose($handle);
// Set Block Blob's content type and metadata
$commitBlockBlobOptions = new CommitBlobBlocksOptions();
$commitBlockBlobOptions->setBlobContentType($blobContentType);
$commitBlockBlobOptions->setMetadata($metadata);
// Commit the block list
$blobRestProxy->commitBlobBlocks($containerName, $blobName, $blocks, $commitBlockBlobOptions);
}
} catch (ServiceException $exception) {
if (!$handle) {
fclose($handle);
}
throw $exception;
}
}
/**
* Create signature
*
* @param string $accountName Account name for Windows Azure
*
* @param string $accountKey Account key for Windows Azure
*
* @param boolean $usePathStyleUri Use path-style URI's
*
* @param string $path Path for the
*
* @param string $resource Signed resource - container (c) - blob (b)
*
* @param string $permissions Signed permissions - read (r), write (w), delete (d) and list (l)
*
* @param string $start The time at which the Shared Access Signature becomes valid.
*
* @param string $expiry The time at which the Shared Access Signature becomes invalid.
*
* @param string $identifier Signed identifier
*
* @return string
*/
public static function createSharedAccessSignature(
$accountName,
$accountKey,
$usePathStyleUri,
$path = '/',
$resource = 'b',
$permissions = 'r',
$start = '',
$expiry = '',
$identifier = ''
) {
$accountKey = base64_decode($accountKey);
// Determine path
if ($usePathStyleUri) {
$path = substr($path, strpos($path, '/'));
}
// Add trailing slash to $path
if (substr($path, 0, 1) !== '/') {
$path = '/' . $path;
}
// Build canonicalized resource string
$canonicalizedResource = '/' . $accountName;
$canonicalizedResource .= $path;
// Create string to sign
$stringToSign = array();
$stringToSign[] = $permissions;
$stringToSign[] = $start;
$stringToSign[] = $expiry;
$stringToSign[] = $canonicalizedResource;
$stringToSign[] = $identifier;
$stringToSign = implode("\n", $stringToSign);
$signature = base64_encode(hash_hmac('sha256', $stringToSign, $accountKey, true));
return $signature;
}
/**
* Generate block id which can be base-64 encoded, the pre-encoded string must be 64
* bytes or less
*
* @param int $part Block number
*
* @return string Windows Azure Blob Storage block number
*/
protected static function _generateBlockId($part = 0)
{
$returnValue = $part;
while (strlen($returnValue) < 64) {
$returnValue = '0' . $returnValue;
}
return $returnValue;
}
}
/**
* Internal class used for redirecting request-response for Http proxy
*
* @category WordPress_Plugin
* @package Windows_Azure_Storage_For_WordPress
* @author Microsoft Open Technologies, Inc. <[email protected]>
* @copyright Microsoft Open Technologies, Inc.
* @license New BSD license, (http://www.opensource.org/licenses/bsd-license.php)
* @link http://www.microsoft.com
*/
class WindowsAzureStorageProxyFilter implements IServiceFilter
{
/**
* Proxy host.
*/
protected $host;
/**
* Proxy port.
*/
protected $port;
/**
* Proxy username.
*/
protected $username;
/**
* Proxy password.
*/
protected $password;
/**
* Create a new instance of WindowsAzureStorageProxyFilter
*
* @param string $host HTTP porxy host.
*
* @param string $port HTTP proxy port.
*
* @param string $username HTTP proxy username.
*
* @param string $password HTTP proxy password.
*/
public function __construct($host, $port, $username, $password) {
$this->host = $host;
$this->port = $port;
$this->username = $username;
$this->password = $password;
}
/**
* Hook to processes HTTP request before send.
*
* @param mix $request HTTP request object.
*
* @return mix processed HTTP request object.
*/
public function handleRequest($request) {
if ($this->host) {
$request->setConfig('proxy_host', $this->host);
if ($this->port) {
$request->setConfig('proxy_port', $this->port);
if ($this->username) {
$request->setConfig('proxy_user', $this->username);
if ($this->password) {
$request->setConfig('proxy_password', $this->password);
}
}
}
}
return $request;
}
/**
* Hook to processes HTTP response after send.
*
* @param mix $request HTTP request object.
* @param mix $response HTTP response object.
*
* @return mix processed HTTP response object.
*/
public function handleResponse($request, $response) {
return $response;
}
}
?>