GlacierClient.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace Aws\Glacier;
  3. use Aws\Api\ApiProvider;
  4. use Aws\Api\DocModel;
  5. use Aws\Api\Service;
  6. use Aws\AwsClient;
  7. use Aws\CommandInterface;
  8. use Aws\Exception\CouldNotCreateChecksumException;
  9. use Aws\HashingStream;
  10. use Aws\Middleware;
  11. use Aws\PhpHash;
  12. use Psr\Http\Message\RequestInterface;
  13. /**
  14. * This client is used to interact with the **Amazon Glacier** service.
  15. *
  16. * @method \Aws\Result abortMultipartUpload(array $args = [])
  17. * @method \GuzzleHttp\Promise\Promise abortMultipartUploadAsync(array $args = [])
  18. * @method \Aws\Result abortVaultLock(array $args = [])
  19. * @method \GuzzleHttp\Promise\Promise abortVaultLockAsync(array $args = [])
  20. * @method \Aws\Result addTagsToVault(array $args = [])
  21. * @method \GuzzleHttp\Promise\Promise addTagsToVaultAsync(array $args = [])
  22. * @method \Aws\Result completeMultipartUpload(array $args = [])
  23. * @method \GuzzleHttp\Promise\Promise completeMultipartUploadAsync(array $args = [])
  24. * @method \Aws\Result completeVaultLock(array $args = [])
  25. * @method \GuzzleHttp\Promise\Promise completeVaultLockAsync(array $args = [])
  26. * @method \Aws\Result createVault(array $args = [])
  27. * @method \GuzzleHttp\Promise\Promise createVaultAsync(array $args = [])
  28. * @method \Aws\Result deleteArchive(array $args = [])
  29. * @method \GuzzleHttp\Promise\Promise deleteArchiveAsync(array $args = [])
  30. * @method \Aws\Result deleteVault(array $args = [])
  31. * @method \GuzzleHttp\Promise\Promise deleteVaultAsync(array $args = [])
  32. * @method \Aws\Result deleteVaultAccessPolicy(array $args = [])
  33. * @method \GuzzleHttp\Promise\Promise deleteVaultAccessPolicyAsync(array $args = [])
  34. * @method \Aws\Result deleteVaultNotifications(array $args = [])
  35. * @method \GuzzleHttp\Promise\Promise deleteVaultNotificationsAsync(array $args = [])
  36. * @method \Aws\Result describeJob(array $args = [])
  37. * @method \GuzzleHttp\Promise\Promise describeJobAsync(array $args = [])
  38. * @method \Aws\Result describeVault(array $args = [])
  39. * @method \GuzzleHttp\Promise\Promise describeVaultAsync(array $args = [])
  40. * @method \Aws\Result getDataRetrievalPolicy(array $args = [])
  41. * @method \GuzzleHttp\Promise\Promise getDataRetrievalPolicyAsync(array $args = [])
  42. * @method \Aws\Result getJobOutput(array $args = [])
  43. * @method \GuzzleHttp\Promise\Promise getJobOutputAsync(array $args = [])
  44. * @method \Aws\Result getVaultAccessPolicy(array $args = [])
  45. * @method \GuzzleHttp\Promise\Promise getVaultAccessPolicyAsync(array $args = [])
  46. * @method \Aws\Result getVaultLock(array $args = [])
  47. * @method \GuzzleHttp\Promise\Promise getVaultLockAsync(array $args = [])
  48. * @method \Aws\Result getVaultNotifications(array $args = [])
  49. * @method \GuzzleHttp\Promise\Promise getVaultNotificationsAsync(array $args = [])
  50. * @method \Aws\Result initiateJob(array $args = [])
  51. * @method \GuzzleHttp\Promise\Promise initiateJobAsync(array $args = [])
  52. * @method \Aws\Result initiateMultipartUpload(array $args = [])
  53. * @method \GuzzleHttp\Promise\Promise initiateMultipartUploadAsync(array $args = [])
  54. * @method \Aws\Result initiateVaultLock(array $args = [])
  55. * @method \GuzzleHttp\Promise\Promise initiateVaultLockAsync(array $args = [])
  56. * @method \Aws\Result listJobs(array $args = [])
  57. * @method \GuzzleHttp\Promise\Promise listJobsAsync(array $args = [])
  58. * @method \Aws\Result listMultipartUploads(array $args = [])
  59. * @method \GuzzleHttp\Promise\Promise listMultipartUploadsAsync(array $args = [])
  60. * @method \Aws\Result listParts(array $args = [])
  61. * @method \GuzzleHttp\Promise\Promise listPartsAsync(array $args = [])
  62. * @method \Aws\Result listProvisionedCapacity(array $args = [])
  63. * @method \GuzzleHttp\Promise\Promise listProvisionedCapacityAsync(array $args = [])
  64. * @method \Aws\Result listTagsForVault(array $args = [])
  65. * @method \GuzzleHttp\Promise\Promise listTagsForVaultAsync(array $args = [])
  66. * @method \Aws\Result listVaults(array $args = [])
  67. * @method \GuzzleHttp\Promise\Promise listVaultsAsync(array $args = [])
  68. * @method \Aws\Result purchaseProvisionedCapacity(array $args = [])
  69. * @method \GuzzleHttp\Promise\Promise purchaseProvisionedCapacityAsync(array $args = [])
  70. * @method \Aws\Result removeTagsFromVault(array $args = [])
  71. * @method \GuzzleHttp\Promise\Promise removeTagsFromVaultAsync(array $args = [])
  72. * @method \Aws\Result setDataRetrievalPolicy(array $args = [])
  73. * @method \GuzzleHttp\Promise\Promise setDataRetrievalPolicyAsync(array $args = [])
  74. * @method \Aws\Result setVaultAccessPolicy(array $args = [])
  75. * @method \GuzzleHttp\Promise\Promise setVaultAccessPolicyAsync(array $args = [])
  76. * @method \Aws\Result setVaultNotifications(array $args = [])
  77. * @method \GuzzleHttp\Promise\Promise setVaultNotificationsAsync(array $args = [])
  78. * @method \Aws\Result uploadArchive(array $args = [])
  79. * @method \GuzzleHttp\Promise\Promise uploadArchiveAsync(array $args = [])
  80. * @method \Aws\Result uploadMultipartPart(array $args = [])
  81. * @method \GuzzleHttp\Promise\Promise uploadMultipartPartAsync(array $args = [])
  82. */
  83. class GlacierClient extends AwsClient
  84. {
  85. public function __construct(array $args)
  86. {
  87. parent::__construct($args);
  88. // Setup middleware.
  89. $stack = $this->getHandlerList();
  90. $stack->appendBuild($this->getApiVersionMiddleware(), 'glacier.api_version');
  91. $stack->appendBuild($this->getChecksumsMiddleware(), 'glacier.checksum');
  92. $stack->appendBuild(
  93. Middleware::contentType(['UploadArchive', 'UploadPart']),
  94. 'glacier.content_type'
  95. );
  96. $stack->appendInit(
  97. Middleware::sourceFile($this->getApi(), 'body', 'sourceFile'),
  98. 'glacier.source_file'
  99. );
  100. }
  101. /**
  102. * {@inheritdoc}
  103. *
  104. * Sets the default accountId to "-" for all operations.
  105. */
  106. public function getCommand($name, array $args = [])
  107. {
  108. return parent::getCommand($name, $args + ['accountId' => '-']);
  109. }
  110. /**
  111. * Creates a middleware that updates a command with the content and tree
  112. * hash headers for upload operations.
  113. *
  114. * @return callable
  115. * @throws CouldNotCreateChecksumException if the body is not seekable.
  116. */
  117. private function getChecksumsMiddleware()
  118. {
  119. return function (callable $handler) {
  120. return function (
  121. CommandInterface $command,
  122. RequestInterface $request = null
  123. ) use ($handler) {
  124. // Accept "ContentSHA256" with a lowercase "c" to match other Glacier params.
  125. if (!$command['ContentSHA256'] && $command['contentSHA256']) {
  126. $command['ContentSHA256'] = $command['contentSHA256'];
  127. unset($command['contentSHA256']);
  128. }
  129. // If uploading, then make sure checksums are added.
  130. $name = $command->getName();
  131. if (($name === 'UploadArchive' || $name === 'UploadMultipartPart')
  132. && (!$command['checksum'] || !$command['ContentSHA256'])
  133. ) {
  134. $body = $request->getBody();
  135. if (!$body->isSeekable()) {
  136. throw new CouldNotCreateChecksumException('sha256');
  137. }
  138. // Add a tree hash if not provided.
  139. if (!$command['checksum']) {
  140. $body = new HashingStream(
  141. $body, new TreeHash(),
  142. function ($result) use (&$request) {
  143. $request = $request->withHeader(
  144. 'x-amz-sha256-tree-hash',
  145. bin2hex($result)
  146. );
  147. }
  148. );
  149. }
  150. // Add a linear content hash if not provided.
  151. if (!$command['ContentSHA256']) {
  152. $body = new HashingStream(
  153. $body, new PhpHash('sha256'),
  154. function ($result) use ($command) {
  155. $command['ContentSHA256'] = bin2hex($result);
  156. }
  157. );
  158. }
  159. // Read the stream in order to calculate the hashes.
  160. while (!$body->eof()) {
  161. $body->read(1048576);
  162. }
  163. $body->seek(0);
  164. }
  165. // Set the content hash header if a value is in the command.
  166. if ($command['ContentSHA256']) {
  167. $request = $request->withHeader(
  168. 'x-amz-content-sha256',
  169. $command['ContentSHA256']
  170. );
  171. }
  172. return $handler($command, $request);
  173. };
  174. };
  175. }
  176. /**
  177. * Creates a middleware that adds the API version header for all requests.
  178. *
  179. * @return callable
  180. */
  181. private function getApiVersionMiddleware()
  182. {
  183. return function (callable $handler) {
  184. return function (
  185. CommandInterface $command,
  186. RequestInterface $request = null
  187. ) use ($handler) {
  188. return $handler($command, $request->withHeader(
  189. 'x-amz-glacier-version',
  190. $this->getApi()->getMetadata('apiVersion')
  191. ));
  192. };
  193. };
  194. }
  195. /**
  196. * @internal
  197. * @codeCoverageIgnore
  198. */
  199. public static function applyDocFilters(array $api, array $docs)
  200. {
  201. // Add the SourceFile parameter.
  202. $docs['shapes']['SourceFile']['base'] = 'The path to a file on disk to use instead of the body parameter.';
  203. $api['shapes']['SourceFile'] = ['type' => 'string'];
  204. $api['shapes']['UploadArchiveInput']['members']['sourceFile'] = ['shape' => 'SourceFile'];
  205. $api['shapes']['UploadMultipartPartInput']['members']['sourceFile'] = ['shape' => 'SourceFile'];
  206. // Add the ContentSHA256 parameter.
  207. $docs['shapes']['ContentSHA256']['base'] = 'A SHA256 hash of the content of the request body';
  208. $api['shapes']['ContentSHA256'] = ['type' => 'string'];
  209. $api['shapes']['UploadArchiveInput']['members']['contentSHA256'] = ['shape' => 'ContentSHA256'];
  210. $api['shapes']['UploadMultipartPartInput']['members']['contentSHA256'] = ['shape' => 'ContentSHA256'];
  211. // Add information about "checksum" and "ContentSHA256" being optional.
  212. $optional = '<div class="alert alert-info">The SDK will compute this value '
  213. . 'for you on your behalf if it is not supplied.</div>';
  214. $docs['shapes']['checksum']['append'] = $optional;
  215. $docs['shapes']['ContentSHA256']['append'] = $optional;
  216. // Make "accountId" optional for all operations.
  217. foreach ($api['operations'] as $operation) {
  218. $inputShape =& $api['shapes'][$operation['input']['shape']];
  219. $accountIdIndex = array_search('accountId', $inputShape['required']);
  220. unset($inputShape['required'][$accountIdIndex]);
  221. }
  222. // Add information about the default value for "accountId".
  223. $optional = '<div class="alert alert-info">The SDK will set this value to "-" by default.</div>';
  224. foreach ($docs['shapes']['string']['refs'] as $name => &$ref) {
  225. if (strpos($name, 'accountId')) {
  226. $ref .= $optional;
  227. }
  228. }
  229. return [
  230. new Service($api, ApiProvider::defaultProvider()),
  231. new DocModel($docs)
  232. ];
  233. }
  234. }