forked from KnpLabs/Gaufrette
/
AclAwareAmazonS3.php
171 lines (143 loc) · 4.05 KB
/
AclAwareAmazonS3.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
167
168
169
170
171
<?php
namespace Gaufrette\Adapter;
use Gaufrette\Adapter;
/**
* Makes the AmazonS3 adapter ACL aware.
*
* @package Gaufrette
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class AclAwareAmazonS3 extends Base
{
protected $delegate;
protected $s3;
protected $bucketName;
protected $aclConstant = \AmazonS3::ACL_PRIVATE;
protected $users = array();
public function __construct(Adapter $delegate, \AmazonS3 $s3, $bucketName)
{
$this->delegate = $delegate;
$this->s3 = $s3;
$this->bucketName = $bucketName;
}
public function setAclConstant($constant)
{
if (!defined($constant = 'AmazonS3::ACL_'.strtoupper($constant))) {
throw new \InvalidArgumentException(sprintf('The ACL constant "%s" does not exist on AmazonS3.', $constant));
}
$this->aclConstant = constant($constant);
}
public function setUsers(array $users)
{
$this->users = array();
foreach ($users as $user) {
if (!isset($user['permission'])) {
throw new \InvalidArgumentException(sprintf('setUsers() expects an array where each item contains a "permission" key, but got %s.', json_encode($user)));
}
if (!defined($constant = 'AmazonS3::GRANT_'.strtoupper($user['permission']))) {
throw new \InvalidArgumentException('The permission must be the suffix for one of the AmazonS3::GRANT_ constants.');
}
$user['permission'] = constant($constant);
if (isset($user['group'])) {
if (!defined($constant = 'AmazonS3::USERS_'.strtoupper($user['group']))) {
throw new \InvalidArgumentException('The group must be the suffix for one of the AmazonS3::USERS_ constants.');
}
$user['id'] = constant($constant);
unset($user['group']);
} else if (!isset($user['id'])) {
throw new \InvalidArgumentException(sprintf('Either "group", or "id" must be set for each user, but got %s.', json_encode($user)));
}
$this->users[] = $user;
}
}
/**
* {@inheritDoc}
*/
public function read($key)
{
return $this->delegate->read($key);
}
/**
* {@inheritDoc}
*/
public function rename($key, $new)
{
$this->delegate->rename($key, $new);
try {
$this->updateAcl($key);
} catch (\Exception $ex) {
$this->delete($key);
throw $ex;
}
}
/**
* {@inheritDoc}
*/
public function write($key, $content, array $metadata = null)
{
$rs = $this->delegate->write($key, $content, $metadata);
try {
$this->updateAcl($key);
return $rs;
} catch (\Exception $ex) {
$this->delete($key);
throw $ex;
}
}
/**
* {@inheritDoc}
*/
public function exists($key)
{
return $this->delegate->exists($key);
}
/**
* {@inheritDoc}
*/
public function mtime($key)
{
return $this->delegate->mtime($key);
}
/**
* {@inheritDoc}
*/
public function checksum($key)
{
return $this->delegate->checksum($key);
}
/**
* {@inheritDoc}
*/
public function keys($prefix = null)
{
return $this->delegate->keys($prefix);
}
/**
* {@inheritDoc}
*/
public function delete($key)
{
$this->delegate->delete($key);
}
/**
* {@inheritDoc}
*/
public function supportsMetadata()
{
return false;
}
protected function getAcl()
{
if (empty($this->users)) {
return $this->aclConstant;
}
return $this->users;
}
private function updateAcl($key)
{
$response = $this->s3->set_object_acl($this->bucketName, $key, $this->getAcl());
if (200 != $response->status) {
throw new \RuntimeException('S3-ACL change failed: '.print_r($response, true));
}
}
}