-
Notifications
You must be signed in to change notification settings - Fork 11
/
S3Storage.php
166 lines (145 loc) · 3.62 KB
/
S3Storage.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
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
<?php
namespace Kanboard\Plugin\S3;
require_once __DIR__.'/vendor/aws-autoloader.php';
use Kanboard\Core\ObjectStorage\ObjectStorageInterface;
use Kanboard\Core\ObjectStorage\ObjectStorageException;
use Aws\S3\S3Client;
use Aws\Credentials\Credentials;
/**
* S3 storage driver
*
* @package objectStorage
* @author Frederic Guillot
*/
class S3Storage implements ObjectStorageInterface
{
/**
* @var S3Client
*/
private $client;
/**
* @var string
*/
private $bucket = '';
/**
* @var string
*/
private $prefix = '';
/**
* Constructor
*
* @access public
* @param string $key AWS API Key
* @param string $secret AWS API Secret
* @param string $region AWS S3 Region
* @param string $bucket AWS S3 bucket
* @param string $prefix Object prefix
*/
public function __construct($key, $secret, $region, $bucket, $prefix, $custom_options)
{
$this->bucket = $bucket;
$credentials = new Credentials($key, $secret);
$opts = array_merge([
'credentials' => $credentials,
'region' => $region,
'version' => '2006-03-01',
], $custom_options);
$this->client = new S3Client($opts);
$this->client->registerStreamWrapper();
$this->prefix = $prefix;
}
/**
* Fetch object contents
*
* @access public
* @param string $key
* @return string
* @throws ObjectStorageException
*/
public function get($key)
{
$data = @file_get_contents($this->getObjectPath($key));
if ($data === false) {
throw new ObjectStorageException('Object not found');
}
return $data;
}
/**
* Save object
*
* @access public
* @param string $key
* @param string $blob
* @return boolean
* @throws ObjectStorageException
*/
public function put($key, &$blob)
{
if (@file_put_contents($this->getObjectPath($key), $blob) === false) {
throw new ObjectStorageException('Unable to save object');
}
return true;
}
/**
* Output directly object content
*
* @access public
* @param string $key
*/
public function output($key)
{
@readfile($this->getObjectPath($key));
}
/**
* Move local file to object storage
*
* @access public
* @param string $filename
* @param string $key
* @return boolean
* @throws ObjectStorageException
*/
public function moveFile($filename, $key)
{
if (@file_put_contents($this->getObjectPath($key), file_get_contents($filename)) === false) {
throw new ObjectStorageException('Unable to upload file');
}
unlink($filename);
return true;
}
/**
* Move uploaded file to object storage
*
* @access public
* @param string $filename
* @param string $key
* @return boolean
*/
public function moveUploadedFile($filename, $key)
{
return $this->moveFile($filename, $key);
}
/**
* Remove object
*
* @access public
* @param string $key
* @return boolean
* @throws ObjectStorageException
*/
public function remove($key)
{
return @unlink($this->getObjectPath($key));
}
/**
* Get object URL
*
* @access private
* @param string $key
* @return string
*/
private function getObjectPath($key)
{
return sprintf('s3://%s/%s%s', $this->bucket, $this->prefix !== '' ? $this->prefix.'/' : '', $key);
}
}