PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/aws/aws-sdk-php/src/Aws/S3/Model/MultipartUpload/UploadBuilder.php

https://github.com/lslucas/105fm
PHP | 295 lines | 119 code | 40 blank | 136 comment | 11 complexity | 33c711012cc52731819989d44b65ed1e MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Aws\S3\Model\MultipartUpload;
  17. use Aws\Common\Enum\UaString as Ua;
  18. use Aws\Common\Exception\InvalidArgumentException;
  19. use Aws\Common\Model\MultipartUpload\AbstractUploadBuilder;
  20. use Aws\S3\Model\Acp;
  21. /**
  22. * Easily create a multipart uploader used to quickly and reliably upload a
  23. * large file or data stream to Amazon S3 using multipart uploads
  24. */
  25. class UploadBuilder extends AbstractUploadBuilder
  26. {
  27. /**
  28. * @var int Concurrency level to transfer the parts
  29. */
  30. protected $concurrency = 1;
  31. /**
  32. * @var int Minimum part size to upload
  33. */
  34. protected $minPartSize = AbstractTransfer::MIN_PART_SIZE;
  35. /**
  36. * @var string MD5 hash of the entire body to transfer
  37. */
  38. protected $md5;
  39. /**
  40. * @var bool Whether or not to calculate the entire MD5 hash of the object
  41. */
  42. protected $calculateEntireMd5 = false;
  43. /**
  44. * @var bool Whether or not to calculate MD5 hash of each part
  45. */
  46. protected $calculatePartMd5 = true;
  47. /**
  48. * @var array Array of initiate command options
  49. */
  50. protected $commandOptions = array();
  51. /**
  52. * @var array Array of transfer options
  53. */
  54. protected $transferOptions = array();
  55. /**
  56. * Set the bucket to upload the object to
  57. *
  58. * @param string $bucket Name of the bucket
  59. *
  60. * @return self
  61. */
  62. public function setBucket($bucket)
  63. {
  64. return $this->setOption('Bucket', $bucket);
  65. }
  66. /**
  67. * Set the key of the object
  68. *
  69. * @param string $key Key of the object to upload
  70. *
  71. * @return self
  72. */
  73. public function setKey($key)
  74. {
  75. return $this->setOption('Key', $key);
  76. }
  77. /**
  78. * Set the minimum acceptable part size
  79. *
  80. * @param int $minSize Minimum acceptable part size in bytes
  81. *
  82. * @return self
  83. */
  84. public function setMinPartSize($minSize)
  85. {
  86. $this->minPartSize = (int) max((int) $minSize, AbstractTransfer::MIN_PART_SIZE);
  87. return $this;
  88. }
  89. /**
  90. * Set the concurrency level to use when uploading parts. This affects how
  91. * many parts are uploaded in parallel. You must use a local file as your
  92. * data source when using a concurrency greater than 1
  93. *
  94. * @param int $concurrency Concurrency level
  95. *
  96. * @return self
  97. */
  98. public function setConcurrency($concurrency)
  99. {
  100. $this->concurrency = $concurrency;
  101. return $this;
  102. }
  103. /**
  104. * Explicitly set the MD5 hash of the entire body
  105. *
  106. * @param string $md5 MD5 hash of the entire body
  107. *
  108. * @return self
  109. */
  110. public function setMd5($md5)
  111. {
  112. $this->md5 = $md5;
  113. return $this;
  114. }
  115. /**
  116. * Set to true to have the builder calculate the MD5 hash of the entire data
  117. * source before initiating a multipart upload (this could be an expensive
  118. * operation). This setting can ony be used with seekable data sources.
  119. *
  120. * @param bool $calculateMd5 Set to true to calculate the MD5 hash of the body
  121. *
  122. * @return self
  123. */
  124. public function calculateMd5($calculateMd5)
  125. {
  126. $this->calculateEntireMd5 = (bool) $calculateMd5;
  127. return $this;
  128. }
  129. /**
  130. * Specify whether or not to calculate the MD5 hash of each uploaded part.
  131. * This setting defaults to true.
  132. *
  133. * @param bool $usePartMd5 Set to true to calculate the MD5 has of each part
  134. *
  135. * @return self
  136. */
  137. public function calculatePartMd5($usePartMd5)
  138. {
  139. $this->calculatePartMd5 = (bool) $usePartMd5;
  140. return $this;
  141. }
  142. /**
  143. * Set the ACP to use on the object
  144. *
  145. * @param Acp $acp ACP to set on the object
  146. *
  147. * @return self
  148. */
  149. public function setAcp(Acp $acp)
  150. {
  151. return $this->setOption('ACP', $acp);
  152. }
  153. /**
  154. * Set an option to pass to the initial CreateMultipartUpload operation
  155. *
  156. * @param string $name Option name
  157. * @param string $value Option value
  158. *
  159. * @return self
  160. */
  161. public function setOption($name, $value)
  162. {
  163. $this->commandOptions[$name] = $value;
  164. return $this;
  165. }
  166. /**
  167. * Add an array of options to pass to the initial CreateMultipartUpload operation
  168. *
  169. * @param array $options Array of CreateMultipartUpload operation parameters
  170. *
  171. * @return self
  172. */
  173. public function addOptions(array $options)
  174. {
  175. $this->commandOptions = array_replace($this->commandOptions, $options);
  176. return $this;
  177. }
  178. /**
  179. * Set an array of transfer options to apply to the upload transfer object
  180. *
  181. * @param array $options Transfer options
  182. *
  183. * @return self
  184. */
  185. public function setTransferOptions(array $options)
  186. {
  187. $this->transferOptions = $options;
  188. return $this;
  189. }
  190. /**
  191. * {@inheritdoc}
  192. * @throws InvalidArgumentException when attempting to resume a transfer using a non-seekable stream
  193. * @throws InvalidArgumentException when missing required properties (bucket, key, client, source)
  194. */
  195. public function build()
  196. {
  197. if ($this->state instanceof TransferState) {
  198. $this->commandOptions = array_replace($this->commandOptions, $this->state->getUploadId()->toParams());
  199. }
  200. if (!isset($this->commandOptions['Bucket']) || !isset($this->commandOptions['Key'])
  201. || !$this->client || !$this->source
  202. ) {
  203. throw new InvalidArgumentException('You must specify a Bucket, Key, client, and source.');
  204. }
  205. if ($this->state && !$this->source->isSeekable()) {
  206. throw new InvalidArgumentException('You cannot resume a transfer using a non-seekable source.');
  207. }
  208. // If no state was set, then create one by initiating or loading a multipart upload
  209. if (is_string($this->state)) {
  210. $this->state = TransferState::fromUploadId($this->client, UploadId::fromParams(array(
  211. 'Bucket' => $this->commandOptions['Bucket'],
  212. 'Key' => $this->commandOptions['Key'],
  213. 'UploadId' => $this->state
  214. )));
  215. } elseif (!$this->state) {
  216. $this->state = $this->initiateMultipartUpload();
  217. }
  218. $options = array_replace(array(
  219. 'min_part_size' => $this->minPartSize,
  220. 'part_md5' => (bool) $this->calculatePartMd5,
  221. 'concurrency' => $this->concurrency
  222. ), $this->transferOptions);
  223. return $this->concurrency > 1
  224. ? new ParallelTransfer($this->client, $this->state, $this->source, $options)
  225. : new SerialTransfer($this->client, $this->state, $this->source, $options);
  226. }
  227. /**
  228. * {@inheritdoc}
  229. */
  230. protected function initiateMultipartUpload()
  231. {
  232. // Determine Content-Type
  233. if ($mimeType = $this->source->getContentType()) {
  234. $this->commandOptions['ContentType'] = $mimeType;
  235. }
  236. $params = array_replace(array(
  237. Ua::OPTION => Ua::MULTIPART_UPLOAD,
  238. 'command.headers' => $this->headers,
  239. 'Metadata' => array()
  240. ), $this->commandOptions);
  241. // Calculate the MD5 hash if none was set and it is asked of the builder
  242. if ($this->calculateEntireMd5) {
  243. $this->md5 = $this->source->getContentMd5();
  244. }
  245. // If an MD5 is specified, then add it to the custom headers of the request
  246. // so that it will be returned when downloading the object from Amazon S3
  247. if ($this->md5) {
  248. $params['Metadata']['x-amz-Content-MD5'] = $this->md5;
  249. }
  250. $result = $this->client->getCommand('CreateMultipartUpload', $params)->execute();
  251. // Create a new state based on the initiated upload
  252. $params['UploadId'] = $result['UploadId'];
  253. return new TransferState(UploadId::fromParams($params));
  254. }
  255. }