1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Collector; |
3
|
|
|
|
4
|
|
|
use Aws\S3\S3Client; |
5
|
|
|
use phpbu\App\Backup\Collector; |
6
|
|
|
use phpbu\App\Backup\File\AmazonS3v3 as AwsFile; |
7
|
|
|
use phpbu\App\Backup\Path; |
8
|
|
|
use phpbu\App\Backup\Target; |
9
|
|
|
use phpbu\App\Util; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* AmazonS3v3 class. |
13
|
|
|
* |
14
|
|
|
* @package phpbu |
15
|
|
|
* @subpackage Backup |
16
|
|
|
* @author Sebastian Feldmann <[email protected]> |
17
|
|
|
* @author Vitaly Baev <[email protected]> |
18
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
19
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
20
|
|
|
* @link http://phpbu.de/ |
21
|
|
|
* @since Class available since Release 5.1.0 |
22
|
|
|
*/ |
23
|
|
|
class AmazonS3v3 extends Collector |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var \Aws\S3\S3Client |
27
|
|
|
*/ |
28
|
|
|
protected $client; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Amazon S3 bucket name |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $bucket; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Amazon S3 constructor. |
39
|
|
|
* |
40
|
|
|
* @param \phpbu\App\Backup\Target $target |
41
|
|
|
* @param S3Client $client |
42
|
|
|
* @param string $bucket |
43
|
|
|
* @param string $path |
44
|
|
|
* @param int $time |
45
|
|
|
*/ |
46
|
1 |
|
public function __construct(Target $target, S3Client $client, string $bucket, string $path, int $time) |
47
|
|
|
{ |
48
|
1 |
|
$this->client = $client; |
49
|
1 |
|
$this->bucket = $bucket; |
50
|
1 |
|
$this->path = new Path($path, $time); |
51
|
1 |
|
$this->setUp($target); |
52
|
1 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get all created backups. |
56
|
|
|
* |
57
|
|
|
* @return \phpbu\App\Backup\File[] |
58
|
|
|
*/ |
59
|
1 |
|
public function getBackupFiles() : array |
60
|
|
|
{ |
61
|
1 |
|
$result = $this->client->listObjects([ |
62
|
1 |
|
'Bucket' => $this->bucket, |
63
|
1 |
|
'Prefix' => $this->getPrefix($this->path->getPathThatIsNotChanging()), |
64
|
|
|
]); |
65
|
|
|
|
66
|
1 |
|
if (!isset($result['Contents']) || !$result['Contents'] || !is_array($result['Contents'])) { |
67
|
1 |
|
return []; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
View Code Duplication |
foreach ($result['Contents'] as $object) { |
71
|
|
|
// skip currently created backup |
72
|
1 |
|
if ($object['Key'] == $this->getPrefix() . $this->target->getFilename()) { |
73
|
1 |
|
continue; |
74
|
|
|
} |
75
|
1 |
|
if ($this->isFileMatch($object['Key'])) { |
76
|
1 |
|
$this->files[] = new AwsFile($this->client, $this->bucket, $object); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
return $this->files; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Return prefix for querying remote files and folders |
85
|
|
|
* |
86
|
|
|
* @param string|null $path |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
1 |
|
protected function getPrefix($path = null): string |
90
|
|
|
{ |
91
|
1 |
|
$path = $path ?: $this->path->getPathThatIsNotChanging(); |
92
|
1 |
|
$prefix = Util\Path::withoutLeadingSlash($path); |
93
|
1 |
|
$prefix = $prefix ? Util\Path::withTrailingSlash($prefix) : ''; |
94
|
1 |
|
return $prefix; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|