1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the limit0/assets package. |
5
|
|
|
* |
6
|
|
|
* (c) Limit Zero, LLC <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Limit0\Assets\StorageEngine; |
13
|
|
|
|
14
|
|
|
use Aws\S3\S3Client; |
15
|
|
|
use Aws\S3\Exception\S3Exception; |
16
|
|
|
use Limit0\Assets\Asset; |
17
|
|
|
use Limit0\Assets\AssetFactory; |
18
|
|
|
use Limit0\Assets\Exception\StorageException; |
19
|
|
|
use Limit0\Assets\StorageEngineInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* This class supports storing, retrieve, and deleting files to Amazon S3 storage. |
23
|
|
|
* At the moment, no additional storage parameters can be set -- it's best practice |
24
|
|
|
* to make these the defaults for your bucket to prevent objects being out of sync |
25
|
|
|
* with your AWS policies. |
26
|
|
|
* |
27
|
|
|
* Per AWS best practices, this passes through the authentication of requests to |
28
|
|
|
* the AWS SDK itself, for more info: |
29
|
|
|
* @see http://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/credentials.html#environment-credentials |
30
|
|
|
* |
31
|
|
|
* @see setBucket -- bucket must be set when configuring this storage engine. |
32
|
|
|
* |
33
|
|
|
* @author Josh Worden <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
class AmazonS3StorageEngine implements StorageEngineInterface |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $bucket; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var S3Client |
44
|
|
|
*/ |
45
|
|
|
private $client; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private $region = 'us-east-1'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function getBucket() |
56
|
|
|
{ |
57
|
|
|
if (null === $this->bucket) { |
58
|
|
|
throw StorageException::invalidConfiguration('bucket'); |
59
|
|
|
} |
60
|
|
|
return $this->bucket; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Instantiates and returns an S3Client instance |
65
|
|
|
* @return S3Client |
66
|
|
|
*/ |
67
|
|
|
public function getClient() |
68
|
|
|
{ |
69
|
|
|
if (null === $this->client) { |
70
|
|
|
$this->client = S3Client::factory([ |
|
|
|
|
71
|
|
|
'version' => 'latest', |
72
|
|
|
'region' => $this->getRegion() |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
return $this->client; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getRegion() |
82
|
|
|
{ |
83
|
|
|
return $this->region; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function remove($identifier) |
90
|
|
|
{ |
91
|
|
|
try { |
92
|
|
|
$this->getClient()->deleteObject([ |
93
|
|
|
'Bucket' => $this->getBucket(), |
94
|
|
|
'Key' => $identifier |
95
|
|
|
]); |
96
|
|
|
} catch (S3Exception $e) { |
97
|
|
|
throw new StorageException($e->getMessage()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return true; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
public function retrieve($identifier) |
107
|
|
|
{ |
108
|
|
|
$path = explode('/', $identifier); |
109
|
|
|
$fileName = array_pop($path); |
110
|
|
|
$localPath = sprintf('%s/%s', sys_get_temp_dir(), $fileName); |
111
|
|
|
|
112
|
|
|
try { |
113
|
|
|
$this->getClient()->getObject([ |
114
|
|
|
'Bucket' => $this->getBucket(), |
115
|
|
|
'Key' => $identifier, |
116
|
|
|
'SaveAs' => $localPath |
117
|
|
|
]); |
118
|
|
|
|
119
|
|
|
$asset = AssetFactory::createFromPath($localPath); |
120
|
|
|
$asset->setFilename($fileName)->setFilepath(implode('/', $path)); |
121
|
|
|
return $asset; |
122
|
|
|
|
123
|
|
|
} catch (S3Exception $e) { |
124
|
|
|
throw new StorageException($e->getMessage()); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Sets the bucket the assets should be stored to. |
130
|
|
|
* |
131
|
|
|
* @param string |
132
|
|
|
*/ |
133
|
|
|
public function setBucket($bucket) |
134
|
|
|
{ |
135
|
|
|
$this->bucket = $bucket; |
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Override the S3 region to connect to |
141
|
|
|
* @param string |
142
|
|
|
*/ |
143
|
|
|
public function setRegion($region) |
144
|
|
|
{ |
145
|
|
|
$this->region = $region; |
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
|
|
public function store(Asset $asset, $path = null, $filename = null) |
153
|
|
|
{ |
154
|
|
|
$filename = (null === $filename) ? $asset->getBasename() : $filename; |
155
|
|
|
$key = sprintf('%s/%s', $path, $filename); |
156
|
|
|
|
157
|
|
|
try { |
158
|
|
|
$this->getClient()->putObject([ |
159
|
|
|
'Bucket' => $this->getBucket(), |
160
|
|
|
'Key' => $key, |
161
|
|
|
'Body' => fopen($asset->getPathname(), 'r') |
162
|
|
|
]); |
163
|
|
|
|
164
|
|
|
} catch (S3Exception $e) { |
165
|
|
|
throw new StorageException($e->getMessage()); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return true; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
This method has been deprecated.