|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Simple S3 package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Mauro Cassani<https://github.com/mauretto78> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Matecat\SimpleS3\Commands\Handlers; |
|
13
|
|
|
|
|
14
|
|
|
use Aws\ResultInterface; |
|
15
|
|
|
use Aws\S3\Exception\S3Exception; |
|
16
|
|
|
use Matecat\SimpleS3\Commands\CommandHandler; |
|
17
|
|
|
|
|
18
|
|
|
class CopyItem extends CommandHandler |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Copy an item from a bucket to another one. |
|
22
|
|
|
* For a complete reference: |
|
23
|
|
|
* https://docs.aws.amazon.com/cli/latest/reference/s3api/copy-object.html?highlight=copy |
|
24
|
|
|
* |
|
25
|
|
|
* @param mixed $params |
|
26
|
|
|
* |
|
27
|
|
|
* @return bool |
|
28
|
|
|
* @throws \Exception |
|
29
|
|
|
*/ |
|
30
|
|
|
public function handle($params = []) |
|
31
|
|
|
{ |
|
32
|
|
|
$targetBucketName = $params['target_bucket']; |
|
33
|
|
|
$targetKeyname = $params['target']; |
|
34
|
|
|
$sourceBucket = $params['source_bucket']; |
|
35
|
|
|
$sourceKeyname = $params['source']; |
|
36
|
|
|
|
|
37
|
|
|
$this->client->createBucketIfItDoesNotExist(['bucket' => $targetBucketName]); |
|
38
|
|
|
|
|
39
|
|
|
if ($this->client->hasEncoder()) { |
|
40
|
|
|
$targetKeyname = $this->client->getEncoder()->encode($targetKeyname); |
|
41
|
|
|
$sourceKeyname = $this->client->getEncoder()->encode($sourceKeyname); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$copySource = trim($this->getCopySource($sourceBucket, $sourceKeyname)); |
|
45
|
|
|
|
|
46
|
|
|
try { |
|
47
|
|
|
$config = [ |
|
48
|
|
|
'Bucket' => $targetBucketName, |
|
49
|
|
|
'Key' => $targetKeyname, |
|
50
|
|
|
'CopySource' => $copySource, |
|
51
|
|
|
]; |
|
52
|
|
|
|
|
53
|
|
|
if ($this->client->isBucketVersioned(['bucket' => $sourceBucket])) { |
|
54
|
|
|
$version = $this->client->getCurrentItemVersion(['bucket' => $sourceBucket, 'key' => $params['source']]); |
|
55
|
|
|
$config['CopySource'] = $copySource . '?versionId='.$version; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$copied = $this->client->getConn()->copyObject($config); |
|
59
|
|
|
|
|
60
|
|
|
if (($copied instanceof ResultInterface) and $copied['@metadata']['statusCode'] === 200) { |
|
61
|
|
|
if (null !== $this->commandHandlerLogger) { |
|
62
|
|
|
$this->commandHandlerLogger->log($this, sprintf('File \'%s/%s\' was successfully copied to \'%s/%s\'', $sourceBucket, $sourceKeyname, $targetBucketName, $targetKeyname)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if ($this->client->hasCache()) { |
|
66
|
|
|
$this->client->getCache()->set($targetBucketName, $targetKeyname, ''); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if (null !== $this->commandHandlerLogger) { |
|
73
|
|
|
$this->commandHandlerLogger->log($this, sprintf('Something went wrong in copying file \'%s/%s\'', $sourceBucket, $sourceKeyname), 'warning'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return false; |
|
77
|
|
|
} catch (S3Exception $exception) { |
|
78
|
|
|
if (null !== $this->commandHandlerLogger) { |
|
79
|
|
|
$this->commandHandlerLogger->logExceptionAndReturnFalse($exception); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
throw $exception; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param array $params |
|
88
|
|
|
* |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
|
|
public function validateParams($params = []) |
|
92
|
|
|
{ |
|
93
|
|
|
return ( |
|
94
|
|
|
isset($params['target_bucket']) and |
|
95
|
|
|
isset($params['target']) and |
|
96
|
|
|
isset($params['source_bucket']) and |
|
97
|
|
|
isset($params['source']) |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Returns an urlencoded string for PUT requests |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $sourceBucket |
|
105
|
|
|
* @param string $sourceKeyname |
|
106
|
|
|
* |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function getCopySource($sourceBucket, $sourceKeyname) |
|
110
|
|
|
{ |
|
111
|
|
|
if ($this->client->hasEncoder()) { |
|
112
|
|
|
return $sourceBucket . $this->client->getPrefixSeparator() . $sourceKeyname; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$encoded = []; |
|
116
|
|
|
|
|
117
|
|
|
foreach (explode($this->client->getPrefixSeparator(), $sourceKeyname) as $word) { |
|
118
|
|
|
$encoded[] = urlencode($word); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $sourceBucket . $this->client->getPrefixSeparator() . implode($this->client->getPrefixSeparator(), $encoded); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|