| 42 | | /** |
|---|
| 43 | | * Action |
|---|
| 44 | | * |
|---|
| 45 | | * Name of the action last performed |
|---|
| 46 | | * |
|---|
| 47 | | * @var string |
|---|
| 48 | | * @access public |
|---|
| 49 | | */ |
|---|
| 50 | | public $action; |
|---|
| 51 | | |
|---|
| 52 | | /** |
|---|
| 53 | | * Service Url |
|---|
| 54 | | * |
|---|
| 55 | | * Amazon S3 Web Service Url used in HTTP requests |
|---|
| 56 | | * |
|---|
| 57 | | * @var string |
|---|
| 58 | | * @access public |
|---|
| 59 | | */ |
|---|
| 60 | | public $serviceUrl = 'http://s3.amazonaws.com/'; |
|---|
| 61 | | |
|---|
| 62 | | /** |
|---|
| 63 | | * Access Key Id |
|---|
| 64 | | * |
|---|
| 65 | | * Amazon S3 Web Services access key id |
|---|
| 66 | | * |
|---|
| 67 | | * @var string |
|---|
| 68 | | * @access public |
|---|
| 69 | | */ |
|---|
| 70 | | public $accessKeyId; |
|---|
| 71 | | |
|---|
| 72 | | /** |
|---|
| 73 | | * Secret Key |
|---|
| 74 | | * |
|---|
| 75 | | * Amazon S3 Web Services secret key |
|---|
| 76 | | * |
|---|
| 77 | | * @var string |
|---|
| 78 | | * @access public |
|---|
| 79 | | */ |
|---|
| 80 | | public $secretKey; |
|---|
| 81 | | |
|---|
| 82 | | /** |
|---|
| 83 | | * Response String |
|---|
| 84 | | * |
|---|
| 85 | | * Http resonse string |
|---|
| 86 | | * |
|---|
| 87 | | * @var string |
|---|
| 88 | | * @access public |
|---|
| 89 | | */ |
|---|
| 90 | | public $responseString; |
|---|
| 91 | | |
|---|
| 92 | | /** |
|---|
| 93 | | * Response Code |
|---|
| 94 | | * |
|---|
| 95 | | * @var mixed |
|---|
| 96 | | * @access public |
|---|
| 97 | | */ |
|---|
| 98 | | public $responseCode; |
|---|
| 99 | | |
|---|
| 100 | | /** |
|---|
| 101 | | * Parsed Xml |
|---|
| 102 | | * |
|---|
| 103 | | * @var array |
|---|
| 104 | | * @access public |
|---|
| 105 | | */ |
|---|
| 106 | | public $parsedXml; |
|---|
| 107 | | |
|---|
| 108 | | /** |
|---|
| 109 | | * BasesfAmazonS3 Constructor |
|---|
| 110 | | * |
|---|
| 111 | | * Requires an $accessKeyId and $secretKey in order to be initialized. |
|---|
| 112 | | * |
|---|
| 113 | | * <code> |
|---|
| 114 | | * <?php |
|---|
| 115 | | * $sfAmazonS3 = new BasesfAmazonS3('accessKeyId', 'secretKey'); |
|---|
| 116 | | * ?> |
|---|
| 117 | | * </code> |
|---|
| 118 | | * |
|---|
| 119 | | * @param string $accessKeyId Access key id provided by Amazon when you sign up for a AmazonS3 account |
|---|
| 120 | | * @param string $secretKey Secret key provided by Amazon when you sign up for a AmazonS3 account |
|---|
| 121 | | */ |
|---|
| 122 | | public function __construct($accessKeyId = null, $secretKey = null) |
|---|
| 123 | | { |
|---|
| 124 | | // If params passed then setup class, otherwise throw exception |
|---|
| 125 | | if( $accessKeyId AND $secretKey ) |
|---|
| | 42 | /** |
|---|
| | 43 | * Action |
|---|
| | 44 | * |
|---|
| | 45 | * Name of the action last performed |
|---|
| | 46 | * |
|---|
| | 47 | * @var string |
|---|
| | 48 | * @access public |
|---|
| | 49 | */ |
|---|
| | 50 | public $action; |
|---|
| | 51 | |
|---|
| | 52 | /** |
|---|
| | 53 | * Service Url |
|---|
| | 54 | * |
|---|
| | 55 | * Amazon S3 Web Service Url used in HTTP requests |
|---|
| | 56 | * |
|---|
| | 57 | * @var string |
|---|
| | 58 | * @access public |
|---|
| | 59 | */ |
|---|
| | 60 | public $serviceUrl = 'http://s3.amazonaws.com/'; |
|---|
| | 61 | |
|---|
| | 62 | /** |
|---|
| | 63 | * Access Key Id |
|---|
| | 64 | * |
|---|
| | 65 | * Amazon S3 Web Services access key id |
|---|
| | 66 | * |
|---|
| | 67 | * @var string |
|---|
| | 68 | * @access public |
|---|
| | 69 | */ |
|---|
| | 70 | public $accessKeyId; |
|---|
| | 71 | |
|---|
| | 72 | /** |
|---|
| | 73 | * Secret Key |
|---|
| | 74 | * |
|---|
| | 75 | * Amazon S3 Web Services secret key |
|---|
| | 76 | * |
|---|
| | 77 | * @var string |
|---|
| | 78 | * @access public |
|---|
| | 79 | */ |
|---|
| | 80 | public $secretKey; |
|---|
| | 81 | |
|---|
| | 82 | /** |
|---|
| | 83 | * Response String |
|---|
| | 84 | * |
|---|
| | 85 | * Http resonse string |
|---|
| | 86 | * |
|---|
| | 87 | * @var string |
|---|
| | 88 | * @access public |
|---|
| | 89 | */ |
|---|
| | 90 | public $responseString; |
|---|
| | 91 | |
|---|
| | 92 | /** |
|---|
| | 93 | * Response Code |
|---|
| | 94 | * |
|---|
| | 95 | * @var mixed |
|---|
| | 96 | * @access public |
|---|
| | 97 | */ |
|---|
| | 98 | public $responseCode; |
|---|
| | 99 | |
|---|
| | 100 | /** |
|---|
| | 101 | * Parsed Xml |
|---|
| | 102 | * |
|---|
| | 103 | * @var array |
|---|
| | 104 | * @access public |
|---|
| | 105 | */ |
|---|
| | 106 | public $parsedXml; |
|---|
| | 107 | |
|---|
| | 108 | /** |
|---|
| | 109 | * BasesfAmazonS3 Constructor |
|---|
| | 110 | * |
|---|
| | 111 | * Requires an $accessKeyId and $secretKey in order to be initialized. |
|---|
| | 112 | * |
|---|
| | 113 | * <code> |
|---|
| | 114 | * <?php |
|---|
| | 115 | * $sfAmazonS3 = new BasesfAmazonS3('accessKeyId', 'secretKey'); |
|---|
| | 116 | * ?> |
|---|
| | 117 | * </code> |
|---|
| | 118 | * |
|---|
| | 119 | * @param string $accessKeyId Access key id provided by Amazon when you sign up for a AmazonS3 account |
|---|
| | 120 | * @param string $secretKey Secret key provided by Amazon when you sign up for a AmazonS3 account |
|---|
| | 121 | */ |
|---|
| | 122 | public function __construct($accessKeyId = null, $secretKey = null) |
|---|
| | 123 | { |
|---|
| | 124 | // If params passed then setup class, otherwise throw exception |
|---|
| | 125 | if( $accessKeyId AND $secretKey ) |
|---|
| | 126 | { |
|---|
| | 127 | $this->accessKeyId = $accessKeyId; |
|---|
| | 128 | $this->secretKey = $secretKey; |
|---|
| | 129 | } |
|---|
| | 130 | else |
|---|
| | 131 | { |
|---|
| | 132 | throw new Exception('You must instantiate BasesfAmazonS3 plugin class with your aws access key id and secret key.'); |
|---|
| | 133 | } |
|---|
| | 134 | } |
|---|
| | 135 | |
|---|
| | 136 | public static function getInstance() |
|---|
| | 137 | { |
|---|
| | 138 | static $sfAmazonS3; |
|---|
| | 139 | |
|---|
| | 140 | if (!isset($sfAmazonS3)) |
|---|
| | 141 | { |
|---|
| | 142 | // Get instance |
|---|
| | 143 | $className = sfConfig::get('app_amazon_s3_class_name'); |
|---|
| | 144 | $sfAmazonS3 = new $className(sfConfig::get('app_amazon_s3_aws_access_key_id'), sfConfig::get('app_amazon_s3_secret_access_key')); |
|---|
| | 145 | } |
|---|
| | 146 | |
|---|
| | 147 | return $sfAmazonS3; |
|---|
| | 148 | } |
|---|
| | 149 | |
|---|
| | 150 | |
|---|
| | 151 | /** |
|---|
| | 152 | * Create Amazon S3 Bucket |
|---|
| | 153 | * |
|---|
| | 154 | * Method for create a blank bucket on Amazon S3, bucket name must be inique across all of Amazon |
|---|
| | 155 | * <code> |
|---|
| | 156 | * <?php |
|---|
| | 157 | * $sfAmazonS3 = new BasesfAmazonS3('accessKeyId', 'secretKey'); |
|---|
| | 158 | * $sfAmazonS3->createBucket('name', 'private'); // 2nd argument can be public-read, public-read-write, private, authenticated-users-read |
|---|
| | 159 | * ?> |
|---|
| | 160 | * </code> |
|---|
| | 161 | * |
|---|
| | 162 | * @param string $bucket Unique name of your bucket to create |
|---|
| | 163 | * @param string $acl Amazon S3 permission setting (private, public-read, public-read-write, authenticated-users-read) |
|---|
| | 164 | * @return mixed $response Returns true if the call was successful, otherwise throws an exception |
|---|
| | 165 | */ |
|---|
| | 166 | public function createBucket($bucket, $acl = 'private') |
|---|
| | 167 | { |
|---|
| | 168 | $this->action = 'create_bucket'; |
|---|
| | 169 | |
|---|
| | 170 | $httpDate = gmdate("D, d M Y G:i:s T"); |
|---|
| | 171 | $stringToSign = "PUT\n\n\n$httpDate\nx-amz-acl:$acl\n/$bucket"; |
|---|
| | 172 | $signature = $this->getSignedString($stringToSign); |
|---|
| | 173 | |
|---|
| | 174 | $req = new HTTP_Request($this->serviceUrl . $bucket); |
|---|
| | 175 | $req->setMethod("PUT"); |
|---|
| | 176 | $req->addHeader("Date", $httpDate); |
|---|
| | 177 | $req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature); |
|---|
| | 178 | $req->addHeader("x-amz-acl", $acl); |
|---|
| | 179 | |
|---|
| | 180 | return $this->sendHttpRequest($req, 200); |
|---|
| | 181 | } |
|---|
| | 182 | |
|---|
| | 183 | /** |
|---|
| | 184 | * Delete Amazon S3 Bucket |
|---|
| | 185 | * |
|---|
| | 186 | * Method for emptying and deleting a bucket on Amazon S3 |
|---|
| | 187 | * |
|---|
| | 188 | * <code> |
|---|
| | 189 | * <?php |
|---|
| | 190 | * $sfAmazonS3 = new BasesfAmazonS3('accessKeyId', 'secretKey'); |
|---|
| | 191 | * $sfAmazonS3->deleteBucket('bucket_name'); |
|---|
| | 192 | * ?> |
|---|
| | 193 | * </code> |
|---|
| | 194 | * |
|---|
| | 195 | * @param string $bucket Name of the bucket you wish to delete |
|---|
| | 196 | * @return mixed $response Returns true if the call was successful, otherwise throws an exception |
|---|
| | 197 | */ |
|---|
| | 198 | public function deleteBucket($bucket) |
|---|
| | 199 | { |
|---|
| | 200 | $this->emptyBucket($bucket); |
|---|
| | 201 | $this->action = 'delete_bucket'; |
|---|
| | 202 | |
|---|
| | 203 | $httpDate = gmdate("D, d M Y G:i:s T"); |
|---|
| | 204 | $stringToSign = "DELETE\n\n\n$httpDate\n/$bucket"; |
|---|
| | 205 | $signature = $this->getSignedString($stringToSign); |
|---|
| | 206 | |
|---|
| | 207 | $req = new HTTP_Request($this->serviceUrl . $bucket); |
|---|
| | 208 | $req->setMethod("DELETE"); |
|---|
| | 209 | $req->addHeader("Date", $httpDate); |
|---|
| | 210 | $req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature); |
|---|
| | 211 | |
|---|
| | 212 | return $this->sendHttpRequest($req, 204); |
|---|
| | 213 | } |
|---|
| | 214 | |
|---|
| | 215 | /** |
|---|
| | 216 | * Empty Amazon S3 Bucket |
|---|
| | 217 | * |
|---|
| | 218 | * Method for emptying a bucket on Amazon S3. This method pulls all the objects in the bucket and calls the delete method on each bucket object |
|---|
| | 219 | * |
|---|
| | 220 | * <code> |
|---|
| | 221 | * <?php |
|---|
| | 222 | * $sfAmazonS3 = new BasesfAmazonS3('accessKeyId', 'secretKey'); |
|---|
| | 223 | * $sfAmazonS3->emptyBucket('bucket name'); |
|---|
| | 224 | * ?> |
|---|
| | 225 | * </code> |
|---|
| | 226 | * |
|---|
| | 227 | * @param string $bucket Name of the bucket you wish to empty |
|---|
| | 228 | * @return mixed $response Returns true if the call was successful, otherwise throws an exception |
|---|
| | 229 | */ |
|---|
| | 230 | public function emptyBucket($bucket) |
|---|
| | 231 | { |
|---|
| | 232 | $this->action = 'empty_bucket'; |
|---|
| | 233 | |
|---|
| | 234 | if ($this->listBucketKeys($bucket)) |
|---|
| | 235 | { |
|---|
| | 236 | $more = $this->parsedXml->IsTruncated; // determine of all keys in bucket are returned on this list call |
|---|
| | 237 | $keys = $this->parsedXml->Contents; |
|---|
| | 238 | |
|---|
| | 239 | // set up array for catching keys that aren't successfully deleted and set the initial count of these to 0 |
|---|
| | 240 | $not_deleted_keys = array(); |
|---|
| | 241 | $not_deleted_count = 0; |
|---|
| | 242 | |
|---|
| | 243 | foreach ($keys as $current) |
|---|
| | 244 | { |
|---|
| | 245 | //try up to 3 times to delete current key |
|---|
| | 246 | $tries=1; |
|---|
| | 247 | while (!$this->deleteBucketObject($bucket, $current->Key) && $tries <= 3) |
|---|
| 134 | | public static function getInstance() |
|---|
| 135 | | { |
|---|
| 136 | | static $sfAmazonS3; |
|---|
| | 274 | /** |
|---|
| | 275 | * List Amazon S3 Buckets |
|---|
| | 276 | * |
|---|
| | 277 | * Method for retrieving a list of all the buckets in your Amazon S3 account |
|---|
| | 278 | * |
|---|
| | 279 | * <code> |
|---|
| | 280 | * <?php |
|---|
| | 281 | * $sfAmazonS3 = new BasesfAmazonS3('accessKeyId', 'secretKey'); |
|---|
| | 282 | * $buckets = $sfAmazonS3->listBuckets(); |
|---|
| | 283 | * print_r($buckets); |
|---|
| | 284 | * ?> |
|---|
| | 285 | * </code> |
|---|
| | 286 | * |
|---|
| | 287 | * @return mixed $response Returns true if the call was successful, otherwise throws an exception |
|---|
| | 288 | */ |
|---|
| | 289 | public function listBuckets() |
|---|
| | 290 | { |
|---|
| | 291 | $this->action = 'list_buckets'; |
|---|
| | 292 | |
|---|
| | 293 | $httpDate = gmdate("D, d M Y G:i:s T"); |
|---|
| | 294 | $stringToSign="GET\n\n\n$httpDate\n/"; |
|---|
| | 295 | $signature = $this->getSignedString($stringToSign); |
|---|
| | 296 | |
|---|
| | 297 | $req = new HTTP_Request($this->serviceUrl); |
|---|
| | 298 | $req->addHeader("Date", $httpDate); |
|---|
| | 299 | $req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature); |
|---|
| | 300 | |
|---|
| | 301 | if ($this->sendHttpRequest($req, 200)) |
|---|
| | 302 | { |
|---|
| | 303 | return $this->getActionData(); |
|---|
| | 304 | } |
|---|
| | 305 | else |
|---|
| | 306 | { |
|---|
| | 307 | return false; |
|---|
| | 308 | } |
|---|
| | 309 | } |
|---|
| | 310 | |
|---|
| | 311 | /** |
|---|
| | 312 | * List Bucket Objects |
|---|
| | 313 | * |
|---|
| | 314 | * <code> |
|---|
| | 315 | * <?php |
|---|
| | 316 | * $sfAmazonS3 = new BasesfAmazonS3('accessKeyId', 'secretKey'); |
|---|
| | 317 | * $bucketObjects = $sfAmazonS3->listBucketObjects('bucket name'); |
|---|
| | 318 | * print_r($bucketObjects); |
|---|
| | 319 | * ?> |
|---|
| | 320 | * </code> |
|---|
| | 321 | * |
|---|
| | 322 | * @param mixed $bucket |
|---|
| | 323 | * @param mixed $marker |
|---|
| | 324 | * @param mixed $prefix |
|---|
| | 325 | * @param mixed $delimiter |
|---|
| | 326 | * @param int $maxKeys |
|---|
| | 327 | * @access public |
|---|
| | 328 | * @return void |
|---|
| | 329 | */ |
|---|
| | 330 | public function listBucketObjects($bucket, $marker = FALSE, $prefix = FALSE, $delimiter = FALSE, $maxKeys = 1000) |
|---|
| | 331 | { |
|---|
| | 332 | return $this->listBucketKeys($bucket, $marker, $prefix, $delimiter, $maxKeys); |
|---|
| | 333 | } |
|---|
| | 334 | |
|---|
| | 335 | /** |
|---|
| | 336 | * List Buckets Keys/Objects |
|---|
| | 337 | * |
|---|
| | 338 | * Method for retrieving all the objects in a specified bucket |
|---|
| | 339 | |
|---|
| | 340 | * <code> |
|---|
| | 341 | * <?php |
|---|
| | 342 | * $sfAmazonS3 = new BasesfAmazonS3('accessKeyId', 'secretKey'); |
|---|
| | 343 | * $bucketObjects = $sfAmazonS3->listBucketKeys('bucket name'); |
|---|
| | 344 | * print_r($bucketObjects); |
|---|
| | 345 | * ?> |
|---|
| | 346 | * </code> |
|---|
| | 347 | * |
|---|
| | 348 | * @param string $bucket Name of the bucket |
|---|
| | 349 | * @param bool $marker |
|---|
| | 350 | * @param bool $prefix |
|---|
| | 351 | * @param bool $delimiter |
|---|
| | 352 | * @param int $maxKeys |
|---|
| | 353 | * @return mixed $response Returns true if the call was successful, otherwise throws an exception |
|---|
| | 354 | */ |
|---|
| | 355 | public function listBucketKeys($bucket, $marker = FALSE, $prefix = FALSE, $delimiter = FALSE, $maxKeys = 1000) |
|---|
| | 356 | { |
|---|
| | 357 | $this->action = 'list_bucket_keys'; |
|---|
| | 358 | |
|---|
| | 359 | $httpDate = gmdate("D, d M Y G:i:s T"); |
|---|
| | 360 | $stringToSign = "GET\n\n\n$httpDate\n/$bucket"; |
|---|
| | 361 | $signature = $this->getSignedString($stringToSign); |
|---|
| | 362 | |
|---|
| | 363 | $req = new HTTP_Request($this->serviceUrl.$bucket."?max-keys={$maxKeys}&marker={$marker}&prefix={$prefix}&delimiter={$delimiter}"); |
|---|
| | 364 | $req->addHeader("Date", $httpDate); |
|---|
| | 365 | $req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature); |
|---|
| | 366 | |
|---|
| | 367 | if ($this->sendHttpRequest($req, 200)) |
|---|
| | 368 | { |
|---|
| | 369 | return $this->getActionData(); |
|---|
| | 370 | } |
|---|
| | 371 | else |
|---|
| | 372 | { |
|---|
| | 373 | return false; |
|---|
| | 374 | } |
|---|
| | 375 | } |
|---|
| | 376 | |
|---|
| | 377 | /** |
|---|
| | 378 | * Get Amazon S3 Bucket Acl(permissions) |
|---|
| | 379 | * |
|---|
| | 380 | * Method for retrieving the current ACL for a specified bucket |
|---|
| | 381 | * |
|---|
| | 382 | * <code> |
|---|
| | 383 | * <?php |
|---|
| | 384 | * $sfAmazonS3 = new BasesfAmazonS3('accessKeyId', 'secretKey'); |
|---|
| | 385 | * echo $sfAmazonS3->getBucketAcl('bucket name'); // public-read, public-read-write, private, authenticated-users-read |
|---|
| | 386 | * ?> |
|---|
| | 387 | * </code> |
|---|
| | 388 | * |
|---|
| | 389 | * @param string $bucket Name of the bucket |
|---|
| | 390 | * @return mixed $response Returns true if the call was successful, otherwise throws an exception |
|---|
| | 391 | */ |
|---|
| | 392 | public function getBucketAcl($bucket) |
|---|
| | 393 | { |
|---|
| | 394 | $this->action = 'get_bucket_acl'; |
|---|
| | 395 | |
|---|
| | 396 | $httpDate = gmdate("D, d M Y G:i:s T"); |
|---|
| | 397 | $stringToSign = "GET\n\n\n$httpDate\n/$bucket/?acl"; |
|---|
| | 398 | $signature = $this->getSignedString($stringToSign); |
|---|
| | 399 | |
|---|
| | 400 | $req = new HTTP_Request($this->serviceUrl.$bucket.'/?acl'); |
|---|
| | 401 | $req->setMethod("GET"); |
|---|
| | 402 | $req->addHeader("Date", $httpDate); |
|---|
| | 403 | $req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature); |
|---|
| | 404 | |
|---|
| | 405 | if ($this->sendHttpRequest($req, 200)) |
|---|
| | 406 | { |
|---|
| | 407 | return $this->getActionData(); |
|---|
| | 408 | } |
|---|
| | 409 | else |
|---|
| | 410 | { |
|---|
| | 411 | return false; |
|---|
| | 412 | } |
|---|
| | 413 | } |
|---|
| | 414 | |
|---|
| | 415 | /** |
|---|
| | 416 | * Set Amazon S3 Bucket Acl(permissions) |
|---|
| | 417 | * |
|---|
| | 418 | * Method for setting the current Acl fpr a speficied bucket |
|---|
| | 419 | * |
|---|
| | 420 | * <code> |
|---|
| | 421 | * <?php |
|---|
| | 422 | * $sfAmazonS3 = new BasesfAmazonS3('accessKeyId', 'secretKey'); |
|---|
| | 423 | * $sfAmazonS3->setBucketAcl('bucket name', 'public-read'); // public-read, public-read-write, private, authenticated-users-read |
|---|
| | 424 | * ?> |
|---|
| | 425 | * </code> |
|---|
| | 426 | * @return mixed $response Returns true if the call was successful, otherwise throws an exception |
|---|
| | 427 | */ |
|---|
| | 428 | public function setBucketAcl($bucket, $acl) |
|---|
| | 429 | { |
|---|
| | 430 | $this->action = 'set_bucket_acl'; |
|---|
| | 431 | |
|---|
| | 432 | $httpDate = gmdate("D, d M Y G:i:s T"); |
|---|
| | 433 | $stringToSign = "PUT\n\n\n{$httpDate}\nx-amz-acl:$acl\n/$bucket/?acl"; |
|---|
| | 434 | $signature = $this->getSignedString($stringToSign); |
|---|
| | 435 | |
|---|
| | 436 | $req = new HTTP_Request($this->serviceUrl.$bucket.'/?acl'); |
|---|
| | 437 | $req->setMethod("PUT"); |
|---|
| | 438 | $req->addHeader("Date", $httpDate); |
|---|
| | 439 | $req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature); |
|---|
| | 440 | $req->addHeader("x-amz-acl", $acl); |
|---|
| | 441 | |
|---|
| | 442 | return $this->sendHttpRequest($req, 200); |
|---|
| | 443 | } |
|---|
| | 444 | |
|---|
| | 445 | /** |
|---|
| | 446 | * Put Object(file, binary data, etc.) in Amazon S3 Bucket |
|---|
| | 447 | * |
|---|
| | 448 | * Method for uploading a file to a bucket on Amazon S3 |
|---|
| | 449 | * |
|---|
| | 450 | * <code> |
|---|
| | 451 | * <?php |
|---|
| | 452 | * $sfAmazonS3 = new BasesfAmazonS3('accessKeyId', 'secretKey'); |
|---|
| | 453 | * echo $sfAmazonS3->putObjectInBucket('bucket name', 'file_key', '/path/to/file', 'content/type', 'private'); // public-read, public-read-write, private, authenticated-users-read |
|---|
| | 454 | * ?> |
|---|
| | 455 | * </code> |
|---|
| | 456 | * |
|---|
| | 457 | * @param string $bucket Name of the bucket to put the object in |
|---|
| | 458 | * @param string $key Key to identify the object |
|---|
| | 459 | * @param string $fileName Name of the file to be displayed |
|---|
| | 460 | * @param string $filePath Absolute path to the file that is to be uploaded to bucket |
|---|
| | 461 | * @param string $contentType The MIME type for the file |
|---|
| | 462 | * @param string $acl The ACL(permissions) for the file |
|---|
| | 463 | * @param array $metaDataArray Array of meta data information for the object |
|---|
| | 464 | * @param bool $md5 True/False for whether or not to MD5 the contents of the file |
|---|
| | 465 | * @return mixed $response Returns true if the call was successful, otherwise throws an exception |
|---|
| | 466 | */ |
|---|
| | 467 | public function putObjectInBucket($bucket, $key, $fileName, $filePath, $contentType, $acl = 'private', $metaDataArray = array(), $md5 = false) |
|---|
| | 468 | { |
|---|
| | 469 | $metaDataArray = !is_array($metaDataArray) ? array():$metaDataArray; |
|---|
| | 470 | $this->action = 'put_object_in_bucket'; |
|---|
| | 471 | |
|---|
| | 472 | sort($metaDataArray); |
|---|
| | 473 | $resource = $bucket."/".urlencode($key); |
|---|
| | 474 | |
|---|
| | 475 | $req = new HTTP_Request($this->serviceUrl.$resource); |
|---|
| | 476 | $body = file_get_contents($filePath); |
|---|
| | 477 | $req->setBody($body); |
|---|
| | 478 | $req->setMethod("PUT"); |
|---|
| | 479 | $httpDate = gmdate("D, d M Y G:i:s T"); |
|---|
| | 480 | $req->addHeader("Date", $httpDate); |
|---|
| | 481 | $req->addHeader("Content-Type", $contentType); |
|---|
| | 482 | $req->addHeader("Content-Length", strlen($body)); |
|---|
| | 483 | $req->addHeader("x-amz-acl", $acl); |
|---|
| | 484 | |
|---|
| | 485 | if ($md5) |
|---|
| | 486 | { |
|---|
| | 487 | $MD5 = $this->hex2b64(md5_file($filePath)); |
|---|
| | 488 | $req->addHeader("Content-MD5", $MD5); |
|---|
| | 489 | } |
|---|
| | 490 | else |
|---|
| | 491 | { |
|---|
| | 492 | $MD5 = ''; |
|---|
| | 493 | } |
|---|
| | 494 | |
|---|
| | 495 | $stringToSign = "PUT\n$MD5\n$contentType\n$httpDate\nx-amz-acl:$acl\n"; |
|---|
| | 496 | |
|---|
| | 497 | /* |
|---|
| | 498 | foreach($metaDataArray as $current) |
|---|
| | 499 | { |
|---|
| | 500 | if( $current != "" ) |
|---|
| | 501 | { |
|---|
| | 502 | $header = trim(substr($current, 0, strpos($current,':'))); |
|---|
| | 503 | $meta = trim(substr($current, strpos($current, ':') +1, strlen($current))); |
|---|
| | 504 | |
|---|
| | 505 | $req->addHeader("x-amz-meta-$header", $meta); |
|---|
| | 506 | |
|---|
| | 507 | $stringToSign .= "x-amz-meta-$header:$meta\n"; |
|---|
| | 508 | } |
|---|
| | 509 | } |
|---|
| | 510 | */ |
|---|
| | 511 | |
|---|
| | 512 | $stringToSign .= "/$resource"; |
|---|
| | 513 | $signature = $this->getSignedString($stringToSign); |
|---|
| | 514 | $req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature); |
|---|
| | 515 | $req->addHeader("Content-Disposition", "filename=\"".$fileName."\""); |
|---|
| | 516 | |
|---|
| | 517 | return $this->sendHttpRequest($req, 200); |
|---|
| | 518 | } |
|---|
| | 519 | |
|---|
| | 520 | /** |
|---|
| | 521 | * Stream Object to Amazon S3 Bucket |
|---|
| | 522 | * |
|---|
| | 523 | * This is not much different than the putObjectInBucket(), instead of using an http request to put the file, it uses curl. |
|---|
| | 524 | * |
|---|
| | 525 | * @param string $bucket Name of the bucket to put the object in |
|---|
| | 526 | * @param string $key Name/key of the object |
|---|
| | 527 | * @param string $filePath Absolute path to the file that is to be uploaded to bucket |
|---|
| | 528 | * @param string $contentType The MIME type for the file |
|---|
| | 529 | * @param string $acl The ACL(permissions) for the file |
|---|
| | 530 | * @param array $metaDataArray Array of meta data information for the object |
|---|
| | 531 | * @return mixed $response Returns true if the call was successful, otherwise throws an exception |
|---|
| | 532 | */ |
|---|
| | 533 | public function streamObjectToBucket($bucket, $key, $filePath, $contentType, $acl = 'public', $metaDataArray = array()) |
|---|
| | 534 | { |
|---|
| | 535 | $metaDataArray = !is_array($metaDataArray) ? array():$metaDataArray; |
|---|
| | 536 | $this->action = 'stream_object_to_bucket'; |
|---|
| | 537 | |
|---|
| | 538 | $stream_inst = new sfAmazonS3ObjectStream(); |
|---|
| | 539 | $stream_inst->data = fopen($filePath, "r"); |
|---|
| | 540 | $streamFunction = array($stream_inst, "doStream"); |
|---|
| | 541 | |
|---|
| | 542 | sort($metaDataArray); |
|---|
| | 543 | $resource = "$bucket/$key"; |
|---|
| | 544 | $resource = urlencode($resource); |
|---|
| | 545 | $httpDate = gmdate("D, d M Y G:i:s T"); |
|---|
| | 546 | |
|---|
| | 547 | $curl_inst = curl_init(); |
|---|
| | 548 | |
|---|
| | 549 | curl_setopt ($curl_inst, CURLOPT_CONNECTTIMEOUT, 30); |
|---|
| | 550 | curl_setopt ($curl_inst, CURLOPT_LOW_SPEED_LIMIT, 1); |
|---|
| | 551 | curl_setopt ($curl_inst, CURLOPT_LOW_SPEED_TIME, 180); |
|---|
| | 552 | curl_setopt ($curl_inst, CURLOPT_NOSIGNAL, 1); |
|---|
| | 553 | curl_setopt ($curl_inst, CURLOPT_READFUNCTION, $streamFunction); |
|---|
| | 554 | curl_setopt ($curl_inst, CURLOPT_URL, $this->serviceUrl . $resource); |
|---|
| | 555 | curl_setopt ($curl_inst, CURLOPT_UPLOAD, true); |
|---|
| | 556 | curl_setopt ($curl_inst, CURLINFO_CONTENT_LENGTH_UPLOAD, $contentLength); |
|---|
| | 557 | |
|---|
| | 558 | $header[] = "Date: $httpDate"; |
|---|
| | 559 | $header[] = "Content-Type: $contentType"; |
|---|
| | 560 | $header[] = "Content-Length: $contentLength"; |
|---|
| | 561 | $header[] = "Expect: "; |
|---|
| | 562 | $header[] = "Transfer-Encoding: "; |
|---|
| | 563 | $header[] = "x-amz-acl: $acl"; |
|---|
| | 564 | |
|---|
| | 565 | $stringToSign="PUT\n$MD5\n$contentType\n$httpDate\nx-amz-acl:$acl\n"; |
|---|
| | 566 | |
|---|
| | 567 | foreach ($metaDataArray as $current) |
|---|
| | 568 | { |
|---|
| | 569 | if ($current != "") |
|---|
| | 570 | { |
|---|
| | 571 | $stringToSign .= "x-amz-meta-$current\n"; |
|---|
| | 572 | $header = substr($current,0,strpos($current,':')); |
|---|
| | 573 | $meta = substr($current,strpos($current,':')+1,strlen($current)); |
|---|
| | 574 | $header[] = "x-amz-meta-$header: $meta"; |
|---|
| | 575 | } |
|---|
| | 576 | } |
|---|
| | 577 | |
|---|
| | 578 | $stringToSign .= "/$resource"; |
|---|
| | 579 | $signature = $this->getSignedString($stringToSign); |
|---|
| | 580 | $header[] = "Authorization: AWS $this->accessKeyId:$signature"; |
|---|
| | 581 | |
|---|
| | 582 | curl_setopt($curl_inst, CURLOPT_HTTPHEADER, $header); |
|---|
| | 583 | curl_setopt($curl_inst, CURLOPT_RETURNTRANSFER, 1); |
|---|
| | 584 | |
|---|
| | 585 | $result = curl_exec ($curl_inst); |
|---|
| | 586 | |
|---|
| | 587 | $this->responseString = $result; |
|---|
| | 588 | $this->responseCode = curl_getinfo($curl_inst, CURLINFO_HTTP_CODE); |
|---|
| | 589 | fclose($stream_inst->data); |
|---|
| | 590 | curl_close($curl_inst); |
|---|
| | 591 | |
|---|
| | 592 | return $this->validateResponseCode(200); |
|---|
| | 593 | } |
|---|
| 138 | | if( !isset($sfAmazonS3) ) |
|---|
| | 595 | /** |
|---|
| | 596 | * Delete Bucket Object |
|---|
| | 597 | * |
|---|
| | 598 | * <code> |
|---|
| | 599 | * <?php |
|---|
| | 600 | * $sfAmazonS3 = new BasesfAmazonS3('accessKeyId', 'secretKey'); |
|---|
| | 601 | * $sfAmazonS3->deleteBucketObject('bucket', 'key'); |
|---|
| | 602 | * ?> |
|---|
| | 603 | * </code> |
|---|
| | 604 | * |
|---|
| | 605 | * Method to delete a object/file from an Amazon S3 bucket |
|---|
| | 606 | * |
|---|
| | 607 | * @param string $bucket Name of the bucket |
|---|
| | 608 | * @param string $key Name/key of the object |
|---|
| | 609 | * @return mixed $response Returns true if the call was successful, otherwise throws an exception |
|---|
| | 610 | */ |
|---|
| | 611 | public function deleteBucketObject($bucket, $key) |
|---|
| | 612 | { |
|---|
| | 613 | $this->action = 'delete_bucket_object'; |
|---|
| | 614 | |
|---|
| | 615 | $httpDate = gmdate("D, d M Y G:i:s T"); |
|---|
| | 616 | $resource = $bucket."/".urlencode($key); |
|---|
| | 617 | $stringToSign = "DELETE\n\n\n$httpDate\n/$resource"; |
|---|
| | 618 | $signature = $this->getSignedString($stringToSign); |
|---|
| | 619 | |
|---|
| | 620 | $req = new HTTP_Request($this->serviceUrl.$resource); |
|---|
| | 621 | $req->setMethod("DELETE"); |
|---|
| | 622 | $req->addHeader("Date", $httpDate); |
|---|
| | 623 | $req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature); |
|---|
| | 624 | |
|---|
| | 625 | return $this->sendHttpRequest($req, 204); |
|---|
| | 626 | } |
|---|
| | 627 | |
|---|
| | 628 | /* |
|---|
| | 629 | * Get Object Acl(permissions) |
|---|
| | 630 | * |
|---|
| | 631 | * Method for retrieving the current object acl |
|---|
| | 632 | * |
|---|
| | 633 | * <code> |
|---|
| | 634 | * <?php |
|---|
| | 635 | * $sfAmazonS3 = new BasesfAmazonS3('accessKeyId', 'secretKey'); |
|---|
| | 636 | * echo $sfAmazonS3->getBucketObjectAcl('bucket', 'key'); |
|---|
| | 637 | * ?> |
|---|
| | 638 | * </code> |
|---|
| | 639 | * |
|---|
| | 640 | * @param string $bucket Name of the bucket |
|---|
| | 641 | * @param string @key Name/key of the object |
|---|
| | 642 | */ |
|---|
| | 643 | public function getBucketObjectAcl($bucket, $key) |
|---|
| | 644 | { |
|---|
| | 645 | $this->action = 'get_bucket_object_acl'; |
|---|
| | 646 | |
|---|
| | 647 | $httpDate = gmdate("D, d M Y G:i:s T"); |
|---|
| | 648 | $resource = $bucket."/".urlencode($key); |
|---|
| | 649 | $stringToSign = "GET\n\n\n$httpDate\n/$resource?acl"; |
|---|
| | 650 | $signature = $this->getSignedString($stringToSign); |
|---|
| | 651 | |
|---|
| | 652 | $req = new HTTP_Request($this->serviceUrl.$resource.'?acl'); |
|---|
| | 653 | $req->setMethod("GET"); |
|---|
| | 654 | $req->addHeader("Date", $httpDate); |
|---|
| | 655 | $req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature); |
|---|
| | 656 | |
|---|
| | 657 | if ($this->sendHttpRequest($req, 200)) |
|---|
| | 658 | { |
|---|
| | 659 | return $this->getActionData(); |
|---|
| | 660 | } |
|---|
| | 661 | else |
|---|
| | 662 | { |
|---|
| | 663 | return false; |
|---|
| | 664 | } |
|---|
| | 665 | } |
|---|
| | 666 | |
|---|
| | 667 | /** |
|---|
| | 668 | * Set Bucket Object Acl |
|---|
| | 669 | * |
|---|
| | 670 | * Method for setting the Acl(permissions) for a bucket object |
|---|
| | 671 | * |
|---|
| | 672 | * <code> |
|---|
| | 673 | * <?php |
|---|
| | 674 | * $sfAmazonS3 = new BasesfAmazonS3('accessKeyId', 'secretKey'); |
|---|
| | 675 | * $sfAmazonS3->setBucketObjectAcl('bucket', 'key', 'private'); |
|---|
| | 676 | * ?> |
|---|
| | 677 | * </code> |
|---|
| | 678 | * |
|---|
| | 679 | * @param string $bucket Name of the bucket |
|---|
| | 680 | * @param string $key Name/key of the object |
|---|
| | 681 | * @param string $acl String for the object acl(private, public-read-write, public-read, authenticated-users-read) |
|---|
| | 682 | * @return mixed $response Returns true if the call was successful, otherwise throws an exception |
|---|
| | 683 | */ |
|---|
| | 684 | public function setBucketObjectAcl($bucket, $key, $acl) |
|---|
| | 685 | { |
|---|
| | 686 | $this->action = 'set_bucket_object_acl'; |
|---|
| | 687 | |
|---|
| | 688 | $httpDate = gmdate("D, d M Y G:i:s T"); |
|---|
| | 689 | $resource = $bucket."/".urlencode($key); |
|---|
| | 690 | $stringToSign = "PUT\n\n\n$httpDate\nx-amz-acl:$acl\n/$resource?acl"; |
|---|
| | 691 | $signature = $this->getSignedString($stringToSign); |
|---|
| | 692 | |
|---|
| | 693 | $req = new HTTP_Request($this->serviceUrl.$resource.'?acl'); |
|---|
| | 694 | $req->setMethod("PUT"); |
|---|
| | 695 | $req->addHeader("Date", $httpDate); |
|---|
| | 696 | $req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature); |
|---|
| | 697 | $req->addHeader("x-amz-acl", $acl); |
|---|
| | 698 | |
|---|
| | 699 | return $this->sendHttpRequest($req, 200); |
|---|
| | 700 | } |
|---|
| | 701 | |
|---|
| | 702 | /** |
|---|
| | 703 | * Get Bucket Object Meta Data |
|---|
| | 704 | * |
|---|
| | 705 | * Method for retrieving all the meta data information for a object file |
|---|
| | 706 | * |
|---|
| | 707 | * <code> |
|---|
| | 708 | * <?php |
|---|
| | 709 | * $sfAmazonS3 = new BasesfAmazonS3('accessKeyId', 'secretKey'); |
|---|
| | 710 | * $metaData = $sfAmazonS3->getBucketObjectMetaData('bucket', 'key'); |
|---|
| | 711 | * print_r($metaData); |
|---|
| | 712 | * ?> |
|---|
| | 713 | * </code> |
|---|
| | 714 | * |
|---|
| | 715 | * @param string $bucket Name of the bucket |
|---|
| | 716 | * @param string $key Name/key of the object |
|---|
| | 717 | * @return mixed $response Returns true if the call was successful, otherwise throws an exception |
|---|
| | 718 | */ |
|---|
| | 719 | public function getBucketObjectMetaData($bucket, $key) |
|---|
| | 720 | { |
|---|
| | 721 | $this->action = 'get_bucket_object_meta_data'; |
|---|
| | 722 | |
|---|
| | 723 | $httpDate = gmdate("D, d M Y G:i:s T"); |
|---|
| | 724 | $resource = $bucket."/".urlencode($key); |
|---|
| | 725 | $stringToSign = "HEAD\n\n\n$httpDate\n/$resource"; |
|---|
| | 726 | $signature = $this->getSignedString($stringToSign); |
|---|
| | 727 | |
|---|
| | 728 | $req = new HTTP_Request($this->serviceUrl.$resource); |
|---|
| | 729 | $req->setMethod("HEAD"); |
|---|
| | 730 | $req->addHeader("Date", $httpDate); |
|---|
| | 731 | $req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature); |
|---|
| | 732 | |
|---|
| | 733 | if ($this->sendHttpRequest($req, 200)) |
|---|
| | 734 | { |
|---|
| | 735 | return $this->getActionData(); |
|---|
| | 736 | } |
|---|
| | 737 | else |
|---|
| | 738 | { |
|---|
| | 739 | return false; |
|---|
| | 740 | } |
|---|
| | 741 | } |
|---|
| | 742 | |
|---|
| | 743 | /** |
|---|
| | 744 | * Get Bucket Object As String |
|---|
| | 745 | * |
|---|
| | 746 | * Method for retrieving the contents of a object as a string |
|---|
| | 747 | * |
|---|
| | 748 | * <code> |
|---|
| | 749 | * <?php |
|---|
| | 750 | * $sfAmazonS3 = new BasesfAmazonS3('accessKeyId', 'secretKey'); |
|---|
| | 751 | * header('Content-type: image/jpeg'); |
|---|
| | 752 | * echo $sfAmazonS3->getBucketObjectAsString('bucket', 'key'); |
|---|
| | 753 | * ?> |
|---|
| | 754 | * </code> |
|---|
| | 755 | * |
|---|
| | 756 | * @param string $bucket Name of the bucket |
|---|
| | 757 | * @param string $key Name/key of the object |
|---|
| | 758 | * @return mixed $response Returns true if the call was successful, otherwise throws an exception |
|---|
| | 759 | */ |
|---|
| | 760 | public function getBucketObjectAsString($bucket, $key) |
|---|
| | 761 | { |
|---|
| | 762 | $this->action = 'get_bucket_object_as_string'; |
|---|
| | 763 | |
|---|
| | 764 | $httpDate = gmdate("D, d M Y G:i:s T"); |
|---|
| | 765 | $resource = $bucket."/".urlencode($key); |
|---|
| | 766 | $stringToSign = "GET\n\n\n{$httpDate}\n/$resource"; |
|---|
| | 767 | $signature = $this->getSignedString($stringToSign); |
|---|
| | 768 | |
|---|
| | 769 | $req = new HTTP_Request($this->serviceUrl.$resource); |
|---|
| | 770 | $req->setMethod("GET"); |
|---|
| | 771 | $req->addHeader("Date", $httpDate); |
|---|
| | 772 | $req->addHeader("Authorization", "AWS ".$this->accessKeyId.":".$signature); |
|---|
| | 773 | |
|---|
| | 774 | if ($this->sendHttpRequest($req, 200)) |
|---|
| | 775 | { |
|---|
| | 776 | return $this->getActionData(); |
|---|
| | 777 | } |
|---|
| | 778 | else |
|---|
| | 779 | { |
|---|
| | 780 | return false; |
|---|
| | 781 | } |
|---|
| | 782 | } |
|---|
| | 783 | |
|---|
| | 784 | /** |
|---|
| | 785 | * Get Bucket Object Query Url |
|---|
| | 786 | * |
|---|
| | 787 | * Method for getting the url to a specified object |
|---|
| | 788 | * |
|---|
| | 789 | * <code> |
|---|
| | 790 | * <?php |
|---|
| | 791 | * $sfAmazonS3 = new BasesfAmazonS3('accessKeyId', 'secretKey'); |
|---|
| | 792 | * echo $sfAmazonS3->getBucketObjectQueryUrl('bucket', 'file key', 3600); // link lives for 1 hour |
|---|
| | 793 | * ?> |
|---|
| | 794 | * </code> |
|---|
| | 795 | * |
|---|
| | 796 | * @param string $bucket Name of bucket |
|---|
| | 797 | * @param string $key Name/key of object |
|---|
| | 798 | * @param int $expires Unix timestamp for when the link will expire |
|---|
| | 799 | * @return string $queryString |
|---|
| | 800 | */ |
|---|
| | 801 | public function getBucketObjectQueryUrl($bucket, $key, $expires) |
|---|
| | 802 | { |
|---|
| | 803 | $expires = time() + $expires; |
|---|
| | 804 | $resource = $bucket."/".urlencode($key); |
|---|
| | 805 | $stringToSign = "GET\n\n\n$expires\n/$resource"; |
|---|
| | 806 | $signature = urlencode($this->getSignedString($stringToSign)); |
|---|
| | 807 | $queryString = $this->serviceUrl."$resource?AWSAccessKeyId=$this->accessKeyId&Expires=$expires&Signature=$signature"; |
|---|
| | 808 | |
|---|
| | 809 | return $queryString; |
|---|
| | 810 | } |
|---|
| | 811 | |
|---|
| | 812 | /** |
|---|
| | 813 | * Get Bucket Object Query Url Link |
|---|
| | 814 | * |
|---|
| | 815 | * Method for getting an html link for a bucket object |
|---|
| | 816 | * |
|---|
| | 817 | * @param string $bucket Name of bucket |
|---|
| | 818 | * @param string $key Name/key of object |
|---|
| | 819 | * @param int $expires Unix timestamp for when the link will expire |
|---|
| | 820 | * @return string $link |
|---|
| | 821 | */ |
|---|
| | 822 | public function getBucketObjectQueryLink($bucket, $key, $expires) |
|---|
| | 823 | { |
|---|
| | 824 | $link = '<a href="'.$this->getBucketObjectQueryUrl($bucket, $key, $expires).'">'.$bucket.'/'.$key.'</a>'; |
|---|
| | 825 | |
|---|
| | 826 | return $link; |
|---|
| | 827 | } |
|---|
| | 828 | |
|---|
| | 829 | /** |
|---|
| | 830 | * Get Action Data |
|---|
| | 831 | * |
|---|
| | 832 | * Method for getting the processed data returned from the GET actions above |
|---|
| | 833 | * |
|---|
| | 834 | * @return mixed $result |
|---|
| | 835 | */ |
|---|
| | 836 | protected function getActionData() |
|---|
| | 837 | { |
|---|
| | 838 | switch ($this->action) |
|---|
| | 839 | { |
|---|
| | 840 | case 'list_buckets': |
|---|
| | 841 | return $this->getBucketsArray(); |
|---|
| | 842 | break; |
|---|
| | 843 | |
|---|
| | 844 | case 'list_bucket_keys': |
|---|
| | 845 | return $this->getBucketKeysArray(); |
|---|
| | 846 | break; |
|---|
| | 847 | |
|---|
| | 848 | case 'get_bucket_object_as_string': |
|---|
| | 849 | return $this->responseString; |
|---|
| | 850 | break; |
|---|
| | 851 | |
|---|
| | 852 | case 'get_bucket_acl': |
|---|
| | 853 | return $this->getBucketACLString(); |
|---|
| | 854 | break; |
|---|
| | 855 | |
|---|
| | 856 | case 'get_bucket_object_acl': |
|---|
| | 857 | return $this->getBucketObjectACLString(); |
|---|
| | 858 | break; |
|---|
| | 859 | |
|---|
| | 860 | case 'get_bucket_object_meta_data': |
|---|
| | 861 | return $this->headers; |
|---|
| | 862 | break; |
|---|
| | 863 | } |
|---|
| | 864 | } |
|---|
| | 865 | |
|---|
| | 866 | /** |
|---|
| | 867 | * Send Http Request |
|---|
| | 868 | * |
|---|
| | 869 | * Standard function for sending the http request for an action. Sets some class vars after sending the request and validates that the request was successful |
|---|
| | 870 | * |
|---|
| | 871 | * @param object $httpRequest |
|---|
| | 872 | * @param integer $successCode Code required for the http request to be successful |
|---|
| | 873 | * @return mixed $response Returns true if the call was successful, otherwise throws an exception |
|---|
| | 874 | */ |
|---|
| | 875 | protected function sendHttpRequest($httpRequest, $successCode) |
|---|
| | 876 | { |
|---|
| | 877 | $httpRequest->sendRequest(); |
|---|
| | 878 | |
|---|
| | 879 | $this->responseCode = $httpRequest->getResponseCode(); |
|---|
| | 880 | $this->responseString = $httpRequest->getResponseBody(); |
|---|
| | 881 | $this->headers = $httpRequest->getResponseHeader(); |
|---|
| | 882 | $this->parsedXml = simplexml_load_string($this->responseString); |
|---|
| | 883 | |
|---|
| | 884 | return $this->validateResponseCode($successCode); |
|---|
| | 885 | } |
|---|
| | 886 | |
|---|
| | 887 | /** |
|---|
| | 888 | * Validate Response Code |
|---|
| | 889 | * |
|---|
| | 890 | * Method for validating a http request was successful |
|---|
| | 891 | * |
|---|
| | 892 | * @param int @successCode |
|---|
| | 893 | * @return mixed $response Returns true if the call was successful, otherwise throws an exception |
|---|
| | 894 | */ |
|---|
| | 895 | protected function validateResponseCode($successCode) |
|---|
| | 896 | { |
|---|
| | 897 | if ($this->responseCode == $successCode) |
|---|
| | 898 | { |
|---|
| | 899 | return true; |
|---|
| | 900 | } |
|---|
| | 901 | else |
|---|
| | 902 | { |
|---|
| | 903 | throw new Exception($this->responseCode.' - '.$this->responseString); |
|---|
| | 904 | } |
|---|
| | 905 | } |
|---|
| | 906 | |
|---|
| | 907 | /** |
|---|
| | 908 | * Hex to Base 64 Converter |
|---|
| | 909 | * |
|---|
| | 910 | * @param string $string String to convert and return |
|---|
| | 911 | * @return string $b64raw |
|---|
| | 912 | */ |
|---|
| | 913 | protected function hex2b64($string) |
|---|
| | 914 | { |
|---|
| | 915 | $raw = ''; |
|---|
| | 916 | |
|---|
| | 917 | for ($i=0; $i < strlen($string); $i+=2) |
|---|
| | 918 | { |
|---|
| | 919 | $raw .= chr(hexdec(substr($string, $i, 2))); |
|---|
| | 920 | } |
|---|
| | 921 | |
|---|
| | 922 | $b64raw = base64_encode($raw); |
|---|
| | 923 | |
|---|
| | 924 | return $b64raw; |
|---|
| | 925 | } |
|---|
| | 926 | |
|---|
| | 927 | /** |
|---|
| | 928 | * Get Signed String |
|---|
| | 929 | * |
|---|
| | 930 | * Method for signing a unique string used in each action http request |
|---|
| | 931 | * |
|---|
| | 932 | * @param string $string |
|---|
| | 933 | * @return string $signedString |
|---|
| | 934 | */ |
|---|
| | 935 | protected function getSignedString($string) |
|---|
| | 936 | { |
|---|
| | 937 | $hasher = new Crypt_HMAC($this->secretKey, "sha1"); |
|---|
| | 938 | $signedString = $this->hex2b64($hasher->hash($string)); |
|---|
| | 939 | return $signedString; |
|---|
| | 940 | } |
|---|
| | 941 | |
|---|
| | 942 | /** |
|---|
| | 943 | * Get Buckets Array |
|---|
| | 944 | * |
|---|
| | 945 | * Processes the parsed xml in to a formatted array of buckets |
|---|
| | 946 | * |
|---|
| | 947 | * @return array $buckets |
|---|
| | 948 | */ |
|---|
| | 949 | protected function getBucketsArray() |
|---|
| | 950 | { |
|---|
| | 951 | $buckets = array(); |
|---|
| | 952 | |
|---|
| | 953 | foreach ($this->parsedXml->Buckets->Bucket AS $key => $bucket) |
|---|
| | 954 | { |
|---|
| | 955 | $buckets[] = array('Name' => (string)$bucket->Name, |
|---|
| | 956 | 'CreationDate' => (string)$bucket->CreationDate); |
|---|
| | 957 | } |
|---|
| | 958 | |
|---|
| | 959 | return $buckets; |
|---|
| | 960 | } |
|---|
| | 961 | |
|---|
| | 962 | /** |
|---|
| | 963 | * Get Bucket Keys Array |
|---|
| | 964 | * |
|---|
| | 965 | * Processes the parsed xml in to a formatted array of bucket keys/objects |
|---|
| | 966 | * |
|---|
| | 967 | * @return array $bucketKeys |
|---|
| | 968 | */ |
|---|
| | 969 | protected function getBucketKeysArray() |
|---|
| | 970 | { |
|---|
| | 971 | $bucketKeys = array(); |
|---|
| | 972 | |
|---|
| | 973 | foreach ($this->parsedXml->Contents AS $key) |
|---|
| | 974 | { |
|---|
| | 975 | $bucketKeys[] = array('Key' => (string)$key->Key, |
|---|
| | 976 | 'LastModified' => (string)$key->LastModified, |
|---|
| | 977 | 'ETag' => (string)$key->Etag, |
|---|
| | 978 | 'Size' => (string)$key->Size, |
|---|
| | 979 | 'Owner' => array('ID' => (string)$key->Owner->ID, 'DisplayName' => (string)$key->Owner->DisplayName), |
|---|
| | 980 | 'StorageClass' => (string)$key->StorageClass); |
|---|
| | 981 | } |
|---|
| | 982 | |
|---|
| | 983 | return $bucketKeys; |
|---|
| | 984 | } |
|---|
| | 985 | |
|---|
| | 986 | /** |
|---|
| | 987 | * Get Bucket ACL String |
|---|
| | 988 | * |
|---|
| | 989 | * Since no method existed to simple return the string ACL that was passed when setting, I had to hack up this logic to determine what it is based on the xml |
|---|
| | 990 | * |
|---|
| | 991 | * @return string |
|---|
| | 992 | */ |
|---|
| | 993 | protected function getBucketACLString() |
|---|
| | 994 | { |
|---|
| | 995 | $string = ''; |
|---|
| | 996 | |
|---|
| | 997 | foreach ($this->parsedXml->AccessControlList->Grant AS $perm) |
|---|
| | 998 | { |
|---|
| | 999 | if ($perm->Grantee->URI) |
|---|