| 1 |
<?php |
| 2 |
namespace Aws\S3; |
| 3 |
|
| 4 |
use Aws\Arn\ArnParser; |
| 5 |
use Aws\Arn\S3\AccessPointArn; |
| 6 |
use Aws\Exception\MultipartUploadException; |
| 7 |
use Aws\Result; |
| 8 |
use Aws\S3\Exception\S3Exception; |
| 9 |
use GuzzleHttp\Promise\Coroutine; |
| 10 |
use GuzzleHttp\Promise\PromiseInterface; |
| 11 |
use GuzzleHttp\Promise\PromisorInterface; |
| 12 |
use InvalidArgumentException; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
class ObjectCopier implements PromisorInterface |
| 28 |
{ |
| 29 |
const DEFAULT_MULTIPART_THRESHOLD = MultipartUploader::PART_MAX_SIZE; |
| 30 |
|
| 31 |
private $client; |
| 32 |
private $source; |
| 33 |
private $destination; |
| 34 |
private $acl; |
| 35 |
private $options; |
| 36 |
|
| 37 |
private static $defaults = [ |
| 38 |
'before_lookup' => null, |
| 39 |
'before_upload' => null, |
| 40 |
'concurrency' => 5, |
| 41 |
'mup_threshold' => self::DEFAULT_MULTIPART_THRESHOLD, |
| 42 |
'params' => [], |
| 43 |
'part_size' => null, |
| 44 |
'version_id' => null, |
| 45 |
]; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public function __construct( |
| 69 |
S3ClientInterface $client, |
| 70 |
array $source, |
| 71 |
array $destination, |
| 72 |
$acl = 'private', |
| 73 |
array $options = [] |
| 74 |
) { |
| 75 |
$this->validateLocation($source); |
| 76 |
$this->validateLocation($destination); |
| 77 |
|
| 78 |
$this->client = $client; |
| 79 |
$this->source = $source; |
| 80 |
$this->destination = $destination; |
| 81 |
$this->acl = $acl; |
| 82 |
$this->options = $options + self::$defaults; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public function promise(): PromiseInterface |
| 93 |
{ |
| 94 |
return Coroutine::of(function () { |
| 95 |
$headObjectCommand = $this->client->getCommand( |
| 96 |
'HeadObject', |
| 97 |
$this->options['params'] + $this->source |
| 98 |
); |
| 99 |
if (is_callable($this->options['before_lookup'])) { |
| 100 |
$this->options['before_lookup']($headObjectCommand); |
| 101 |
} |
| 102 |
$objectStats = (yield $this->client->executeAsync( |
| 103 |
$headObjectCommand |
| 104 |
)); |
| 105 |
|
| 106 |
if ($objectStats['ContentLength'] > $this->options['mup_threshold']) { |
| 107 |
$mup = new MultipartCopy( |
| 108 |
$this->client, |
| 109 |
$this->getSourcePath(), |
| 110 |
['source_metadata' => $objectStats, 'acl' => $this->acl] |
| 111 |
+ $this->destination |
| 112 |
+ $this->options |
| 113 |
); |
| 114 |
|
| 115 |
yield $mup->promise(); |
| 116 |
} else { |
| 117 |
$defaults = [ |
| 118 |
'ACL' => $this->acl, |
| 119 |
'MetadataDirective' => 'COPY', |
| 120 |
'CopySource' => $this->getSourcePath(), |
| 121 |
]; |
| 122 |
|
| 123 |
$params = array_diff_key($this->options, self::$defaults) |
| 124 |
+ $this->destination + $defaults + $this->options['params']; |
| 125 |
|
| 126 |
yield $this->client->executeAsync( |
| 127 |
$this->client->getCommand('CopyObject', $params) |
| 128 |
); |
| 129 |
} |
| 130 |
}); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public function copy() |
| 143 |
{ |
| 144 |
return $this->promise()->wait(); |
| 145 |
} |
| 146 |
|
| 147 |
private function validateLocation(array $location) |
| 148 |
{ |
| 149 |
if (empty($location['Bucket']) || empty($location['Key'])) { |
| 150 |
throw new \InvalidArgumentException('Locations provided to an' |
| 151 |
. ' Aws\S3\ObjectCopier must have a non-empty Bucket and Key'); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
private function getSourcePath() |
| 156 |
{ |
| 157 |
$path = "/{$this->source['Bucket']}/"; |
| 158 |
if (ArnParser::isArn($this->source['Bucket'])) { |
| 159 |
try { |
| 160 |
new AccessPointArn($this->source['Bucket']); |
| 161 |
$path = "{$this->source['Bucket']}/object/"; |
| 162 |
} catch (\Exception $e) { |
| 163 |
throw new \InvalidArgumentException( |
| 164 |
'Provided ARN was a not a valid S3 access point ARN (' |
| 165 |
. $e->getMessage() . ')', |
| 166 |
0, |
| 167 |
$e |
| 168 |
); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
$sourcePath = $path . rawurlencode($this->source['Key']); |
| 173 |
if (isset($this->source['VersionId'])) { |
| 174 |
$sourcePath .= "?versionId={$this->source['VersionId']}"; |
| 175 |
} |
| 176 |
|
| 177 |
return $sourcePath; |
| 178 |
} |
| 179 |
} |