-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathS3Helper.php
More file actions
executable file
·177 lines (152 loc) · 6.05 KB
/
S3Helper.php
File metadata and controls
executable file
·177 lines (152 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
namespace TestPlugin\DataClasses\ServiceHelpers {
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
use TestPlugin\UtilityFunctions;
/**
* A class that helps connect and use an Amazon Web Services S3 service
* */
class S3Helper {
private $s3client;
public function getS3Client():?S3Client { return $this->s3client; }
private $awsConfig;
private $s3config;
private $buckets;
private $primaryBucketName = "";
/**
* Gets the primary bucket to be used with this S3 instance
*
* @return S3Bucket|null
*/
public function getPrimaryBucket():?S3Bucket {
return (empty($this->primaryBucketName)) ? null : $this->buckets[$this->primaryBucketName];
}
public function __construct(array $awsconfig, array $s3config) {
$this->awsConfig = $awsconfig;
$this->s3config = $s3config;
if(!$this->verifyConfig() ) {
throw new \Exception("The AWS S3 Configuration is missing required fields.");
}
$this->buckets = [];
$this->initClient();
$this->verifyBuckets();
}
public function verifyConfig() : bool {
$valid = is_array($this->s3config)
&& array_key_exists("buckets",$this->s3config)
&& is_array($this->s3config["buckets"]);
if($valid) {
foreach($this->s3config["buckets"] as $bucket){
$valid = is_array($bucket)
&& array_key_exists("name", $bucket)
&& is_string($bucket["name"])
&& array_key_exists("default_dir", $bucket)
&& is_string($bucket["default_dir"])
&& array_key_exists("file_prefix", $bucket)
&& is_string($bucket["file_prefix"]);
if(!$valid) {
break;
}
}
}
return $valid;
}
private function initClient() {
$this->s3client = new S3Client($this->awsConfig);
}
/**
* Ensures every bucket associated with this S3 instance actually exists
**/
public function verifyBuckets() {
$invalidBuckets = [];
foreach ($this->s3config["buckets"] as $bucketInfo) {
$bucket = new S3Bucket($this, $bucketInfo);
if($bucket->bucketExists() ) {
$this->buckets[$bucketInfo["name"]] = $bucket;
if(empty($this->primaryBucketName) && ($bucketInfo['primary'] === true)) {
$this->primaryBucketName = $bucketInfo["name"];
}
} else {
$invalidBuckets[] = $bucketInfo["name"];
}
}
if(count($this->buckets) && empty($this->primaryBucketName)) {
$this->primaryBucketName = $this->buckets[0]->getBucketName();//set the first bucket as the primary
}
if(count($invalidBuckets)>0 ) {
throw new \Exception("The following buckets were not found: ".implode(", ", $invalidBuckets) );
}
}
/**
* Gets a bucket from this S3 instance, by name
*
* @param string $bucketName The bucket name to fetch
*
* @return S3Bucket|null The found buckwt for the provided name, or null
**/
public function getBucket(string $bucketName):S3Bucket {
if(!array_key_exists($bucketName, $this->buckets) ) {
return NULL;
} else {
return $this->buckets[$bucketName];
}
}
/**
* Gets all the bucket contents info for this S3 instance
*
* @return array The S3 bucket contents as an array in the form:
* ["html"=>$tableHtml, "data"=>$allBucketContents]
*
**/
public function listAllBucketContents() {
$allBucketContents = $this->getAllBucketContents();
$tableHtml =
<table class=\"table table-bordered\">
<thead>
<tr>
<th>Bucket</th>
<th>Blob</th>
</tr>
</thead>
<tbody>
";
foreach ($allBucketContents as $bucketName => $bucketContents) {
if(count($bucketContents) > 0) {
foreach ($bucketContents as $object) {//this is paginated, hence a loop needed
$tableHtml .= "<tr>
<td>".$bucketName."</td>
<td><a href='https://".$bucketName.".s3.amazonaws.com/".$object['Key']."'>".$object['Key']."</a></td>
</tr>";
}
} else {
$tableHtml .=
<td> No files </td>
";
}
}
$tableHtml .=
</tbody>
</table>
";
return ["html"=>$tableHtml, "data"=>$allBucketContents];
}
/**
* Gets all the bucket contents data for this S3 instance
*
* @return array The S3 bucket contents as an array in the form:
* ["bucketName1" => contents array
*
**/
public function getAllBucketContents():array {
$allBucketContents = [];
foreach ($this->buckets as $bucketname => $bucket) {
$allBucketContents[$bucketname] = $bucket->getBucketContents(true);
}
return $allBucketContents;
}
function __destruct() {
$this->s3client = NULL;
$this->buckets = NULL;
}
}
}