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