Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
namespace Aws\S3;
4
 
5
use Aws\Arn\ArnParser;
6
use Aws\Multipart\AbstractUploadManager;
7
use Aws\ResultInterface;
8
use GuzzleHttp\Psr7;
9
 
10
class MultipartCopy extends AbstractUploadManager
11
{
12
    use MultipartUploadingTrait;
13
 
14
    /** @var string|array */
15
    private $source;
16
    /** @var string */
17
    private $sourceVersionId;
18
    /** @var ResultInterface */
19
    private $sourceMetadata;
20
 
21
    /**
22
     * Creates a multipart upload for copying an S3 object.
23
     *
24
     * The valid configuration options are as follows:
25
     *
26
     * - acl: (string) ACL to set on the object being upload. Objects are
27
     *   private by default.
28
     * - before_complete: (callable) Callback to invoke before the
29
     *   `CompleteMultipartUpload` operation. The callback should have a
30
     *   function signature like `function (Aws\Command $command) {...}`.
31
     * - before_initiate: (callable) Callback to invoke before the
32
     *   `CreateMultipartUpload` operation. The callback should have a function
33
     *   signature like `function (Aws\Command $command) {...}`.
34
     * - before_upload: (callable) Callback to invoke before `UploadPartCopy`
35
     *   operations. The callback should have a function signature like
36
     *   `function (Aws\Command $command) {...}`.
37
     * - bucket: (string, required) Name of the bucket to which the object is
38
     *   being uploaded.
39
     * - concurrency: (int, default=int(5)) Maximum number of concurrent
40
     *   `UploadPart` operations allowed during the multipart upload.
41
     * - key: (string, required) Key to use for the object being uploaded.
42
     * - params: (array) An array of key/value parameters that will be applied
43
     *   to each of the sub-commands run by the uploader as a base.
44
     *   Auto-calculated options will override these parameters. If you need
45
     *   more granularity over parameters to each sub-command, use the before_*
46
     *   options detailed above to update the commands directly.
47
     * - part_size: (int, default=int(5242880)) Part size, in bytes, to use when
48
     *   doing a multipart upload. This must between 5 MB and 5 GB, inclusive.
49
     * - state: (Aws\Multipart\UploadState) An object that represents the state
50
     *   of the multipart upload and that is used to resume a previous upload.
51
     *   When this option is provided, the `bucket`, `key`, and `part_size`
52
     *   options are ignored.
53
     * - source_metadata: (Aws\ResultInterface) An object that represents the
54
     *   result of executing a HeadObject command on the copy source.
1441 ariadna 55
     * - display_progress: (boolean) Set true to track status in 1/8th increments
56
     *   for upload.
1 efrain 57
     *
58
     * @param S3ClientInterface $client Client used for the upload.
59
     * @param string|array $source Location of the data to be copied (in the
60
     *                       form /<bucket>/<key>).  If the key contains a '?'
61
     *                       character, instead pass an array of source_key,
62
     *                       source_bucket, and source_version_id.
63
     * @param array $config Configuration used to perform the upload.
64
     */
65
    public function __construct(
66
        S3ClientInterface $client,
67
        $source,
68
        array $config = []
69
    ) {
70
        if (is_array($source)) {
71
            $this->source = $source;
72
        } else {
73
            $this->source = $this->getInputSource($source);
74
        }
75
 
76
        parent::__construct(
77
            $client,
78
            array_change_key_case($config) + ['source_metadata' => null]
79
        );
1441 ariadna 80
 
81
        if ($this->displayProgress) {
82
            $this->getState()->setProgressThresholds(
83
                $this->sourceMetadata["ContentLength"]
84
            );
85
        }
1 efrain 86
    }
87
 
88
    /**
89
     * An alias of the self::upload method.
90
     *
91
     * @see self::upload
92
     */
93
    public function copy()
94
    {
95
        return $this->upload();
96
    }
97
 
98
    protected function loadUploadWorkflowInfo()
99
    {
100
        return [
101
            'command' => [
102
                'initiate' => 'CreateMultipartUpload',
103
                'upload' => 'UploadPartCopy',
104
                'complete' => 'CompleteMultipartUpload',
105
            ],
106
            'id' => [
107
                'bucket' => 'Bucket',
108
                'key' => 'Key',
109
                'upload_id' => 'UploadId',
110
            ],
111
            'part_num' => 'PartNumber',
112
        ];
113
    }
114
 
115
    protected function getUploadCommands(callable $resultHandler)
116
    {
117
        $parts = ceil($this->getSourceSize() / $this->determinePartSize());
118
 
119
        for ($partNumber = 1; $partNumber <= $parts; $partNumber++) {
120
            // If we haven't already uploaded this part, yield a new part.
121
            if (!$this->state->hasPartBeenUploaded($partNumber)) {
122
                $command = $this->client->getCommand(
123
                    $this->info['command']['upload'],
124
                    $this->createPart($partNumber, $parts) + $this->getState()->getId()
125
                );
126
                $command->getHandlerList()->appendSign($resultHandler, 'mup');
127
                yield $command;
128
            }
129
        }
130
    }
131
 
132
    private function createPart($partNumber, $partsCount)
133
    {
134
        $data = [];
135
 
136
        // Apply custom params to UploadPartCopy data
137
        $config = $this->getConfig();
138
        $params = isset($config['params']) ? $config['params'] : [];
139
        foreach ($params as $k => $v) {
140
            $data[$k] = $v;
141
        }
142
        // The source parameter here is usually a string, but can be overloaded as an array
143
        // if the key contains a '?' character to specify where the query parameters start
144
        if (is_array($this->source)) {
145
            $key = str_replace('%2F', '/', rawurlencode($this->source['source_key']));
146
            $bucket = $this->source['source_bucket'];
147
        } else {
148
            list($bucket, $key) = explode('/', ltrim($this->source, '/'), 2);
149
            $key = implode(
150
                '/',
151
                array_map(
152
                    'urlencode',
153
                    explode('/', rawurldecode($key))
154
                )
155
            );
156
        }
157
 
158
        $uri = ArnParser::isArn($bucket) ? '' : '/';
159
        $uri .= $bucket . '/' . $key;
160
        $data['CopySource'] = $uri;
161
        $data['PartNumber'] = $partNumber;
162
        if (!empty($this->sourceVersionId)) {
163
            $data['CopySource'] .= "?versionId=" . $this->sourceVersionId;
164
        }
165
 
166
        $defaultPartSize = $this->determinePartSize();
167
        $startByte = $defaultPartSize * ($partNumber - 1);
168
        $data['ContentLength'] = $partNumber < $partsCount
169
            ? $defaultPartSize
170
            : $this->getSourceSize() - ($defaultPartSize * ($partsCount - 1));
171
        $endByte = $startByte + $data['ContentLength'] - 1;
172
        $data['CopySourceRange'] = "bytes=$startByte-$endByte";
173
 
174
        return $data;
175
    }
176
 
177
    protected function extractETag(ResultInterface $result)
178
    {
179
        return $result->search('CopyPartResult.ETag');
180
    }
181
 
182
    protected function getSourceMimeType()
183
    {
184
        return $this->getSourceMetadata()['ContentType'];
185
    }
186
 
187
    protected function getSourceSize()
188
    {
189
        return $this->getSourceMetadata()['ContentLength'];
190
    }
191
 
192
    private function getSourceMetadata()
193
    {
194
        if (empty($this->sourceMetadata)) {
195
            $this->sourceMetadata = $this->fetchSourceMetadata();
196
        }
197
 
198
        return $this->sourceMetadata;
199
    }
200
 
201
    private function fetchSourceMetadata()
202
    {
203
        if ($this->config['source_metadata'] instanceof ResultInterface) {
204
            return $this->config['source_metadata'];
205
        }
206
        //if the source variable was overloaded with an array, use the inputs for key and bucket
207
        if (is_array($this->source)) {
208
            $headParams = [
209
                'Key' => $this->source['source_key'],
210
                'Bucket' => $this->source['source_bucket']
211
            ];
212
            if (isset($this->source['source_version_id'])) {
213
                $this->sourceVersionId = $this->source['source_version_id'];
214
                $headParams['VersionId'] = $this->sourceVersionId;
215
            }
216
        //otherwise, use the default source parsing behavior
217
        } else {
218
            list($bucket, $key) = explode('/', ltrim($this->source, '/'), 2);
219
            $headParams = [
220
                'Bucket' => $bucket,
221
                'Key' => $key,
222
            ];
223
            if (strpos($key, '?')) {
224
                list($key, $query) = explode('?', $key, 2);
225
                $headParams['Key'] = $key;
226
                $query = Psr7\Query::parse($query, false);
227
                if (isset($query['versionId'])) {
228
                    $this->sourceVersionId = $query['versionId'];
229
                    $headParams['VersionId'] = $this->sourceVersionId;
230
                }
231
            }
232
        }
233
        return $this->client->headObject($headParams);
234
    }
235
 
236
    /**
237
     * Get the url decoded input source, starting with a slash if it is not an
238
     * ARN to standardize the source location syntax.
239
     *
240
     * @param string $inputSource The source that was passed to the constructor
241
     * @return string The source, starting with a slash if it's not an arn
242
     */
243
    private function getInputSource($inputSource)
244
    {
245
        $sourceBuilder = ArnParser::isArn($inputSource) ? '' : '/';
246
        $sourceBuilder .= ltrim(rawurldecode($inputSource), '/');
247
        return $sourceBuilder;
248
    }
249
}