-
Notifications
You must be signed in to change notification settings - Fork 2
/
YiiAzure.php
277 lines (228 loc) · 8.18 KB
/
YiiAzure.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
<?php
/**
* LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* PHP version 5
*
* @category Microsoft Azure
* @package Yii-Azure
* @author Giuliano Iacobelli <[email protected]>
* @copyright 2012 Stamplay
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link http://www.windowsazure.com/en-us/develop/php/how-to-guides/blob-service/
*/
Yii::setPathOfAlias('WindowsAzure', Yii::getPathOfAlias('ext.yii-azure.lib'));
class YiiAzure extends CApplicationComponent {
// set the consumer key and secret
public $config = array();
/**
* @var string Storage Account name
*/
public $storageAccountName;
/**
* @var string Storage Account key
*/
public $storageAccountKey;
/**
* @var string protocol
*/
public $protocol;
/**
* @var bool instance of the SendGrid library
*/
private $_blobRestProxy;
/**
* Returns services settings declared in the authorization classes.
* For perfomance reasons it uses Yii::app()->cache to store settings array.
* @return array services settings.
*/
public function getConfig() {
if (Yii::app()->hasComponent('cache'))
$config = Yii::app()->cache->get('azure.config');
if (!isset($config) || !is_array($config)) {
$config = array();
foreach ($this->config as $configElem => $value) {
$config[$configElem] = $value;
}
if (Yii::app()->hasComponent('cache'))
Yii::app()->cache->set('azure.config', $config);
}
return $config;
}
/*
* Returns a BlobRestProxy object, this lets you operate with your storage.
* This Object let's you create, manage and delete Containers, Block Blob and Page Blob.
* @return BlobRestProxy object.
*/
private function getBlobRestProxy() {
$connectionString ='DefaultEndpointsProtocol='.$this->protocol.';'
.'AccountName='.$this->storageAccountName.';'
.'AccountKey='.$this->storageAccountKey;
// Create blob REST proxy.
$blobRestProxy = WindowsAzure\Common\ServicesBuilder::getInstance()->createBlobService($connectionString);
return $blobRestProxy;
}
/*
* This method creates a new block blob on a given container
* located in the current storage account.
* @param string $container The name of the container.
* @param string $blob_name The name of the blob.
* @param string|resource $content The content of the blob.
*
* @return CopyBlobResult
*/
public function createBlockBlob($container, $blob_name, $content) {
$blobRestProxy = $this->getBlobRestProxy();
try {
//Upload blob
$CopyBlobResult = $blobRestProxy->createBlockBlob($container, $blob_name, $content);
$result = $CopyBlobResult->getETag();
}
catch(ServiceException $e){
$this->handleError($e);
}
return $result;
}
/**
* Reads or downloads a blob from the system, including its metadata and
* properties.
* In order to save your file on disk you can access the
* resource stream with $blob->getContentStream()
* file_put_contents($new_file_path, $blob->getContentStream());
*
* @param string $container name of the container
* @param string $blob name of the blob
* @param Models\GetBlobOptions $options optional parameters
*
* @return Models\GetBlobResult
*/
public function getBlob($container, $blob, $options = null) {
$blobRestProxy = $this->getBlobRestProxy();
try {
// Get blob.
$blob = $blobRestProxy->getBlob($container, $blob, $options);
return $blob;
}
catch(ServiceException $e){
$this->handleError($e);
}
}
/**
* Deletes a blob or blob snapshot.
* @param string $container name of the container
* @param string $blob name of the blob
* @param Models\DeleteBlobOptions $options optional parameters
*
* @return none
*/
public function deleteBlob($container, $blobName, $options = null) {
$blobRestProxy = $this->getBlobRestProxy();
try {
// Delete container.
$blobRestProxy->deleteBlob($container, $blobName, $options);
}
catch(ServiceException $e){
$this->handleError($e);
}
}
/**
* Lists all of the blobs in the given container.
*
* After retrieving you can iterate over blob array
* foreach($blobs as $blob)
* echo $blob->getName().": ".$blob->getUrl();
*
* @param string $container The container name.
* @param Models\ListBlobsOptions $options The optional parameters.
*
* @array Blobs
*/
public function listBlobs($container, $options = null) {
$blobRestProxy = $this->getBlobRestProxy();
try {
// List blobs.
$blob_list = $blobRestProxy->listBlobs($container, $options);
$blobs = $blob_list->getBlobs();
return $blobs;
}
catch(ServiceException $e){
$this->handleError($e);
}
}
/**
* Creates a new container in the given storage account.
* When creating a container, you can set options on the container, but doing so is not required.
*
* @param string $container The container name.
* @param array $options The optional parameters.
*
* @return none
*/
public function createContainer($name, $metadata=array()) {
$blobRestProxy = $this->getBlobRestProxy();
// OPTIONAL: Set public access policy and metadata.
// Create container options object.
$createContainerOptions = new WindowsAzure\Blob\Models\CreateContainerOptions();
// Set public access policy. Possible values are
// PublicAccessType::CONTAINER_AND_BLOBS and PublicAccessType::BLOBS_ONLY.
// CONTAINER_AND_BLOBS:
// Specifies full public read access for container and blob data.
// proxys can enumerate blobs within the container via anonymous
// request, but cannot enumerate containers within the storage account.
//
// BLOBS_ONLY:
// Specifies public read access for blobs. Blob data within this
// container can be read via anonymous request, but container data is not
// available. proxys cannot enumerate blobs within the container via
// anonymous request.
// If this value is not specified in the request, container data is
// private to the account owner.
$createContainerOptions->setPublicAccess(WindowsAzure\Blob\Models\PublicAccessType::CONTAINER_AND_BLOBS);
// Set container metadata
foreach($metadata as $key=>$value)
$createContainerOptions->addMetaData($key, $value);
try {
// Create container.
$response = $blobRestProxy->createContainer($name, $createContainerOptions);
}
catch(ServiceException $e){
$this->handleError($e);
}
}
/**
* Creates a new container in the given storage account.
*
* @param string $container The container name.
* @param Models\DeleteContainerOptions $options The optional parameters.
*
* @return none
*
*/
public function deleteContainer($container, $options = null) {
$blobRestProxy = $this->getBlobRestProxy();
try {
// Delete container.
$blobRestProxy->deleteContainer($container);
}
catch(ServiceException $e){
$this->handleError($e);
}
}
private function handleError($e) {
// Handle exception based on error codes and messages.
// Error codes and messages are here:
// http://msdn.microsoft.com/en-us/library/windowsazure/dd179439.aspx
$code = $e->getCode();
$error_message = $e->getMessage();
Yii::log($code.": ".$error_message,CLogger::LEVEL_ERROR,'ext.yii-azure');
}
}