-
Notifications
You must be signed in to change notification settings - Fork 126
/
s3.controller.php
108 lines (88 loc) · 1.99 KB
/
s3.controller.php
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
<?php
/**
* Config needed
*
* S3_BUCKET
* S3_ACCESS_KEY
* S3_SECRET_KEY
* (optional) S3_ENDPOINT
*/
class S3Storage implements StorageController
{
private $s3;
function connect(){
$this->s3 = new Aws\S3\S3Client([
'version' => 'latest',
'region' => (defined('S3_REGION') && S3_REGION ?S3_REGION:'us-east-1'),
'endpoint' => S3_ENDPOINT,
'use_path_style_endpoint' => true,
'credentials' => [
'key' => S3_ACCESS_KEY,
'secret' => S3_SECRET_KEY,
],
]);
}
function isEnabled()
{
return (defined('S3_BUCKET') && S3_BUCKET);
}
function hashExists($hash)
{
if(!$this->s3)$this->connect();
return $this->s3->doesObjectExist(S3_BUCKET,$hash);
}
function getItems($dev=false)
{
if(!$this->s3)$this->connect();
$KeyCount = 9999;
$keys = 100; //the amount of keys we'll receive per request. 1000 max but that times out sometimes
$lastkey = false;
$count = 0;
$items = array();
while($KeyCount>=$keys)
{
$objects = $this->s3->listObjectsV2([
'Bucket' => S3_BUCKET,
'MaxKeys'=> $keys,
'StartAfter'=>($lastkey?$lastkey:'')
]);
++$count;
foreach ($objects['Contents'] as $object){
$lastkey = $object['Key'];
$items[] = $lastkey;
}
if($dev===true) echo " Got ".($count*$keys)." files \r";
$KeyCount = $objects['KeyCount'];
}
return $items;
}
function pullFile($hash,$location)
{
if(!$this->s3)$this->connect();
if(!$this->hashExists($hash)) return false;
$this->s3->getObject([
'Bucket' => S3_BUCKET,
'Key' => $hash,
'SaveAs' => $location
]);
return true;
}
function pushFile($source,$hash)
{
if(!$this->s3)$this->connect();
$this->s3->putObject([
'Bucket' => S3_BUCKET,
'Key' => $hash,
'SourceFile' => $source
]);
return true;
}
function deleteFile($hash)
{
if(!$this->s3)$this->connect();
$this->s3->deleteObject([
'Bucket' => S3_BUCKET,
'Key' => $hash
]);
}
}