MultipartUploadingTrait.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. $partData = [];
  49. $partData['PartNumber'] = $command['PartNumber'];
  50. $partData['ETag'] = $this->extractETag($result);
  51. if (isset($command['ChecksumAlgorithm'])) {
  52. $checksumMemberName = 'Checksum' . strtoupper($command['ChecksumAlgorithm']);
  53. $partData[$checksumMemberName] = $result[$checksumMemberName];
  54. }
  55. $this->getState()->markPartAsUploaded($command['PartNumber'], $partData);
  56. }
  57. abstract protected function extractETag(ResultInterface $result);
  58. protected function getCompleteParams()
  59. {
  60. $config = $this->getConfig();
  61. $params = isset($config['params']) ? $config['params'] : [];
  62. $params['MultipartUpload'] = [
  63. 'Parts' => $this->getState()->getUploadedParts()
  64. ];
  65. return $params;
  66. }
  67. protected function determinePartSize()
  68. {
  69. // Make sure the part size is set.
  70. $partSize = $this->getConfig()['part_size'] ?: MultipartUploader::PART_MIN_SIZE;
  71. // Adjust the part size to be larger for known, x-large uploads.
  72. if ($sourceSize = $this->getSourceSize()) {
  73. $partSize = (int) max(
  74. $partSize,
  75. ceil($sourceSize / MultipartUploader::PART_MAX_NUM)
  76. );
  77. }
  78. // Ensure that the part size follows the rules: 5 MB <= size <= 5 GB.
  79. if ($partSize < MultipartUploader::PART_MIN_SIZE || $partSize > MultipartUploader::PART_MAX_SIZE) {
  80. throw new \InvalidArgumentException('The part size must be no less '
  81. . 'than 5 MB and no greater than 5 GB.');
  82. }
  83. return $partSize;
  84. }
  85. protected function getInitiateParams()
  86. {
  87. $config = $this->getConfig();
  88. $params = isset($config['params']) ? $config['params'] : [];
  89. if (isset($config['acl'])) {
  90. $params['ACL'] = $config['acl'];
  91. }
  92. // Set the ContentType if not already present
  93. if (empty($params['ContentType']) && $type = $this->getSourceMimeType()) {
  94. $params['ContentType'] = $type;
  95. }
  96. return $params;
  97. }
  98. /**
  99. * @return UploadState
  100. */
  101. abstract protected function getState();
  102. /**
  103. * @return array
  104. */
  105. abstract protected function getConfig();
  106. /**
  107. * @return int
  108. */
  109. abstract protected function getSourceSize();
  110. /**
  111. * @return string|null
  112. */
  113. abstract protected function getSourceMimeType();
  114. }