/
S3ClientTest.php
135 lines (113 loc) · 4.95 KB
/
S3ClientTest.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
<?php
/*
---------------------------------------------------------------------
Copyright (c) 2007-2013 Basho Technologies, Inc. All Rights Reserved.
This file is provided to you under the Apache License,
Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain
a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
---------------------------------------------------------------------
*/
use Guzzle\Plugin\Backoff\BackoffPlugin;
// http://docs.amazonwebservices.com/aws-sdk-php-2/latest/class-Aws.S3.S3Client.html
class S3ClientTest extends \Guzzle\Tests\GuzzleTestCase
{
const ALL_USER_URL = "http://acs.amazonaws.com/groups/global/AllUsers";
public function setUp()
{
$this->client = $this->getServiceBuilder()->get('s3');
$this->client->getEventDispatcher()->removeSubscriber(BackoffPlugin::getExponentialBackoff()); // disable retry
$this->bucket = randBucket();
}
public function tearDown()
{
if ( ! $this->client->doesBucketExist($this->bucket)) { return; }
$objectKeys = $this->client->listObjects(array('Bucket' => $this->bucket))['Contents'];
foreach ($objectKeys as $key) {
$this->client->deleteObject(array('Bucket' => $this->bucket, 'Key' => $key['Key']));
}
$this->client->deleteBucket(array('Bucket' => $this->bucket));
}
public function testBucketNotExists()
{
$this->assertFalse($this->client->doesBucketExist($this->bucket));
try {
$this->client->getBucketAcl(array('Bucket' => $this->bucket))['Grants'];
$this->fail();
} catch (Aws\S3\Exception\S3Exception $e) { /* noop */ }
try {
$this->client->getObject(array('Bucket' => $this->bucket, 'Key' => randKey()));
$this->fail();
} catch (Aws\S3\Exception\S3Exception $e) { /* noop */ }
}
public function testCreateDeleteBucket()
{
$this->client->createBucket(array('Bucket' => $this->bucket));
$this->assertTrue($this->client->doesBucketExist($this->bucket));
$this->client->deleteBucket(array('Bucket' => $this->bucket));
$this->assertFalse($this->client->doesBucketExist($this->bucket));
}
public function testPutGetDeleteObject()
{
$this->client->createBucket(array('Bucket' => $this->bucket));
$key = randKey();
$this->client->putObject(
array(
'Bucket' => $this->bucket,
'Key' => $key,
'Content-Type' => 'text/plain',
'Body' => "This is a entity body."));
$this->assertTrue($this->client->doesObjectExist($this->bucket, $key));
$object = $this->client->getObject(array('Bucket' => $this->bucket, 'Key' => $key));
$this->assertEquals('This is a entity body.',(string)$object['Body']);
$this->client->deleteObject(array('Bucket' => $this->bucket, 'Key' => $key));
$this->assertFalse($this->client->doesObjectExist($this->bucket, $key));
}
public function testBucketACL()
{
$this->client->createBucket(array('Bucket' => $this->bucket));
$this->client->putBucketAcl(array('Bucket' => $this->bucket, 'ACL' => 'public-read'));
$grants = $this->client->getBucketAcl(array('Bucket' => $this->bucket))['Grants'];
$filtered_grants = array_filter($grants, function($item){
return array_key_exists('URI', $item['Grantee'])
&& $item['Grantee']['URI'] == self::ALL_USER_URL
&& $item['Permission'] == 'READ';
});
$this->assertGreaterThanOrEqual(1, sizeof($filtered_grants));
}
public function testObjectACL()
{
$this->client->createBucket(array('Bucket' => $this->bucket));
$key = randKey();
$this->client->putObject(
array(
'Bucket' => $this->bucket,
'Key' => $key,
'Content-Type' => 'text/plain',
'Body' => "This is a entity body."));
$this->client->putObjectAcl(array('Bucket' => $this->bucket, 'Key' => $key,'ACL' => 'public-read'));
$grants = $this->client->getObjectAcl(array('Bucket' => $this->bucket, 'Key' => $key))['Grants'];
$filtered_grants = array_filter($grants, function($item){
return array_key_exists('URI', $item['Grantee'])
&& $item['Grantee']['URI'] == self::ALL_USER_URL
&& $item['Permission'] == 'READ';
});
$this->assertGreaterThanOrEqual(1, sizeof($filtered_grants));
}
}
function randBucket()
{
return uniqid('aws-sdk-test-');
}
function randKey()
{
return uniqid('key-');
}
?>