Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/S3/ObjectCopier.php

raw symbols SCIP
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 * Copies objects from one S3 location to another, utilizing a multipart copy
16 * when appropriate.
17 *
18 * Makes a HeadObject call on the source to determine object size. Objects
19 * below the multipart threshold (default 5 GB) are copied with a single
20 * CopyObject call using MetadataDirective: COPY.
21 *
22 * Objects above the threshold use MultipartCopy, which preserves source metadata by default.
23 * When the multipart path is used with the default metadata_directive ('COPY'),
24 * metadata fields in 'params' (e.g. Metadata, ContentType, CacheControl)
25 * are overridden by the source object's values.
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 * @param S3ClientInterface $client The S3 Client used to execute
49 * the copy command(s).
50 * @param array $source The object to copy, specified as
51 * an array with a 'Bucket' and
52 * 'Key' keys. Provide a
53 * 'VersionID' key to copy a
54 * specified version of an object.
55 * @param array $destination The bucket and key to which to
56 * copy the $source, specified as
57 * an array with a 'Bucket' and
58 * 'Key' keys.
59 * @param string $acl ACL to apply to the copy
60 * (default: private).
61 * @param array $options Options used to configure the
62 * copy process. Options passed in
63 * through 'params' are added to
64 * the sub commands.
65 *
66 * @throws InvalidArgumentException
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 * Perform the configured copy asynchronously. Returns a promise that is
87 * fulfilled with the result of the CompleteMultipartUpload or CopyObject
88 * operation or rejected with an exception.
89 *
90 * @return Coroutine
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 * Perform the configured copy synchronously. Returns the result of the
135 * CompleteMultipartUpload or CopyObject operation.
136 *
137 * @return Result
138 *
139 * @throws S3Exception
140 * @throws MultipartUploadException
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 }