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
|
5 |
|
public function handle($params = []) |
31
|
|
|
{ |
32
|
5 |
|
$targetBucketName = $params['target_bucket']; |
33
|
5 |
|
$targetKeyname = $params['target']; |
34
|
5 |
|
$sourceBucket = $params['source_bucket']; |
35
|
5 |
|
$sourceKeyname = $params['source']; |
36
|
|
|
|
37
|
5 |
|
$this->client->createBucketIfItDoesNotExist(['bucket' => $targetBucketName]); |
38
|
|
|
|
39
|
5 |
|
if ($this->client->hasEncoder()) { |
40
|
3 |
|
$targetKeyname = $this->client->getEncoder()->encode($targetKeyname); |
41
|
3 |
|
$sourceKeyname = $this->client->getEncoder()->encode($sourceKeyname); |
42
|
|
|
} |
43
|
|
|
|
44
|
5 |
|
$copySource = trim($this->getCopySource($sourceBucket, $sourceKeyname)); |
45
|
|
|
|
46
|
|
|
try { |
47
|
|
|
$config = [ |
48
|
5 |
|
'Bucket' => $targetBucketName, |
49
|
5 |
|
'Key' => $targetKeyname, |
50
|
5 |
|
'CopySource' => $copySource, |
51
|
|
|
]; |
52
|
|
|
|
53
|
5 |
|
if ($this->client->isBucketVersioned(['bucket' => $sourceBucket])) { |
54
|
1 |
|
$version = $this->client->getCurrentItemVersion(['bucket' => $sourceBucket, 'key' => $params['source']]); |
55
|
1 |
|
$config['CopySource'] = $copySource . '?versionId='.$version; |
56
|
|
|
} |
57
|
|
|
|
58
|
5 |
|
$copied = $this->client->getConn()->copyObject($config); |
59
|
|
|
|
60
|
5 |
|
if (($copied instanceof ResultInterface) and $copied['@metadata']['statusCode'] === 200) { |
61
|
5 |
|
if (null !== $this->commandHandlerLogger) { |
62
|
5 |
|
$this->commandHandlerLogger->log($this, sprintf('File \'%s/%s\' was successfully copied to \'%s/%s\'', $sourceBucket, $sourceKeyname, $targetBucketName, $targetKeyname)); |
63
|
|
|
} |
64
|
|
|
|
65
|
5 |
|
if ($this->client->hasCache()) { |
66
|
3 |
|
$this->client->getCache()->set($targetBucketName, $targetKeyname, ''); |
67
|
|
|
} |
68
|
|
|
|
69
|
5 |
|
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
|
5 |
|
public function validateParams($params = []) |
92
|
|
|
{ |
93
|
|
|
return ( |
94
|
5 |
|
isset($params['target_bucket']) and |
95
|
5 |
|
isset($params['target']) and |
96
|
5 |
|
isset($params['source_bucket']) and |
97
|
5 |
|
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
|
8 |
|
protected function getCopySource($sourceBucket, $sourceKeyname) |
110
|
|
|
{ |
111
|
8 |
|
if ($this->client->hasEncoder()) { |
112
|
5 |
|
return $sourceBucket . $this->client->getPrefixSeparator() . $sourceKeyname; |
113
|
|
|
} |
114
|
|
|
|
115
|
3 |
|
$encoded = []; |
116
|
|
|
|
117
|
3 |
|
foreach (explode($this->client->getPrefixSeparator(), $sourceKeyname) as $word) { |
118
|
3 |
|
$encoded[] = urlencode($word); |
119
|
|
|
} |
120
|
|
|
|
121
|
3 |
|
return $sourceBucket . $this->client->getPrefixSeparator() . implode($this->client->getPrefixSeparator(), $encoded); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|