PageRenderTime 70ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/codes-php/phpjakarta/WindowsAzure/Blob/BlobRestProxy.php

http://bukuphpjs.codeplex.com
PHP | 2337 lines | 1619 code | 215 blank | 503 comment | 41 complexity | 2aad45435059f20702f817393653a00f MD5 | raw file
Possible License(s): Apache-2.0, MIT, LGPL-2.1
  1. <?php
  2. /**
  3. * LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. *
  14. * PHP version 5
  15. *
  16. * @category Microsoft
  17. * @package WindowsAzure\Blob
  18. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  19. * @copyright 2012 Microsoft Corporation
  20. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  21. * @link https://github.com/windowsazure/azure-sdk-for-php
  22. */
  23. namespace WindowsAzure\Blob;
  24. use WindowsAzure\Common\Internal\Utilities;
  25. use WindowsAzure\Common\Internal\Resources;
  26. use WindowsAzure\Common\Internal\Validate;
  27. use WindowsAzure\Common\Models\ServiceProperties;
  28. use WindowsAzure\Common\Internal\ServiceRestProxy;
  29. use WindowsAzure\Blob\Internal\IBlob;
  30. use WindowsAzure\Blob\Models\BlobServiceOptions;
  31. use WindowsAzure\Common\Models\GetServicePropertiesResult;
  32. use WindowsAzure\Blob\Models\ListContainersOptions;
  33. use WindowsAzure\Blob\Models\ListContainersResult;
  34. use WindowsAzure\Blob\Models\CreateContainerOptions;
  35. use WindowsAzure\Blob\Models\GetContainerPropertiesResult;
  36. use WindowsAzure\Blob\Models\GetContainerAclResult;
  37. use WindowsAzure\Blob\Models\SetContainerMetadataOptions;
  38. use WindowsAzure\Blob\Models\DeleteContainerOptions;
  39. use WindowsAzure\Blob\Models\ListBlobsOptions;
  40. use WindowsAzure\Blob\Models\ListBlobsResult;
  41. use WindowsAzure\Blob\Models\BlobType;
  42. use WindowsAzure\Blob\Models\CreateBlobOptions;
  43. use WindowsAzure\Blob\Models\BlobProperties;
  44. use WindowsAzure\Blob\Models\GetBlobPropertiesOptions;
  45. use WindowsAzure\Blob\Models\GetBlobPropertiesResult;
  46. use WindowsAzure\Blob\Models\SetBlobPropertiesOptions;
  47. use WindowsAzure\Blob\Models\SetBlobPropertiesResult;
  48. use WindowsAzure\Blob\Models\GetBlobMetadataOptions;
  49. use WindowsAzure\Blob\Models\GetBlobMetadataResult;
  50. use WindowsAzure\Blob\Models\SetBlobMetadataOptions;
  51. use WindowsAzure\Blob\Models\SetBlobMetadataResult;
  52. use WindowsAzure\Blob\Models\GetBlobOptions;
  53. use WindowsAzure\Blob\Models\GetBlobResult;
  54. use WindowsAzure\Blob\Models\DeleteBlobOptions;
  55. use WindowsAzure\Blob\Models\LeaseMode;
  56. use WindowsAzure\Blob\Models\AcquireLeaseOptions;
  57. use WindowsAzure\Blob\Models\AcquireLeaseResult;
  58. use WindowsAzure\Blob\Models\CreateBlobPagesOptions;
  59. use WindowsAzure\Blob\Models\CreateBlobPagesResult;
  60. use WindowsAzure\Blob\Models\PageWriteOption;
  61. use WindowsAzure\Blob\Models\ListPageBlobRangesOptions;
  62. use WindowsAzure\Blob\Models\ListPageBlobRangesResult;
  63. use WindowsAzure\Blob\Models\CreateBlobBlockOptions;
  64. use WindowsAzure\Blob\Models\CommitBlobBlocksOptions;
  65. use WindowsAzure\Blob\Models\BlockList;
  66. use WindowsAzure\Blob\Models\ListBlobBlocksOptions;
  67. use WindowsAzure\Blob\Models\ListBlobBlocksResult;
  68. use WindowsAzure\Blob\Models\CopyBlobOptions;
  69. use WindowsAzure\Blob\Models\CreateBlobSnapshotOptions;
  70. use WindowsAzure\Blob\Models\CreateBlobSnapshotResult;
  71. use WindowsAzure\Blob\Models\PageRange;
  72. use WindowsAzure\Blob\Models\CopyBlobResult;
  73. /**
  74. * This class constructs HTTP requests and receive HTTP responses for blob
  75. * service layer.
  76. *
  77. * @category Microsoft
  78. * @package WindowsAzure\Blob
  79. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  80. * @copyright 2012 Microsoft Corporation
  81. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  82. * @version Release: @package_version@
  83. * @link https://github.com/windowsazure/azure-sdk-for-php
  84. */
  85. class BlobRestProxy extends ServiceRestProxy implements IBlob
  86. {
  87. /**
  88. * Gets the copy blob source name with specified parameters.
  89. *
  90. * @param string $containerName The name of the container.
  91. * @param string $blobName The name of the blob.
  92. * @param Models\CopyBlobOptions $options The optional parameters.
  93. *
  94. * @return string
  95. */
  96. private function _getCopyBlobSourceName($containerName, $blobName, $options)
  97. {
  98. $sourceName = '/' . $this->getAccountName();
  99. $sourceName .= '/' . $this->_createPath($containerName, $blobName);
  100. if (!is_null($options->getSourceSnapshot())) {
  101. $sourceName .= '?snapshot=' . $options->getSourceSnapshot();
  102. }
  103. return $sourceName;
  104. }
  105. /**
  106. * Creates URI path for blob.
  107. *
  108. * @param string $container The container name.
  109. * @param string $blob The blob name.
  110. *
  111. * @return string
  112. */
  113. private function _createPath($container, $blob)
  114. {
  115. // Empty container means accessing default container
  116. if (empty($container)) {
  117. return $blob;
  118. } else {
  119. return $container . '/' . $blob;
  120. }
  121. }
  122. /**
  123. * Creates GetBlobPropertiesResult from headers array.
  124. *
  125. * @param array $headers The HTTP response headers array.
  126. *
  127. * @return GetBlobPropertiesResult
  128. */
  129. private function _getBlobPropertiesResultFromResponse($headers)
  130. {
  131. $result = new GetBlobPropertiesResult();
  132. $properties = new BlobProperties();
  133. $d = $headers[Resources::LAST_MODIFIED];
  134. $bType = $headers[Resources::X_MS_BLOB_TYPE];
  135. $cLength = intval($headers[Resources::CONTENT_LENGTH]);
  136. $lStatus = Utilities::tryGetValue($headers, Resources::X_MS_LEASE_STATUS);
  137. $cType = Utilities::tryGetValue($headers, Resources::CONTENT_TYPE);
  138. $cMD5 = Utilities::tryGetValue($headers, Resources::CONTENT_MD5);
  139. $cEncoding = Utilities::tryGetValue($headers, Resources::CONTENT_ENCODING);
  140. $cLanguage = Utilities::tryGetValue($headers, Resources::CONTENT_LANGUAGE);
  141. $cControl = Utilities::tryGetValue($headers, Resources::CACHE_CONTROL);
  142. $etag = $headers[Resources::ETAG];
  143. $metadata = $this->getMetadataArray($headers);
  144. if (array_key_exists(Resources::X_MS_BLOB_SEQUENCE_NUMBER, $headers)) {
  145. $sNumber = intval($headers[Resources::X_MS_BLOB_SEQUENCE_NUMBER]);
  146. $properties->setSequenceNumber($sNumber);
  147. }
  148. $properties->setBlobType($bType);
  149. $properties->setCacheControl($cControl);
  150. $properties->setContentEncoding($cEncoding);
  151. $properties->setContentLanguage($cLanguage);
  152. $properties->setContentLength($cLength);
  153. $properties->setContentMD5($cMD5);
  154. $properties->setContentType($cType);
  155. $properties->setETag($etag);
  156. $properties->setLastModified(Utilities::rfc1123ToDateTime($d));
  157. $properties->setLeaseStatus($lStatus);
  158. $result->setProperties($properties);
  159. $result->setMetadata($metadata);
  160. return $result;
  161. }
  162. /**
  163. * Helper method for getContainerProperties and getContainerMetadata.
  164. *
  165. * @param string $container The container name.
  166. * @param Models\BlobServiceOptions $options The optional parameters.
  167. * @param string $operation The operation string. Should be
  168. * 'metadata' to get metadata.
  169. *
  170. * @return Models\GetContainerPropertiesResult
  171. */
  172. private function _getContainerPropertiesImpl($container, $options = null,
  173. $operation = null
  174. ) {
  175. Validate::isString($container, 'container');
  176. $method = Resources::HTTP_GET;
  177. $headers = array();
  178. $queryParams = array();
  179. $postParams = array();
  180. $path = $container;
  181. $statusCode = Resources::STATUS_OK;
  182. if (is_null($options)) {
  183. $options = new BlobServiceOptions();
  184. }
  185. $this->addOptionalQueryParam(
  186. $queryParams,
  187. Resources::QP_REST_TYPE,
  188. 'container'
  189. );
  190. $this->addOptionalQueryParam(
  191. $queryParams,
  192. Resources::QP_COMP,
  193. $operation
  194. );
  195. $this->addOptionalQueryParam(
  196. $queryParams,
  197. Resources::QP_TIMEOUT,
  198. $options->getTimeout()
  199. );
  200. $response = $this->send(
  201. $method,
  202. $headers,
  203. $queryParams,
  204. $postParams,
  205. $path,
  206. $statusCode
  207. );
  208. $result = new GetContainerPropertiesResult();
  209. $metadata = $this->getMetadataArray($response->getHeader());
  210. $date = $response->getHeader(Resources::LAST_MODIFIED);
  211. $date = Utilities::rfc1123ToDateTime($date);
  212. $result->setETag($response->getHeader(Resources::ETAG));
  213. $result->setMetadata($metadata);
  214. $result->setLastModified($date);
  215. return $result;
  216. }
  217. /**
  218. * Adds optional create blob headers.
  219. *
  220. * @param CreateBlobOptions $options The optional parameters.
  221. * @param array $headers The HTTP request headers.
  222. *
  223. * @return array
  224. */
  225. private function _addCreateBlobOptionalHeaders($options, $headers)
  226. {
  227. $contentType = $options->getContentType();
  228. $metadata = $options->getMetadata();
  229. $blobContentType = $options->getBlobContentType();
  230. $blobContentEncoding = $options->getBlobContentEncoding();
  231. $blobContentLanguage = $options->getBlobContentLanguage();
  232. $blobContentMD5 = $options->getBlobContentMD5();
  233. $blobCacheControl = $options->getBlobCacheControl();
  234. $leaseId = $options->getLeaseId();
  235. if (!is_null($contentType)) {
  236. $this->addOptionalHeader(
  237. $headers,
  238. Resources::CONTENT_TYPE,
  239. $options->getContentType()
  240. );
  241. } else {
  242. $this->addOptionalHeader(
  243. $headers,
  244. Resources::CONTENT_TYPE,
  245. Resources::BINARY_FILE_TYPE
  246. );
  247. }
  248. $headers = $this->addMetadataHeaders($headers, $metadata);
  249. $headers = $this->addOptionalAccessConditionHeader(
  250. $headers, $options->getAccessCondition()
  251. );
  252. $this->addOptionalHeader(
  253. $headers,
  254. Resources::CONTENT_ENCODING,
  255. $options->getContentEncoding()
  256. );
  257. $this->addOptionalHeader(
  258. $headers,
  259. Resources::CONTENT_LANGUAGE,
  260. $options->getContentLanguage()
  261. );
  262. $this->addOptionalHeader(
  263. $headers,
  264. Resources::CONTENT_MD5,
  265. $options->getContentMD5()
  266. );
  267. $this->addOptionalHeader(
  268. $headers,
  269. Resources::CACHE_CONTROL,
  270. $options->getCacheControl()
  271. );
  272. $this->addOptionalHeader(
  273. $headers,
  274. Resources::X_MS_LEASE_ID,
  275. $leaseId
  276. );
  277. $this->addOptionalHeader(
  278. $headers,
  279. Resources::X_MS_BLOB_CONTENT_TYPE,
  280. $blobContentType
  281. );
  282. $this->addOptionalHeader(
  283. $headers,
  284. Resources::X_MS_BLOB_CONTENT_ENCODING,
  285. $blobContentEncoding
  286. );
  287. $this->addOptionalHeader(
  288. $headers,
  289. Resources::X_MS_BLOB_CONTENT_LANGUAGE,
  290. $blobContentLanguage
  291. );
  292. $this->addOptionalHeader(
  293. $headers,
  294. Resources::X_MS_BLOB_CONTENT_MD5,
  295. $blobContentMD5
  296. );
  297. $this->addOptionalHeader(
  298. $headers,
  299. Resources::X_MS_BLOB_CACHE_CONTROL,
  300. $blobCacheControl
  301. );
  302. return $headers;
  303. }
  304. /**
  305. * Adds Range header to the headers array.
  306. *
  307. * @param array $headers The HTTP request headers.
  308. * @param integer $start The start byte.
  309. * @param integer $end The end byte.
  310. *
  311. * @return array
  312. */
  313. private function _addOptionalRangeHeader($headers, $start, $end)
  314. {
  315. if (!is_null($start) || !is_null($end)) {
  316. $range = $start . '-' . $end;
  317. $rangeValue = 'bytes=' . $range;
  318. $this->addOptionalHeader($headers, Resources::RANGE, $rangeValue);
  319. }
  320. return $headers;
  321. }
  322. /**
  323. * Does the actual work for leasing a blob.
  324. *
  325. * @param string $leaseAction The lease action string.
  326. * @param string $container The container name.
  327. * @param string $blob The blob to lease name.
  328. * @param string $leaseId The existing lease id.
  329. * @param BlobServiceOptions $options The optional parameters.
  330. * @param AccessCondition $accessCondition The access conditions.
  331. *
  332. * @return AcquireLeaseResult
  333. */
  334. private function _putLeaseImpl($leaseAction, $container, $blob, $leaseId,
  335. $options, $accessCondition = null
  336. ) {
  337. Validate::isString($blob, 'blob');
  338. Validate::notNullOrEmpty($blob, 'blob');
  339. Validate::isString($container, 'container');
  340. $method = Resources::HTTP_PUT;
  341. $headers = array();
  342. $queryParams = array();
  343. $postParams = array();
  344. $path = $this->_createPath($container, $blob);
  345. $statusCode = Resources::EMPTY_STRING;
  346. switch ($leaseAction) {
  347. case LeaseMode::ACQUIRE_ACTION:
  348. $statusCode = Resources::STATUS_CREATED;
  349. break;
  350. case LeaseMode::RENEW_ACTION:
  351. $statusCode = Resources::STATUS_OK;
  352. break;
  353. case LeaseMode::RELEASE_ACTION:
  354. $statusCode = Resources::STATUS_OK;
  355. break;
  356. case LeaseMode::BREAK_ACTION:
  357. $statusCode = Resources::STATUS_ACCEPTED;
  358. break;
  359. default:
  360. throw new \Exception(Resources::NOT_IMPLEMENTED_MSG);
  361. }
  362. if (!is_null($options)) {
  363. $options = new BlobServiceOptions();
  364. }
  365. $headers = $this->addOptionalAccessConditionHeader(
  366. $headers, $accessCondition
  367. );
  368. $this->addOptionalHeader($headers, Resources::X_MS_LEASE_ID, $leaseId);
  369. $this->addOptionalHeader(
  370. $headers,
  371. Resources::X_MS_LEASE_ACTION,
  372. $leaseAction
  373. );
  374. $this->addOptionalQueryParam($queryParams, Resources::QP_COMP, 'lease');
  375. $this->addOptionalQueryParam(
  376. $queryParams,
  377. Resources::QP_TIMEOUT,
  378. $options->getTimeout()
  379. );
  380. $response = $this->send(
  381. $method,
  382. $headers,
  383. $queryParams,
  384. $postParams,
  385. $path,
  386. $statusCode
  387. );
  388. return AcquireLeaseResult::create($response->getHeader());
  389. }
  390. /**
  391. * Does actual work for create and clear blob pages.
  392. *
  393. * @param string $action Either clear or create.
  394. * @param string $container The container name.
  395. * @param string $blob The blob name.
  396. * @param PageRange $range The page ranges.
  397. * @param string|resource $content The content stream.
  398. * @param CreateBlobPagesOptions $options The optional parameters.
  399. *
  400. * @return CreateBlobPagesResult
  401. */
  402. private function _updatePageBlobPagesImpl($action, $container, $blob, $range,
  403. $content, $options = null
  404. ) {
  405. Validate::isString($blob, 'blob');
  406. Validate::notNullOrEmpty($blob, 'blob');
  407. Validate::isString($container, 'container');
  408. Validate::isTrue(
  409. $range instanceof PageRange,
  410. sprintf(
  411. Resources::INVALID_PARAM_MSG,
  412. 'range',
  413. get_class(new PageRange())
  414. )
  415. );
  416. Validate::isTrue(
  417. is_string($content) || is_resource($content),
  418. sprintf(Resources::INVALID_PARAM_MSG, 'content', 'string|resource')
  419. );
  420. $method = Resources::HTTP_PUT;
  421. $headers = array();
  422. $queryParams = array();
  423. $postParams = array();
  424. $path = $this->_createPath($container, $blob);
  425. $statusCode = Resources::STATUS_CREATED;
  426. // If read file failed for any reason it will throw an exception.
  427. $body = is_resource($content) ? stream_get_contents($content) : $content;
  428. if (is_null($options)) {
  429. $options = new CreateBlobPagesOptions();
  430. }
  431. $headers = $this->_addOptionalRangeHeader(
  432. $headers, $range->getStart(), $range->getEnd()
  433. );
  434. $headers = $this->addOptionalAccessConditionHeader(
  435. $headers, $options->getAccessCondition()
  436. );
  437. $this->addOptionalHeader(
  438. $headers,
  439. Resources::X_MS_LEASE_ID,
  440. $options->getLeaseId()
  441. );
  442. $this->addOptionalHeader(
  443. $headers,
  444. Resources::CONTENT_MD5,
  445. $options->getContentMD5()
  446. );
  447. $this->addOptionalHeader(
  448. $headers,
  449. Resources::X_MS_PAGE_WRITE,
  450. $action
  451. );
  452. $this->addOptionalHeader(
  453. $headers,
  454. Resources::CONTENT_TYPE,
  455. Resources::URL_ENCODED_CONTENT_TYPE
  456. );
  457. $this->addOptionalQueryParam($queryParams, Resources::QP_COMP, 'page');
  458. $this->addOptionalQueryParam(
  459. $queryParams,
  460. Resources::QP_TIMEOUT,
  461. $options->getTimeout()
  462. );
  463. $response = $this->send(
  464. $method,
  465. $headers,
  466. $queryParams,
  467. $postParams,
  468. $path,
  469. $statusCode,
  470. $body
  471. );
  472. return CreateBlobPagesResult::create($response->getHeader());
  473. }
  474. /**
  475. * Gets the properties of the Blob service.
  476. *
  477. * @param Models\BlobServiceOptions $options The optional parameters.
  478. *
  479. * @return WindowsAzure\Common\Models\GetServicePropertiesResult
  480. *
  481. * @see http://msdn.microsoft.com/en-us/library/windowsazure/hh452239.aspx
  482. */
  483. public function getServiceProperties($options = null)
  484. {
  485. $method = Resources::HTTP_GET;
  486. $headers = array();
  487. $queryParams = array();
  488. $postParams = array();
  489. $path = Resources::EMPTY_STRING;
  490. $statusCode = Resources::STATUS_OK;
  491. if (is_null($options)) {
  492. $options = new BlobServiceOptions();
  493. }
  494. $this->addOptionalQueryParam(
  495. $queryParams,
  496. Resources::QP_TIMEOUT,
  497. $options->getTimeout()
  498. );
  499. $this->addOptionalQueryParam(
  500. $queryParams,
  501. Resources::QP_REST_TYPE,
  502. 'service'
  503. );
  504. $this->addOptionalQueryParam(
  505. $queryParams,
  506. Resources::QP_COMP,
  507. 'properties'
  508. );
  509. $response = $this->send(
  510. $method,
  511. $headers,
  512. $queryParams,
  513. $postParams,
  514. $path,
  515. $statusCode
  516. );
  517. $parsed = $this->dataSerializer->unserialize($response->getBody());
  518. return GetServicePropertiesResult::create($parsed);
  519. }
  520. /**
  521. * Sets the properties of the Blob service.
  522. *
  523. * It's recommended to use getServiceProperties, alter the returned object and
  524. * then use setServiceProperties with this altered object.
  525. *
  526. * @param ServiceProperties $serviceProperties The service properties.
  527. * @param Models\BlobServiceOptions $options The optional parameters.
  528. *
  529. * @return none
  530. *
  531. * @see http://msdn.microsoft.com/en-us/library/windowsazure/hh452235.aspx
  532. */
  533. public function setServiceProperties($serviceProperties, $options = null)
  534. {
  535. Validate::isTrue(
  536. $serviceProperties instanceof ServiceProperties,
  537. Resources::INVALID_SVC_PROP_MSG
  538. );
  539. $method = Resources::HTTP_PUT;
  540. $headers = array();
  541. $queryParams = array();
  542. $postParams = array();
  543. $statusCode = Resources::STATUS_ACCEPTED;
  544. $path = Resources::EMPTY_STRING;
  545. $body = $serviceProperties->toXml($this->dataSerializer);
  546. if (is_null($options)) {
  547. $options = new BlobServiceOptions();
  548. }
  549. $this->addOptionalQueryParam(
  550. $queryParams,
  551. Resources::QP_REST_TYPE,
  552. 'service'
  553. );
  554. $this->addOptionalQueryParam(
  555. $queryParams,
  556. Resources::QP_COMP,
  557. 'properties'
  558. );
  559. $this->addOptionalQueryParam(
  560. $queryParams,
  561. Resources::QP_TIMEOUT,
  562. $options->getTimeout()
  563. );
  564. $this->addOptionalHeader(
  565. $headers,
  566. Resources::CONTENT_TYPE,
  567. Resources::URL_ENCODED_CONTENT_TYPE
  568. );
  569. $this->send(
  570. $method,
  571. $headers,
  572. $queryParams,
  573. $postParams,
  574. $path,
  575. $statusCode,
  576. $body
  577. );
  578. }
  579. /**
  580. * Lists all of the containers in the given storage account.
  581. *
  582. * @param Models\ListContainersOptions $options The optional parameters.
  583. *
  584. * @return WindowsAzure\Blob\Models\ListContainersResult
  585. *
  586. * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179352.aspx
  587. */
  588. public function listContainers($options = null)
  589. {
  590. $method = Resources::HTTP_GET;
  591. $headers = array();
  592. $queryParams = array();
  593. $postParams = array();
  594. $path = Resources::EMPTY_STRING;
  595. $statusCode = Resources::STATUS_OK;
  596. if (is_null($options)) {
  597. $options = new ListContainersOptions();
  598. }
  599. $this->addOptionalQueryParam(
  600. $queryParams,
  601. Resources::QP_TIMEOUT,
  602. $options->getTimeout()
  603. );
  604. $this->addOptionalQueryParam(
  605. $queryParams,
  606. Resources::QP_COMP,
  607. 'list'
  608. );
  609. $this->addOptionalQueryParam(
  610. $queryParams,
  611. Resources::QP_PREFIX,
  612. $options->getPrefix()
  613. );
  614. $this->addOptionalQueryParam(
  615. $queryParams,
  616. Resources::QP_MARKER,
  617. $options->getMarker()
  618. );
  619. $this->addOptionalQueryParam(
  620. $queryParams,
  621. Resources::QP_MAX_RESULTS,
  622. $options->getMaxResults()
  623. );
  624. $isInclude = $options->getIncludeMetadata();
  625. $isInclude = $isInclude ? 'metadata' : null;
  626. $this->addOptionalQueryParam(
  627. $queryParams,
  628. Resources::QP_INCLUDE,
  629. $isInclude
  630. );
  631. $response = $this->send(
  632. $method,
  633. $headers,
  634. $queryParams,
  635. $postParams,
  636. $path,
  637. $statusCode
  638. );
  639. $parsed = $this->dataSerializer->unserialize($response->getBody());
  640. return ListContainersResult::create($parsed);
  641. }
  642. /**
  643. * Creates a new container in the given storage account.
  644. *
  645. * @param string $container The container name.
  646. * @param Models\CreateContainerOptions $options The optional parameters.
  647. *
  648. * @return none
  649. *
  650. * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179468.aspx
  651. */
  652. public function createContainer($container, $options = null)
  653. {
  654. Validate::isString($container, 'container');
  655. Validate::notNullOrEmpty($container, 'container');
  656. $method = Resources::HTTP_PUT;
  657. $headers = array();
  658. $postParams = array();
  659. $queryParams = array(Resources::QP_REST_TYPE => 'container');
  660. $path = $container;
  661. $statusCode = Resources::STATUS_CREATED;
  662. if (is_null($options)) {
  663. $options = new CreateContainerOptions();
  664. }
  665. $this->addOptionalQueryParam(
  666. $queryParams,
  667. Resources::QP_TIMEOUT,
  668. $options->getTimeout()
  669. );
  670. $metadata = $options->getMetadata();
  671. $headers = $this->generateMetadataHeaders($metadata);
  672. $this->addOptionalHeader(
  673. $headers,
  674. Resources::X_MS_BLOB_PUBLIC_ACCESS,
  675. $options->getPublicAccess()
  676. );
  677. $this->send(
  678. $method,
  679. $headers,
  680. $queryParams,
  681. $postParams,
  682. $path,
  683. $statusCode
  684. );
  685. }
  686. /**
  687. * Creates a new container in the given storage account.
  688. *
  689. * @param string $container The container name.
  690. * @param Models\DeleteContainerOptions $options The optional parameters.
  691. *
  692. * @return none
  693. *
  694. * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179408.aspx
  695. */
  696. public function deleteContainer($container, $options = null)
  697. {
  698. Validate::isString($container, 'container');
  699. Validate::notNullOrEmpty($container, 'container');
  700. $method = Resources::HTTP_DELETE;
  701. $headers = array();
  702. $postParams = array();
  703. $queryParams = array();
  704. $path = $container;
  705. $statusCode = Resources::STATUS_ACCEPTED;
  706. if (is_null($options)) {
  707. $options = new DeleteContainerOptions();
  708. }
  709. $headers = $this->addOptionalAccessConditionHeader(
  710. $headers, $options->getAccessCondition()
  711. );
  712. $this->addOptionalQueryParam(
  713. $queryParams,
  714. Resources::QP_TIMEOUT,
  715. $options->getTimeout()
  716. );
  717. $this->addOptionalQueryParam(
  718. $queryParams,
  719. Resources::QP_REST_TYPE,
  720. 'container'
  721. );
  722. $this->send(
  723. $method,
  724. $headers,
  725. $queryParams,
  726. $postParams,
  727. $path,
  728. $statusCode
  729. );
  730. }
  731. /**
  732. * Returns all properties and metadata on the container.
  733. *
  734. * @param string $container name
  735. * @param Models\BlobServiceOptions $options optional parameters
  736. *
  737. * @return Models\GetContainerPropertiesResult
  738. *
  739. * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179370.aspx
  740. */
  741. public function getContainerProperties($container, $options = null)
  742. {
  743. return $this->_getContainerPropertiesImpl($container, $options);
  744. }
  745. /**
  746. * Returns only user-defined metadata for the specified container.
  747. *
  748. * @param string $container name
  749. * @param Models\BlobServiceOptions $options optional parameters
  750. *
  751. * @return Models\GetContainerPropertiesResult
  752. *
  753. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee691976.aspx
  754. */
  755. public function getContainerMetadata($container, $options = null)
  756. {
  757. return $this->_getContainerPropertiesImpl($container, $options, 'metadata');
  758. }
  759. /**
  760. * Gets the access control list (ACL) and any container-level access policies
  761. * for the container.
  762. *
  763. * @param string $container The container name.
  764. * @param Models\BlobServiceOptions $options The optional parameters.
  765. *
  766. * @return Models\GetContainerAclResult
  767. *
  768. * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179469.aspx
  769. */
  770. public function getContainerAcl($container, $options = null)
  771. {
  772. Validate::isString($container, 'container');
  773. $method = Resources::HTTP_GET;
  774. $headers = array();
  775. $postParams = array();
  776. $queryParams = array();
  777. $path = $container;
  778. $statusCode = Resources::STATUS_OK;
  779. if (is_null($options)) {
  780. $options = new BlobServiceOptions();
  781. }
  782. $this->addOptionalQueryParam(
  783. $queryParams,
  784. Resources::QP_TIMEOUT,
  785. $options->getTimeout()
  786. );
  787. $this->addOptionalQueryParam(
  788. $queryParams,
  789. Resources::QP_REST_TYPE,
  790. 'container'
  791. );
  792. $this->addOptionalQueryParam(
  793. $queryParams,
  794. Resources::QP_COMP,
  795. 'acl'
  796. );
  797. $response = $this->send(
  798. $method,
  799. $headers,
  800. $queryParams,
  801. $postParams,
  802. $path,
  803. $statusCode
  804. );
  805. $access = $response->getHeader(Resources::X_MS_BLOB_PUBLIC_ACCESS);
  806. $etag = $response->getHeader(Resources::ETAG);
  807. $modified = $response->getHeader(Resources::LAST_MODIFIED);
  808. $modifiedDate = Utilities::convertToDateTime($modified);
  809. $parsed = $this->dataSerializer->unserialize($response->getBody());
  810. return GetContainerAclResult::create($access, $etag, $modifiedDate, $parsed);
  811. }
  812. /**
  813. * Sets the ACL and any container-level access policies for the container.
  814. *
  815. * @param string $container name
  816. * @param Models\ContainerAcl $acl access control list for container
  817. * @param Models\BlobServiceOptions $options optional parameters
  818. *
  819. * @return none
  820. *
  821. * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179391.aspx
  822. */
  823. public function setContainerAcl($container, $acl, $options = null)
  824. {
  825. Validate::isString($container, 'container');
  826. Validate::notNullOrEmpty($acl, 'acl');
  827. $method = Resources::HTTP_PUT;
  828. $headers = array();
  829. $postParams = array();
  830. $queryParams = array();
  831. $path = $container;
  832. $statusCode = Resources::STATUS_OK;
  833. $body = $acl->toXml($this->dataSerializer);
  834. if (is_null($options)) {
  835. $options = new BlobServiceOptions();
  836. }
  837. $this->addOptionalQueryParam(
  838. $queryParams,
  839. Resources::QP_TIMEOUT,
  840. $options->getTimeout()
  841. );
  842. $this->addOptionalQueryParam(
  843. $queryParams,
  844. Resources::QP_REST_TYPE,
  845. 'container'
  846. );
  847. $this->addOptionalQueryParam(
  848. $queryParams,
  849. Resources::QP_COMP,
  850. 'acl'
  851. );
  852. $this->addOptionalHeader(
  853. $headers,
  854. Resources::X_MS_BLOB_PUBLIC_ACCESS,
  855. $acl->getPublicAccess()
  856. );
  857. $this->addOptionalHeader(
  858. $headers,
  859. Resources::CONTENT_TYPE,
  860. Resources::URL_ENCODED_CONTENT_TYPE
  861. );
  862. $this->send(
  863. $method,
  864. $headers,
  865. $queryParams,
  866. $postParams,
  867. $path,
  868. $statusCode,
  869. $body
  870. );
  871. }
  872. /**
  873. * Sets metadata headers on the container.
  874. *
  875. * @param string $container name
  876. * @param array $metadata metadata key/value pair.
  877. * @param Models\SetContainerMetadataOptions $options optional parameters
  878. *
  879. * @return none
  880. *
  881. * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179362.aspx
  882. */
  883. public function setContainerMetadata($container, $metadata, $options = null)
  884. {
  885. Validate::isString($container, 'container');
  886. $this->validateMetadata($metadata);
  887. $method = Resources::HTTP_PUT;
  888. $headers = $this->generateMetadataHeaders($metadata);
  889. $postParams = array();
  890. $queryParams = array();
  891. $path = $container;
  892. $statusCode = Resources::STATUS_OK;
  893. if (is_null($options)) {
  894. $options = new SetContainerMetadataOptions();
  895. }
  896. $this->addOptionalQueryParam(
  897. $queryParams,
  898. Resources::QP_TIMEOUT,
  899. $options->getTimeout()
  900. );
  901. $this->addOptionalQueryParam(
  902. $queryParams,
  903. Resources::QP_REST_TYPE,
  904. 'container'
  905. );
  906. $this->addOptionalQueryParam(
  907. $queryParams,
  908. Resources::QP_COMP,
  909. 'metadata'
  910. );
  911. $headers = $this->addOptionalAccessConditionHeader(
  912. $headers,
  913. $options->getAccessCondition()
  914. );
  915. $this->send(
  916. $method,
  917. $headers,
  918. $queryParams,
  919. $postParams,
  920. $path,
  921. $statusCode
  922. );
  923. }
  924. /**
  925. * Lists all of the blobs in the given container.
  926. *
  927. * @param string $container The container name.
  928. * @param Models\ListBlobsOptions $options The optional parameters.
  929. *
  930. * @return Models\ListBlobsResult
  931. *
  932. * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd135734.aspx
  933. */
  934. public function listBlobs($container, $options = null)
  935. {
  936. Validate::isString($container, 'container');
  937. $method = Resources::HTTP_GET;
  938. $headers = array();
  939. $postParams = array();
  940. $queryParams = array();
  941. $path = $container;
  942. $statusCode = Resources::STATUS_OK;
  943. if (is_null($options)) {
  944. $options = new ListBlobsOptions();
  945. }
  946. $this->addOptionalQueryParam(
  947. $queryParams,
  948. Resources::QP_TIMEOUT,
  949. $options->getTimeout()
  950. );
  951. $this->addOptionalQueryParam(
  952. $queryParams,
  953. Resources::QP_REST_TYPE,
  954. 'container'
  955. );
  956. $this->addOptionalQueryParam(
  957. $queryParams,
  958. Resources::QP_COMP,
  959. 'list'
  960. );
  961. $this->addOptionalQueryParam(
  962. $queryParams,
  963. Resources::QP_PREFIX,
  964. $options->getPrefix()
  965. );
  966. $this->addOptionalQueryParam(
  967. $queryParams,
  968. Resources::QP_MARKER,
  969. $options->getMarker()
  970. );
  971. $this->addOptionalQueryParam(
  972. $queryParams,
  973. Resources::QP_DELIMITER,
  974. $options->getDelimiter()
  975. );
  976. $this->addOptionalQueryParam(
  977. $queryParams,
  978. Resources::QP_MAX_RESULTS,
  979. $options->getMaxResults()
  980. );
  981. $includeMetadata = $options->getIncludeMetadata();
  982. $includeSnapshots = $options->getIncludeSnapshots();
  983. $includeUncommittedBlobs = $options->getIncludeUncommittedBlobs();
  984. $includeValue = $this->groupQueryValues(
  985. array(
  986. $includeMetadata ? 'metadata' : null,
  987. $includeSnapshots ? 'snapshots' : null,
  988. $includeUncommittedBlobs ? 'uncommittedblobs' : null
  989. )
  990. );
  991. $this->addOptionalQueryParam(
  992. $queryParams,
  993. Resources::QP_INCLUDE,
  994. $includeValue
  995. );
  996. $response = $this->send(
  997. $method,
  998. $headers,
  999. $queryParams,
  1000. $postParams,
  1001. $path,
  1002. $statusCode
  1003. );
  1004. $parsed = $this->dataSerializer->unserialize($response->getBody());
  1005. return ListBlobsResult::create($parsed);
  1006. }
  1007. /**
  1008. * Creates a new page blob. Note that calling createPageBlob to create a page
  1009. * blob only initializes the blob.
  1010. * To add content to a page blob, call createBlobPages method.
  1011. *
  1012. * @param string $container The container name.
  1013. * @param string $blob The blob name.
  1014. * @param integer $length Specifies the maximum size for the
  1015. * page blob, up to 1 TB. The page blob size must be aligned to a 512-byte
  1016. * boundary.
  1017. * @param Models\CreateBlobOptions $options The optional parameters.
  1018. *
  1019. * @return CopyBlobResult
  1020. *
  1021. * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179451.aspx
  1022. */
  1023. public function createPageBlob($container, $blob, $length, $options = null)
  1024. {
  1025. Validate::isString($container, 'container');
  1026. Validate::isString($blob, 'blob');
  1027. Validate::notNullOrEmpty($blob, 'blob');
  1028. Validate::isInteger($length, 'length');
  1029. Validate::notNull($length, 'length');
  1030. $method = Resources::HTTP_PUT;
  1031. $headers = array();
  1032. $postParams = array();
  1033. $queryParams = array();
  1034. $path = $this->_createPath($container, $blob);
  1035. $statusCode = Resources::STATUS_CREATED;
  1036. if (is_null($options)) {
  1037. $options = new CreateBlobOptions();
  1038. }
  1039. $this->addOptionalHeader(
  1040. $headers,
  1041. Resources::X_MS_BLOB_TYPE,
  1042. BlobType::PAGE_BLOB
  1043. );
  1044. $this->addOptionalHeader(
  1045. $headers,
  1046. Resources::X_MS_BLOB_CONTENT_LENGTH,
  1047. $length
  1048. );
  1049. $this->addOptionalHeader(
  1050. $headers,
  1051. Resources::X_MS_BLOB_SEQUENCE_NUMBER,
  1052. $options->getSequenceNumber()
  1053. );
  1054. $headers = $this->_addCreateBlobOptionalHeaders($options, $headers);
  1055. $this->addOptionalQueryParam(
  1056. $queryParams,
  1057. Resources::QP_TIMEOUT,
  1058. $options->getTimeout()
  1059. );
  1060. $response = $this->send(
  1061. $method,
  1062. $headers,
  1063. $queryParams,
  1064. $postParams,
  1065. $path,
  1066. $statusCode
  1067. );
  1068. return CopyBlobResult::create($response->getHeader());
  1069. }
  1070. /**
  1071. * Creates a new block blob or updates the content of an existing block blob.
  1072. *
  1073. * Updating an existing block blob overwrites any existing metadata on the blob.
  1074. * Partial updates are not supported with createBlockBlob the content of the
  1075. * existing blob is overwritten with the content of the new blob. To perform a
  1076. * partial update of the content o f a block blob, use the createBlockList
  1077. * method.
  1078. * Note that the default content type is application/octet-stream.
  1079. *
  1080. * @param string $container The name of the container.
  1081. * @param string $blob The name of the blob.
  1082. * @param string|resource $content The content of the blob.
  1083. * @param Models\CreateBlobOptions $options The optional parameters.
  1084. *
  1085. * @return CopyBlobResult
  1086. *
  1087. * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179451.aspx
  1088. */
  1089. public function createBlockBlob($container, $blob, $content, $options = null)
  1090. {
  1091. Validate::isString($container, 'container');
  1092. Validate::isString($blob, 'blob');
  1093. Validate::notNullOrEmpty($blob, 'blob');
  1094. Validate::isTrue(
  1095. is_string($content) || is_resource($content),
  1096. sprintf(Resources::INVALID_PARAM_MSG, 'content', 'string|resource')
  1097. );
  1098. $method = Resources::HTTP_PUT;
  1099. $headers = array();
  1100. $postParams = array();
  1101. $queryParams = array();
  1102. $path = $this->_createPath($container, $blob);
  1103. $statusCode = Resources::STATUS_CREATED;
  1104. // If read file failed for any reason it will throw an exception.
  1105. $body = is_resource($content) ? stream_get_contents($content) : $content;
  1106. if (is_null($options)) {
  1107. $options = new CreateBlobOptions();
  1108. }
  1109. $headers = $this->_addCreateBlobOptionalHeaders($options, $headers);
  1110. $this->addOptionalHeader(
  1111. $headers,
  1112. Resources::X_MS_BLOB_TYPE,
  1113. BlobType::BLOCK_BLOB
  1114. );
  1115. $this->addOptionalQueryParam(
  1116. $queryParams,
  1117. Resources::QP_TIMEOUT,
  1118. $options->getTimeout()
  1119. );
  1120. $response = $this->send(
  1121. $method,
  1122. $headers,
  1123. $queryParams,
  1124. $postParams,
  1125. $path,
  1126. $statusCode,
  1127. $body
  1128. );
  1129. return CopyBlobResult::create($response->getHeader());
  1130. }
  1131. /**
  1132. * Clears a range of pages from the blob.
  1133. *
  1134. * @param string $container name of the container
  1135. * @param string $blob name of the blob
  1136. * @param Models\PageRange $range Can be up to the value of the
  1137. * blob's full size. Note that ranges must be aligned to 512 (0-511, 512-1023)
  1138. * @param Models\CreateBlobPagesOptions $options optional parameters
  1139. *
  1140. * @return Models\CreateBlobPagesResult.
  1141. *
  1142. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee691975.aspx
  1143. */
  1144. public function clearBlobPages($container, $blob, $range, $options = null)
  1145. {
  1146. return $this->_updatePageBlobPagesImpl(
  1147. PageWriteOption::CLEAR_OPTION,
  1148. $container,
  1149. $blob,
  1150. $range,
  1151. Resources::EMPTY_STRING,
  1152. $options
  1153. );
  1154. }
  1155. /**
  1156. * Creates a range of pages to a page blob.
  1157. *
  1158. * @param string $container name of the container
  1159. * @param string $blob name of the blob
  1160. * @param Models\PageRange $range Can be up to 4 MB in size
  1161. * Note that ranges must be aligned to 512 (0-511, 512-1023)
  1162. * @param string $content the blob contents.
  1163. * @param Models\CreateBlobPagesOptions $options optional parameters
  1164. *
  1165. * @return Models\CreateBlobPagesResult.
  1166. *
  1167. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee691975.aspx
  1168. */
  1169. public function createBlobPages($container, $blob, $range, $content,
  1170. $options = null
  1171. ) {
  1172. return $this->_updatePageBlobPagesImpl(
  1173. PageWriteOption::UPDATE_OPTION,
  1174. $container,
  1175. $blob,
  1176. $range,
  1177. $content,
  1178. $options
  1179. );
  1180. }
  1181. /**
  1182. * Creates a new block to be committed as part of a block blob.
  1183. *
  1184. * @param string $container name of the container
  1185. * @param string $blob name of the blob
  1186. * @param string $blockId must be less than or equal to
  1187. * 64 bytes in size. For a given blob, the length of the value specified for the
  1188. * blockid parameter must be the same size for each block.
  1189. * @param string $content the blob block contents
  1190. * @param Models\CreateBlobBlockOptions $options optional parameters
  1191. *
  1192. * @return none
  1193. *
  1194. * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd135726.aspx
  1195. */
  1196. public function createBlobBlock($container, $blob, $blockId, $content,
  1197. $options = null
  1198. ) {
  1199. Validate::isString($container, 'container');
  1200. Validate::isString($blob, 'blob');
  1201. Validate::notNullOrEmpty($blob, 'blob');
  1202. Validate::isString($blockId, 'blockId');
  1203. Validate::notNullOrEmpty($blockId, 'blockId');
  1204. Validate::isTrue(
  1205. is_string($content) || is_resource($content),
  1206. sprintf(Resources::INVALID_PARAM_MSG, 'content', 'string|resource')
  1207. );
  1208. $method = Resources::HTTP_PUT;
  1209. $headers = array();
  1210. $postParams = array();
  1211. $queryParams = array();
  1212. $path = $this->_createPath($container, $blob);
  1213. $statusCode = Resources::STATUS_CREATED;
  1214. $body = $content;
  1215. if (is_null($options)) {
  1216. $options = new CreateBlobBlockOptions();
  1217. }
  1218. $this->addOptionalHeader(
  1219. $headers,
  1220. Resources::X_MS_LEASE_ID,
  1221. $options->getLeaseId()
  1222. );
  1223. $this->addOptionalHeader(
  1224. $headers,
  1225. Resources::CONTENT_MD5,
  1226. $options->getContentMD5()
  1227. );
  1228. $this->addOptionalHeader(
  1229. $headers,
  1230. Resources::CONTENT_TYPE,
  1231. Resources::URL_ENCODED_CONTENT_TYPE
  1232. );
  1233. $this->addOptionalQueryParam(
  1234. $queryParams,
  1235. Resources::QP_TIMEOUT,
  1236. $options->getTimeout()
  1237. );
  1238. $this->addOptionalQueryParam(
  1239. $queryParams,
  1240. Resources::QP_COMP,
  1241. 'block'
  1242. );
  1243. $this->addOptionalQueryParam(
  1244. $queryParams,
  1245. Resources::QP_BLOCKID,
  1246. base64_encode($blockId)
  1247. );
  1248. $this->send(
  1249. $method,
  1250. $headers,
  1251. $queryParams,
  1252. $postParams,
  1253. $path,
  1254. $statusCode,
  1255. $body
  1256. );
  1257. }
  1258. /**
  1259. * This method writes a blob by specifying the list of block IDs that make up the
  1260. * blob. In order to be written as part of a blob, a block must have been
  1261. * successfully written to the server in a prior createBlobBlock method.
  1262. *
  1263. * You can call Put Block List to update a blob by uploading only those blocks
  1264. * that have changed, then committing the new and existing blocks together.
  1265. * You can do this by specifying whether to commit a block from the committed
  1266. * block list or from the uncommitted block list, or to commit the most recently
  1267. * uploaded version of the block, whichever list it may belong to.
  1268. *
  1269. * @param string $container The container name.
  1270. * @param string $blob The blob name.
  1271. * @param Models\BlockList|array $blockList The block entries.
  1272. * @param Models\CommitBlobBlocksOptions $options The optional parameters.
  1273. *
  1274. * @return none
  1275. *
  1276. * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179467.aspx
  1277. */
  1278. public function commitBlobBlocks($container, $blob, $blockList, $options = null)
  1279. {
  1280. Validate::isString($container, 'container');
  1281. Validate::isString($blob, 'blob');
  1282. Validate::notNullOrEmpty($blob, 'blob');
  1283. Validate::isTrue(
  1284. $blockList instanceof BlockList || is_array($blockList),
  1285. sprintf(
  1286. Resources::INVALID_PARAM_MSG,
  1287. 'blockList',
  1288. get_class(new BlockList())
  1289. )
  1290. );
  1291. $method = Resources::HTTP_PUT;
  1292. $headers = array();
  1293. $postParams = array();
  1294. $queryParams = array();
  1295. $path = $this->_createPath($container, $blob);
  1296. $statusCode = Resources::STATUS_CREATED;
  1297. $isArray = is_array($blockList);
  1298. $blockList = $isArray ? BlockList::create($blockList) : $blockList;
  1299. $body = $blockList->toXml($this->dataSerializer);
  1300. if (is_null($options)) {
  1301. $options = new CommitBlobBlocksOptions();
  1302. }
  1303. $blobContentType = $options->getBlobContentType();
  1304. $blobContentEncoding = $options->getBlobContentEncoding();
  1305. $blobContentLanguage = $options->getBlobContentLanguage();
  1306. $blobContentMD5 = $options->getBlobContentMD5();
  1307. $blobCacheControl = $options->getBlobCacheControl();
  1308. $leaseId = $options->getLeaseId();
  1309. $contentType = Resources::URL_ENCODED_CONTENT_TYPE;
  1310. $metadata = $options->getMetadata();
  1311. $headers = $this->generateMetadataHeaders($metadata);
  1312. $headers = $this->addOptionalAccessConditionHeader(
  1313. $headers, $options->getAccessCondition()
  1314. );
  1315. $this->addOptionalHeader(
  1316. $headers,
  1317. Resources::X_MS_LEASE_ID,
  1318. $leaseId
  1319. );
  1320. $this->addOptionalHeader(
  1321. $headers,
  1322. Resources::X_MS_BLOB_CACHE_CONTROL,
  1323. $blobCacheControl
  1324. );
  1325. $this->addOptionalHeader(
  1326. $headers,
  1327. Resources::X_MS_BLOB_CONTENT_TYPE,
  1328. $blobContentType
  1329. );
  1330. $this->addOptionalHeader(
  1331. $headers,
  1332. Resources::X_MS_BLOB_CONTENT_ENCODING,
  1333. $blobContentEncoding
  1334. );
  1335. $this->addOptionalHeader(
  1336. $headers,
  1337. Resources::X_MS_BLOB_CONTENT_LANGUAGE,
  1338. $blobContentLanguage
  1339. );
  1340. $this->addOptionalHeader(
  1341. $headers,
  1342. Resources::X_MS_BLOB_CONTENT_MD5,
  1343. $blobContentMD5
  1344. );
  1345. $this->addOptionalHeader(
  1346. $headers,
  1347. Resources::CONTENT_TYPE,
  1348. $contentType
  1349. );
  1350. $this->addOptionalQueryParam(
  1351. $queryParams,
  1352. Resources::QP_TIMEOUT,
  1353. $options->getTimeout()
  1354. );
  1355. $this->addOptionalQueryParam(
  1356. $queryParams,
  1357. Resources::QP_COMP,
  1358. 'blocklist'
  1359. );
  1360. $this->send(
  1361. $method,
  1362. $headers,
  1363. $queryParams,
  1364. $postParams,
  1365. $path,
  1366. $statusCode,
  1367. $body
  1368. );
  1369. }
  1370. /**
  1371. * Retrieves the list of blocks that have been uploaded as part of a block blob.
  1372. *
  1373. * There are two block lists maintained for a blob:
  1374. * 1) Committed Block List: The list of blocks that have been successfully
  1375. * committed to a given blob with commitBlobBlocks.
  1376. * 2) Uncommitted Block List: The list of blocks that have been uploaded for a
  1377. * blob using Put Block (REST API), but that have not yet been committed.
  1378. * These blocks are stored in Windows Azure in association with a blob, but do
  1379. * not yet form part of the blob.
  1380. *
  1381. * @param string $container name of the container
  1382. * @param string $blob name of the blob
  1383. * @param Models\ListBlobBlocksOptions $options optional parameters
  1384. *
  1385. * @return Models\ListBlobBlocksResult
  1386. *
  1387. * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179400.aspx
  1388. */
  1389. public function listBlobBlocks($container, $blob, $options = null)
  1390. {
  1391. Validate::isString($container, 'container');
  1392. Validate::isString($blob, 'blob');
  1393. Validate::notNullOrEmpty($blob, 'blob');
  1394. $method = Resources::HTTP_GET;
  1395. $headers = array();
  1396. $postParams = array();
  1397. $queryParams = array();
  1398. $path = $this->_createPath($container, $blob);
  1399. $statusCode = Resources::STATUS_OK;
  1400. if (is_null($options)) {
  1401. $options = new ListBlobBlocksOptions();
  1402. }
  1403. $this->addOptionalHeader(
  1404. $headers,
  1405. Resources::X_MS_LEASE_ID,
  1406. $options->getLeaseId()
  1407. );
  1408. $this->addOptionalQueryParam(
  1409. $queryParams,
  1410. Resources::QP_TIMEOUT,
  1411. $options->getTimeout()
  1412. );
  1413. $this->addOptionalQueryParam(
  1414. $queryParams,
  1415. Resources::QP_BLOCK_LIST_TYPE,
  1416. $options->getBlockListType()
  1417. );
  1418. $this->addOptionalQueryParam(
  1419. $queryParams,
  1420. Resources::QP_SNAPSHOT,
  1421. $options->getSnapshot()
  1422. );
  1423. $this->addOptionalQueryParam(
  1424. $queryParams,
  1425. Resources::QP_COMP,
  1426. 'blocklist'
  1427. );
  1428. $response = $this->send(
  1429. $method,
  1430. $headers,
  1431. $queryParams,
  1432. $postParams,
  1433. $path,
  1434. $statusCode
  1435. );
  1436. $parsed = $this->dataSerializer->unserialize($response->getBody());
  1437. return ListBlobBlocksResult::create($response->getHeader(), $parsed);
  1438. }
  1439. /**
  1440. * Returns all properties and metadata on the blob.
  1441. *
  1442. * @param string $container name of the container
  1443. * @param string $blob name of the blob
  1444. * @param Models\GetBlobPropertiesOptions $options optional parameters
  1445. *
  1446. * @return Models\GetBlobPropertiesResult
  1447. *
  1448. * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179394.aspx
  1449. */
  1450. public function getBlobProperties($container, $blob, $options = null)
  1451. {
  1452. Validate::isString($container, 'container');
  1453. Validate::isString($blob, 'blob');
  1454. Validate::notNullOrEmpty($blob, 'blob');
  1455. $method = Resources::HTTP_HEAD;
  1456. $headers = array();
  1457. $postParams = array();
  1458. $queryParams = array();
  1459. $path = $this->_createPath($container, $blob);
  1460. $statusCode = Resources::STATUS_OK;
  1461. if (is_null($options)) {
  1462. $options = new GetBlobPropertiesOptions();
  1463. }
  1464. $headers = $this->addOptionalAccessConditionHeader(
  1465. $headers, $options->getAccessCondition()
  1466. );
  1467. $this->addOptionalHeader(
  1468. $headers,
  1469. Resources::X_MS_LEASE_ID,
  1470. $options->getLeaseId()
  1471. );
  1472. $this->addOptionalQueryParam(
  1473. $queryParams,
  1474. Resources::QP_SNAPSHOT,
  1475. $options->getSnapshot()
  1476. );
  1477. $this->addOptionalQueryParam(
  1478. $queryParams,
  1479. Resources::QP_TIMEOUT,
  1480. $options->getTimeout()
  1481. );
  1482. $response = $this->send(
  1483. $method,
  1484. $headers,
  1485. $queryParams,
  1486. $postParams,
  1487. $path,
  1488. $statusCode
  1489. );
  1490. return $this->_getBlobPropertiesResultFromResponse($response->getHeader());
  1491. }
  1492. /**
  1493. * Returns all properties and metadata on the blob.
  1494. *
  1495. * @param string $container name of the container
  1496. * @param string $blob name of the blob
  1497. * @param Models\GetBlobMetadataOptions $options optional parameters
  1498. *
  1499. * @return Models\GetBlobMetadataResult
  1500. *
  1501. * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179350.aspx
  1502. */
  1503. public function getBlobMetadata($container, $blob, $options = null)
  1504. {
  1505. Validate::isString($container, 'container');
  1506. Validate::isString($blob, 'blob');
  1507. Validate::notNullOrEmpty($blob, 'blob');
  1508. $method = Resources::HTTP_HEAD;
  1509. $headers = array();
  1510. $postParams = array();
  1511. $queryParams = array();
  1512. $path = $this->_createPath($container, $blob);
  1513. $statusCode = Resources::STATUS_OK;
  1514. if (is_null($options)) {
  1515. $options = new GetBlobMetadataOptions();
  1516. }
  1517. $headers = $this->addOptionalAccessConditionHeader(
  1518. $headers, $options->getAccessCondition()
  1519. );
  1520. $this->addOptionalHeader(
  1521. $headers,
  1522. Resources::X_MS_LEASE_ID,
  1523. $options->getLeaseId()
  1524. );
  1525. $this->addOptionalQueryParam(
  1526. $queryParams,
  1527. Resources::QP_SNAPSHOT,
  1528. $options->getSnapshot()
  1529. );
  1530. $this->addOptionalQueryParam(
  1531. $queryParams,
  1532. Resources::QP_TIMEOUT,
  1533. $options->getTimeout()
  1534. );
  1535. $this->addOptionalQueryParam(
  1536. $queryParams,
  1537. Resources::QP_COMP,
  1538. 'metadata'
  1539. );
  1540. $response = $this->send(
  1541. $method,
  1542. $headers,
  1543. $queryParams,
  1544. $postParams,
  1545. $path,
  1546. $statusCode
  1547. );
  1548. $metadata = $this->getMetadataArray($response->getHeader());
  1549. return GetBlobMetadataResult::create($response->getHeader(), $metadata);
  1550. }
  1551. /**
  1552. * Returns a list of active page ranges for a page blob. Active page ranges are
  1553. * those that have been populated with data.
  1554. *
  1555. * @param string $container name of the container
  1556. * @param string $blob name of the blob
  1557. * @param Models\ListPageBlobRangesOptions $options optional parameters
  1558. *
  1559. * @return Models\ListPageBlobRangesResult
  1560. *
  1561. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee691973.aspx
  1562. */
  1563. public function listPageBlobRanges($container, $blob, $options = null)
  1564. {
  1565. Validate::isString($container, 'container');
  1566. Validate::isString($blob, 'blob');
  1567. Validate::notNullOrEmpty($blob, 'blob');
  1568. $method = Resources::HTTP_GET;
  1569. $headers = array();
  1570. $queryParams = array();
  1571. $postParams = array();
  1572. $path = $this->_createPath($container, $blob);
  1573. $statusCode = Resources::STATUS_OK;
  1574. if (is_null($options)) {
  1575. $options = new ListPageBlobRangesOptions();
  1576. }
  1577. $headers = $this->addOptionalAccessConditionHeader(
  1578. $headers, $options->getAccessCondition()
  1579. );
  1580. $headers = $this->_addOptionalRangeHeader(
  1581. $headers, $options->getRangeStart(), $options->getRangeEnd()
  1582. );
  1583. $this->addOptionalHeader(
  1584. $headers,
  1585. Resources::X_MS_LEASE_ID,
  1586. $options->getLeaseId()
  1587. );
  1588. $this->addOptionalQueryParam(
  1589. $queryParams,
  1590. Resources::QP_SNAPSHOT,
  1591. $options->getSnapshot()
  1592. );
  1593. $this->addOptionalQueryParam(
  1594. $queryParams,
  1595. Resources::QP_TIMEOUT,
  1596. $options->getTimeout()
  1597. );
  1598. $this->addOptionalQueryParam(
  1599. $queryParams,
  1600. Resources::QP_COMP,
  1601. 'pagelist'
  1602. );
  1603. $response = $this->send(
  1604. $method,
  1605. $headers,
  1606. $queryParams,
  1607. $postParams,
  1608. $path,
  1609. $statusCode
  1610. );
  1611. $parsed = $this->dataSerializer->unserialize($response->getBody());
  1612. return ListPageBlobRangesResult::create($response->getHeader(), $parsed);
  1613. }
  1614. /**
  1615. * Sets system properties defined for a blob.
  1616. *
  1617. * @param string $container name of the container
  1618. * @param string $blob name of the blob
  1619. * @param Models\SetBlobPropertiesOptions $options optional parameters
  1620. *
  1621. * @return Models\SetBlobPropertiesResult
  1622. *
  1623. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee691966.aspx
  1624. */
  1625. public function setBlobProperties($container, $blob, $options = null)
  1626. {
  1627. Validate::isString($container, 'container');
  1628. Validate::isString($blob, 'blob');
  1629. Validate::notNullOrEmpty($blob, 'blob');
  1630. $method = Resources::HTTP_PUT;
  1631. $headers = array();
  1632. $postParams = array();
  1633. $queryParams = array();
  1634. $path = $this->_createPath($container, $blob);
  1635. $statusCode = Resources::STATUS_OK;
  1636. if (is_null($options)) {
  1637. $options = new SetBlobPropertiesOptions();
  1638. }
  1639. $blobContentType = $options->getBlobContentType();
  1640. $blobContentEncoding = $options->getBlobContentEncoding();
  1641. $blobContentLanguage = $options->getBlobContentLanguage();
  1642. $blobContentLength = $options->getBlobContentLength();
  1643. $blobContentMD5 = $options->getBlobContentMD5();
  1644. $blobCacheControl = $options->getBlobCacheControl();
  1645. $leaseId = $options->getLeaseId();
  1646. $sNumberAction = $options->getSequenceNumberAction();
  1647. $sNumber = $options->getSequenceNumber();
  1648. $headers = $this->addOptionalAccessConditionHeader(
  1649. $headers, $options->getAccessCondition()
  1650. );
  1651. $this->addOptionalHeader(
  1652. $headers,
  1653. Resources::X_MS_LEASE_ID,
  1654. $leaseId
  1655. );
  1656. $this->addOptionalHeader(
  1657. $headers,
  1658. Resources::X_MS_BLOB_CACHE_CONTROL,
  1659. $blobCacheControl
  1660. );
  1661. $this->addOptionalHeader(
  1662. $headers,
  1663. Resources::X_MS_BLOB_CONTENT_TYPE,
  1664. $blobContentType
  1665. );
  1666. $this->addOptionalHeader(
  1667. $headers,
  1668. Resources::X_MS_BLOB_CONTENT_ENCODING,
  1669. $blobContentEncoding
  1670. );
  1671. $this->addOptionalHeader(
  1672. $headers,
  1673. Resources::X_MS_BLOB_CONTENT_LANGUAGE,
  1674. $blobContentLanguage
  1675. );
  1676. $this->addOptionalHeader(
  1677. $headers,
  1678. Resources::X_MS_BLOB_CONTENT_LENGTH,
  1679. $blobContentLength
  1680. );
  1681. $this->addOptionalHeader(
  1682. $headers,
  1683. Resources::X_MS_BLOB_CONTENT_MD5,
  1684. $blobContentMD5
  1685. );
  1686. $this->addOptionalHeader(
  1687. $headers,
  1688. Resources::X_MS_BLOB_SEQUENCE_NUMBER_ACTION,
  1689. $sNumberAction
  1690. );
  1691. $this->addOptionalHeader(
  1692. $headers,
  1693. Resources::X_MS_BLOB_SEQUENCE_NUMBER,
  1694. $sNumber
  1695. );
  1696. $this->addOptionalQueryParam($queryParams, Resources::QP_COMP, 'properties');
  1697. $this->addOptionalQueryParam(
  1698. $queryParams,
  1699. Resources::QP_TIMEOUT,
  1700. $options->getTimeout()
  1701. );
  1702. $response = $this->send(
  1703. $method,
  1704. $headers,
  1705. $queryParams,
  1706. $postParams,
  1707. $path,
  1708. $statusCode
  1709. );
  1710. return SetBlobPropertiesResult::create($response->getHeader());
  1711. }
  1712. /**
  1713. * Sets metadata headers on the blob.
  1714. *
  1715. * @param string $container name of the container
  1716. * @param string $blob name of the blob
  1717. * @param array $metadata key/value pair representation
  1718. * @param Models\SetBlobMetadataOptions $options optional parameters
  1719. *
  1720. * @return Models\SetBlobMetadataResult
  1721. *
  1722. * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179414.aspx
  1723. */
  1724. public function setBlobMetadata($container, $blob, $metadata, $options = null)
  1725. {
  1726. Validate::isString($container, 'container');
  1727. Validate::isString($blob, 'blob');
  1728. Validate::notNullOrEmpty($blob, 'blob');
  1729. $this->validateMetadata($metadata);
  1730. $method = Resources::HTTP_PUT;
  1731. $headers = array();
  1732. $postParams = array();
  1733. $queryParams = array();
  1734. $path = $this->_createPath($container, $blob);
  1735. $statusCode = Resources::STATUS_OK;
  1736. if (is_null($options)) {
  1737. $options = new SetBlobMetadataOptions();
  1738. }
  1739. $headers = $this->addOptionalAccessConditionHeader(
  1740. $headers, $options->getAccessCondition()
  1741. );
  1742. $headers = $this->addMetadataHeaders($headers, $metadata);
  1743. $this->addOptionalHeader(
  1744. $headers,
  1745. Resources::X_MS_LEASE_ID,
  1746. $options->getLeaseId()
  1747. );
  1748. $this->addOptionalQueryParam(
  1749. $queryParams,
  1750. Resources::QP_TIMEOUT,
  1751. $options->getTimeout()
  1752. );
  1753. $this->addOptionalQueryParam(
  1754. $queryParams,
  1755. Resources::QP_COMP,
  1756. 'metadata'
  1757. );
  1758. $response = $this->send(
  1759. $method,
  1760. $headers,
  1761. $queryParams,
  1762. $postParams,
  1763. $path,
  1764. $statusCode
  1765. );
  1766. return SetBlobMetadataResult::create($response->getHeader());
  1767. }
  1768. /**
  1769. * Reads or downloads a blob from the system, including its metadata and
  1770. * properties.
  1771. *
  1772. * @param string $container name of the container
  1773. * @param string $blob name of the blob
  1774. * @param Models\GetBlobOptions $options optional parameters
  1775. *
  1776. * @return Models\GetBlobResult
  1777. *
  1778. * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179440.aspx
  1779. */
  1780. public function getBlob($container, $blob, $options = null)
  1781. {
  1782. Validate::isString($container, 'container');
  1783. Validate::isString($blob, 'blob');
  1784. $method = Resources::HTTP_GET;
  1785. $headers = array();
  1786. $postParams = array();
  1787. $queryParams = array();
  1788. $path = $this->_createPath($container, $blob);
  1789. $statusCode = array(
  1790. Resources::STATUS_OK,
  1791. Resources::STATUS_PARTIAL_CONTENT
  1792. );
  1793. if (is_null($options)) {
  1794. $options = new GetBlobOptions();
  1795. }
  1796. $getMD5 = $options->getComputeRangeMD5();
  1797. $headers = $this->addOptionalAccessConditionHeader(
  1798. $headers, $options->getAccessCondition()
  1799. );
  1800. $headers = $this->_addOptionalRangeHeader(
  1801. $headers, $options->getRangeStart(), $options->getRangeEnd()
  1802. );
  1803. $this->addOptionalHeader(
  1804. $headers,
  1805. Resources::X_MS_LEASE_ID,
  1806. $options->getLeaseId()
  1807. );
  1808. $this->addOptionalHeader(
  1809. $headers,
  1810. Resources::X_MS_RANGE_GET_CONTENT_MD5,
  1811. $getMD5 ? 'true' : null
  1812. );
  1813. $this->addOptionalQueryParam(
  1814. $queryParams,
  1815. Resources::QP_TIMEOUT,
  1816. $options->getTimeout()
  1817. );
  1818. $this->addOptionalQueryParam(
  1819. $queryParams,
  1820. Resources::QP_SNAPSHOT,
  1821. $options->getSnapshot()
  1822. );
  1823. $response = $this->send(
  1824. $method,
  1825. $headers,
  1826. $queryParams,
  1827. $postParams,
  1828. $path,
  1829. $statusCode
  1830. );
  1831. $metadata = $this->getMetadataArray($response->getHeader());
  1832. return GetBlobResult::create(
  1833. $response->getHeader(),
  1834. $response->getBody(),
  1835. $metadata
  1836. );
  1837. }
  1838. /**
  1839. * Deletes a blob or blob snapshot.
  1840. *
  1841. * Note that if the snapshot entry is specified in the $options then only this
  1842. * blob snapshot is deleted. To delete all blob snapshots, do not set Snapshot
  1843. * and just set getDeleteSnaphotsOnly to true.
  1844. *
  1845. * @param string $container name of the container
  1846. * @param string $blob name of the blob
  1847. * @param Models\DeleteBlobOptions $options optional parameters
  1848. *
  1849. * @return none
  1850. *
  1851. * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179413.aspx
  1852. */
  1853. public function deleteBlob($container, $blob, $options = null)
  1854. {
  1855. Validate::isString($container, 'container');
  1856. Validate::isString($blob, 'blob');
  1857. Validate::notNullOrEmpty($blob, 'blob');
  1858. $method = Resources::HTTP_DELETE;
  1859. $headers = array();
  1860. $postParams = array();
  1861. $queryParams = array();
  1862. $path = $this->_createPath($container, $blob);
  1863. $statusCode = Resources::STATUS_ACCEPTED;
  1864. if (is_null($options)) {
  1865. $options = new DeleteBlobOptions();
  1866. }
  1867. if (is_null($options->getSnapshot())) {
  1868. $delSnapshots = $options->getDeleteSnaphotsOnly() ? 'only' : 'include';
  1869. $this->addOptionalHeader(
  1870. $headers,
  1871. Resources::X_MS_DELETE_SNAPSHOTS,
  1872. $delSnapshots
  1873. );
  1874. } else {
  1875. $this->addOptionalQueryParam(
  1876. $queryParams,
  1877. Resources::QP_SNAPSHOT,
  1878. $options->getSnapshot()
  1879. );
  1880. }
  1881. $headers = $this->addOptionalAccessConditionHeader(
  1882. $headers, $options->getAccessCondition()
  1883. );
  1884. $this->addOptionalHeader(
  1885. $headers,
  1886. Resources::X_MS_LEASE_ID,
  1887. $options->getLeaseId()
  1888. );
  1889. $this->addOptionalQueryParam(
  1890. $queryParams,
  1891. Resources::QP_TIMEOUT,
  1892. $options->getTimeout()
  1893. );
  1894. $this->send(
  1895. $method,
  1896. $headers,
  1897. $queryParams,
  1898. $postParams,
  1899. $path,
  1900. $statusCode
  1901. );
  1902. }
  1903. /**
  1904. * Creates a snapshot of a blob.
  1905. *
  1906. * @param string $container The name of the container.
  1907. * @param string $blob The name of the blob.
  1908. * @param Models\CreateBlobSnapshotOptions $options The optional parameters.
  1909. *
  1910. * @return Models\CreateBlobSnapshotResult
  1911. *
  1912. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee691971.aspx
  1913. */
  1914. public function createBlobSnapshot($container, $blob, $options = null)
  1915. {
  1916. Validate::isString($container, 'container');
  1917. Validate::isString($blob, 'blob');
  1918. Validate::notNullOrEmpty($blob, 'blob');
  1919. $method = Resources::HTTP_PUT;
  1920. $headers = array();
  1921. $postParams = array();
  1922. $queryParams = array();
  1923. $path = $this->_createPath($container, $blob);
  1924. $expectedStatusCode = Resources::STATUS_CREATED;
  1925. if (is_null($options)) {
  1926. $options = new CreateBlobSnapshotOptions();
  1927. }
  1928. $queryParams[Resources::QP_COMP] = 'snapshot';
  1929. $this->addOptionalQueryParam(
  1930. $queryParams,
  1931. Resources::QP_TIMEOUT,
  1932. $options->getTimeout()
  1933. );
  1934. $headers = $this->addOptionalAccessConditionHeader(
  1935. $headers,
  1936. $options->getAccessCondition()
  1937. );
  1938. $headers = $this->addMetadataHeaders($headers, $options->getMetadata());
  1939. $this->addOptionalHeader(
  1940. $headers,
  1941. Resources::X_MS_LEASE_ID,
  1942. $options->getLeaseId()
  1943. );
  1944. $response = $this->send(
  1945. $method,
  1946. $headers,
  1947. $queryParams,
  1948. $postParams,
  1949. $path,
  1950. $expectedStatusCode
  1951. );
  1952. return CreateBlobSnapshotResult::create($response->getHeader());
  1953. }
  1954. /**
  1955. * Copies a source blob to a destination blob within the same storage account.
  1956. *
  1957. * @param string $destinationContainer name of the destination
  1958. * container
  1959. * @param string $destinationBlob name of the destination
  1960. * blob
  1961. * @param string $sourceContainer name of the source
  1962. * container
  1963. * @param string $sourceBlob name of the source
  1964. * blob
  1965. * @param Models\CopyBlobOptions $options optional parameters
  1966. *
  1967. * @return CopyBlobResult
  1968. *
  1969. * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd894037.aspx
  1970. */
  1971. public function copyBlob(
  1972. $destinationContainer,
  1973. $destinationBlob,
  1974. $sourceContainer,
  1975. $sourceBlob,
  1976. $options = null
  1977. ) {
  1978. $method = Resources::HTTP_PUT;
  1979. $headers = array();
  1980. $postParams = array();
  1981. $queryParams = array();
  1982. $destinationBlobPath = $destinationContainer . '/' . $destinationBlob;
  1983. $statusCode = Resources::STATUS_CREATED;
  1984. if (is_null($options)) {
  1985. $options = new CopyBlobOptions();
  1986. }
  1987. $this->addOptionalQueryParam(
  1988. $queryParams,
  1989. Resources::QP_TIMEOUT,
  1990. $options->getTimeout()
  1991. );
  1992. $sourceBlobPath = $this->_getCopyBlobSourceName(
  1993. $sourceContainer,
  1994. $sourceBlob,
  1995. $options
  1996. );
  1997. $headers = $this->addOptionalAccessConditionHeader(
  1998. $headers,
  1999. $options->getAccessCondition()
  2000. );
  2001. $headers = $this->addOptionalSourceAccessConditionHeader(
  2002. $headers,
  2003. $options->getSourceAccessCondition()
  2004. );
  2005. $this->addOptionalHeader(
  2006. $headers,
  2007. Resources::X_MS_COPY_SOURCE,
  2008. $sourceBlobPath
  2009. );
  2010. $headers = $this->addMetadataHeaders($headers, $options->getMetadata());
  2011. $this->addOptionalHeader(
  2012. $headers,
  2013. Resources::X_MS_LEASE_ID,
  2014. $options->getLeaseId()
  2015. );
  2016. $this->addOptionalHeader(
  2017. $headers,
  2018. Resources::X_MS_SOURCE_LEASE_ID,
  2019. $options->getSourceLeaseId()
  2020. );
  2021. $response = $this->send(
  2022. $method,
  2023. $headers,
  2024. $queryParams,
  2025. $postParams,
  2026. $destinationBlobPath,
  2027. $statusCode
  2028. );
  2029. return CopyBlobResult::create($response->getHeader());
  2030. }
  2031. /**
  2032. * Establishes an exclusive one-minute write lock on a blob. To write to a locked
  2033. * blob, a client must provide a lease ID.
  2034. *
  2035. * @param string $container name of the container
  2036. * @param string $blob name of the blob
  2037. * @param Models\AcquireLeaseOptions $options optional parameters
  2038. *
  2039. * @return Models\AcquireLeaseResult
  2040. *
  2041. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee691972.aspx
  2042. */
  2043. public function acquireLease($container, $blob, $options = null)
  2044. {
  2045. return $this->_putLeaseImpl(
  2046. LeaseMode::ACQUIRE_ACTION,
  2047. $container,
  2048. $blob,
  2049. null /* leaseId */,
  2050. is_null($options) ? new AcquireLeaseOptions() : $options,
  2051. is_null($options) ? null : $options->getAccessCondition()
  2052. );
  2053. }
  2054. /**
  2055. * Renews an existing lease
  2056. *
  2057. * @param string $container name of the container
  2058. * @param string $blob name of the blob
  2059. * @param string $leaseId lease id when acquiring
  2060. * @param Models\BlobServiceOptions $options optional parameters
  2061. *
  2062. * @return Models\AcquireLeaseResult
  2063. *
  2064. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee691972.aspx
  2065. */
  2066. public function renewLease($container, $blob, $leaseId, $options = null)
  2067. {
  2068. return $this->_putLeaseImpl(
  2069. LeaseMode::RENEW_ACTION,
  2070. $container,
  2071. $blob,
  2072. $leaseId,
  2073. is_null($options) ? new BlobServiceOptions() : $options
  2074. );
  2075. }
  2076. /**
  2077. * Frees the lease if it is no longer needed so that another client may
  2078. * immediately acquire a lease against the blob.
  2079. *
  2080. * @param string $container name of the container
  2081. * @param string $blob name of the blob
  2082. * @param string $leaseId lease id when acquiring
  2083. * @param Models\BlobServiceOptions $options optional parameters
  2084. *
  2085. * @return none
  2086. *
  2087. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee691972.aspx
  2088. */
  2089. public function releaseLease($container, $blob, $leaseId, $options = null)
  2090. {
  2091. $this->_putLeaseImpl(
  2092. LeaseMode::RELEASE_ACTION,
  2093. $container,
  2094. $blob,
  2095. $leaseId,
  2096. is_null($options) ? new BlobServiceOptions() : $options
  2097. );
  2098. }
  2099. /**
  2100. * Ends the lease but ensure that another client cannot acquire a new lease until
  2101. * the current lease period has expired.
  2102. *
  2103. * @param string $container name of the container
  2104. * @param string $blob name of the blob
  2105. * @param string $leaseId lease id when acquiring
  2106. * @param Models\BlobServiceOptions $options optional parameters
  2107. *
  2108. * @return none
  2109. *
  2110. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee691972.aspx
  2111. */
  2112. public function breakLease($container, $blob, $leaseId, $options = null)
  2113. {
  2114. $this->_putLeaseImpl(
  2115. LeaseMode::BREAK_ACTION,
  2116. $container,
  2117. $blob,
  2118. $leaseId,
  2119. is_null($options) ? new BlobServiceOptions() : $options
  2120. );
  2121. }
  2122. }