MultipartUploadingTrait.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Aws\S3;
  3. use Aws\CommandInterface;
  4. use Aws\Multipart\UploadState;
  5. use Aws\ResultInterface;
  6. trait MultipartUploadingTrait
  7. {
  8. /**
  9. * Creates an UploadState object for a multipart upload by querying the
  10. * service for the specified upload's information.
  11. *
  12. * @param S3ClientInterface $client S3Client used for the upload.
  13. * @param string $bucket Bucket for the multipart upload.
  14. * @param string $key Object key for the multipart upload.
  15. * @param string $uploadId Upload ID for the multipart upload.
  16. *
  17. * @return UploadState
  18. */
  19. public static function getStateFromService(
  20. S3ClientInterface $client,
  21. $bucket,
  22. $key,
  23. $uploadId
  24. ) {
  25. $state = new UploadState([
  26. 'Bucket' => $bucket,
  27. 'Key' => $key,
  28. 'UploadId' => $uploadId,
  29. ]);
  30. foreach ($client->getPaginator('ListParts', $state->getId()) as $result) {
  31. // Get the part size from the first part in the first result.
  32. if (!$state->getPartSize()) {
  33. $state->setPartSize($result->search('Parts[0].Size'));
  34. }
  35. // Mark all the parts returned by ListParts as uploaded.
  36. foreach ($result['Parts'] as $part) {
  37. $state->markPartAsUploaded($part['PartNumber'], [
  38. 'PartNumber' => $part['PartNumber'],
  39. 'ETag' => $part['ETag']
  40. ]);
  41. }
  42. }
  43. $state->setStatus(UploadState::INITIATED);
  44. return $state;
  45. }
  46. protected function handleResult(CommandInterface $command, ResultInterface $result)
  47. {
  48. $this->getState()->markPartAsUploaded($command['PartNumber'], [
  49. 'PartNumber' => $command['PartNumber'],
  50. 'ETag' => $this->extractETag($result),
  51. ]);
  52. }
  53. abstract protected function extractETag(ResultInterface $result);
  54. protected function getCompleteParams()
  55. {
  56. $config = $this->getConfig();
  57. $params = isset($config['params']) ? $config['params'] : [];
  58. $params['MultipartUpload'] = [
  59. 'Parts' => $this->getState()->getUploadedParts()
  60. ];
  61. return $params;
  62. }
  63. protected function determinePartSize()
  64. {
  65. // Make sure the part size is set.
  66. $partSize = $this->getConfig()['part_size'] ?: MultipartUploader::PART_MIN_SIZE;
  67. // Adjust the part size to be larger for known, x-large uploads.
  68. if ($sourceSize = $this->getSourceSize()) {
  69. $partSize = (int) max(
  70. $partSize,
  71. ceil($sourceSize / MultipartUploader::PART_MAX_NUM)
  72. );
  73. }
  74. // Ensure that the part size follows the rules: 5 MB <= size <= 5 GB.
  75. if ($partSize < MultipartUploader::PART_MIN_SIZE || $partSize > MultipartUploader::PART_MAX_SIZE) {
  76. throw new \InvalidArgumentException('The part size must be no less '
  77. . 'than 5 MB and no greater than 5 GB.');
  78. }
  79. return $partSize;
  80. }
  81. protected function getInitiateParams()
  82. {
  83. $config = $this->getConfig();
  84. $params = isset($config['params']) ? $config['params'] : [];
  85. if (isset($config['acl'])) {
  86. $params['ACL'] = $config['acl'];
  87. }
  88. // Set the ContentType if not already present
  89. if (empty($params['ContentType']) && $type = $this->getSourceMimeType()) {
  90. $params['ContentType'] = $type;
  91. }
  92. return $params;
  93. }
  94. /**
  95. * @return UploadState
  96. */
  97. abstract protected function getState();
  98. /**
  99. * @return array
  100. */
  101. abstract protected function getConfig();
  102. /**
  103. * @return int
  104. */
  105. abstract protected function getSourceSize();
  106. /**
  107. * @return string|null
  108. */
  109. abstract protected function getSourceMimeType();
  110. }