Skip to content
This repository was archived by the owner on Oct 13, 2019. It is now read-only.

Commit b8517ed

Browse files
author
helpfulrobot
committed
Converted to PSR-2
1 parent 4548232 commit b8517ed

File tree

2 files changed

+201
-175
lines changed

2 files changed

+201
-175
lines changed

code/S3Bucket.php

Lines changed: 168 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -15,151 +15,172 @@
1515

1616
class S3Bucket extends CloudBucket
1717
{
18-
const CONTAINER = 'Container';
19-
const REGION = 'Region';
20-
const API_KEY = 'ApiKey';
21-
const API_SECRET = 'ApiSecret';
22-
const FORCE_DL = 'ForceDownload';
23-
24-
protected $client;
25-
26-
27-
/**
28-
* @param string $path
29-
* @param array $cfg
30-
* @throws Exception
31-
*/
32-
public function __construct($path, array $cfg=array()) {
33-
parent::__construct($path, $cfg);
34-
if (empty($cfg[self::CONTAINER])) throw new Exception('S3Bucket: missing configuration key - ' . self::CONTAINER);
35-
if (empty($cfg[self::REGION])) throw new Exception('S3Bucket: missing configuration key - ' . self::REGION);
36-
if (empty($cfg[self::API_KEY])) throw new Exception('S3Bucket: missing configuration key - ' . self::API_KEY);
37-
if (empty($cfg[self::API_SECRET])) throw new Exception('S3Bucket: missing configuration key - ' . self::API_SECRET);
38-
$this->containerName = $this->config[self::CONTAINER];
39-
40-
$this->client = S3Client::factory(array(
41-
'key' => $this->config[self::API_KEY],
42-
'secret' => $this->config[self::API_SECRET],
43-
'region' => $this->config[self::REGION]
44-
));
45-
}
46-
47-
48-
/**
49-
* @param File $f
50-
* @throws Exception
51-
*/
52-
public function put(File $f) {
53-
$fp = fopen($f->getFullPath(), 'r');
54-
if (!$fp) throw new Exception('Unable to open file: ' . $f->getFilename());
55-
56-
$uploader = UploadBuilder::newInstance()
57-
->setClient($this->client)
58-
->setSource($f->getFullPath())
59-
->setBucket($this->containerName)
60-
->setKey($this->getRelativeLinkFor($f))
61-
->build();
62-
63-
try {
64-
$uploader->upload();
65-
} catch (MultipartUploadException $e) {
66-
$uploader->abort();
67-
}
68-
}
69-
70-
71-
/**
72-
* @param File|string $f
73-
*/
74-
public function delete($f) {
75-
$this->client->deleteObject(array(
76-
'Bucket' => $this->containerName,
77-
'Key' => $this->getRelativeLinkFor($f),
78-
));
79-
}
80-
81-
/**
82-
* @param File $f
83-
* @param string $beforeName - contents of the Filename property (i.e. relative to site root)
84-
* @param string $afterName - contents of the Filename property (i.e. relative to site root)
85-
*/
86-
public function rename(File $f, $beforeName, $afterName) {
87-
$obj = $this->getFileObjectFor($beforeName);
88-
$result = $this->client->copyObject(array(
89-
'Bucket' => $this->containerName,
90-
'CopySource' => urlencode($this->containerName . '/' . $this->getRelativeLinkFor($beforeName)),
91-
'Key' => $this->getRelativeLinkFor($afterName),
92-
));
93-
if($result) $this->client->deleteObject(array(
94-
'Bucket' => $this->containerName,
95-
'Key' => $this->getRelativeLinkFor($beforeName),
96-
));
97-
}
98-
99-
100-
/**
101-
* @param File $f
102-
* @return string
103-
*/
104-
public function getContents(File $f) {
105-
$obj = $this->getFileObjectFor($f);
106-
return $obj['Body'];
107-
}
108-
109-
110-
/**
111-
* This version just returns a normal link. I'm assuming most
112-
* buckets will implement this but I want it to be optional.
113-
* NOTE: I'm not sure how reliably this is working.
114-
*
115-
* @param File|string $f
116-
* @param int $expires [optional] - Expiration time in seconds
117-
* @return string
118-
*/
119-
public function getTemporaryLinkFor($f, $expires=3600) {
120-
$obj = $this->getFileObjectFor($this->getRelativeLinkFor($f));
121-
return $obj['Body']->getUri();
122-
}
123-
124-
125-
/**
126-
* @param $f - File object or filename
127-
* @return bool
128-
*/
129-
public function checkExists(File $f) {
130-
return $this->client->doesObjectExist(
131-
$this->containerName,
132-
$this->getRelativeLinkFor($f)
133-
);
134-
}
135-
136-
137-
/**
138-
* @param $f - File object or filename
139-
* @return int - if file doesn't exist, returns -1
140-
*/
141-
public function getFileSize(File $f) {
142-
if($obj = $this->getFileObjectFor($f)) {
143-
return $obj['ContentLength'];
144-
} else {
145-
return -1;
146-
}
147-
}
148-
149-
150-
/**
151-
* @param File|string $f
152-
* @return \Guzzle\Http\EntityBody
153-
*/
154-
protected function getFileObjectFor(File $f) {
155-
try {
156-
$result = $this->client->getObject(array(
157-
'Bucket' => $this->containerName,
158-
'Key' => $this->getRelativeLinkFor($f)
159-
));
160-
return $result;
161-
} catch (\Aws\S3\Exception\NoSuchKeyException $e) {
162-
return -1;
163-
}
164-
}
18+
const CONTAINER = 'Container';
19+
const REGION = 'Region';
20+
const API_KEY = 'ApiKey';
21+
const API_SECRET = 'ApiSecret';
22+
const FORCE_DL = 'ForceDownload';
23+
24+
protected $client;
25+
26+
27+
/**
28+
* @param string $path
29+
* @param array $cfg
30+
* @throws Exception
31+
*/
32+
public function __construct($path, array $cfg=array())
33+
{
34+
parent::__construct($path, $cfg);
35+
if (empty($cfg[self::CONTAINER])) {
36+
throw new Exception('S3Bucket: missing configuration key - ' . self::CONTAINER);
37+
}
38+
if (empty($cfg[self::REGION])) {
39+
throw new Exception('S3Bucket: missing configuration key - ' . self::REGION);
40+
}
41+
if (empty($cfg[self::API_KEY])) {
42+
throw new Exception('S3Bucket: missing configuration key - ' . self::API_KEY);
43+
}
44+
if (empty($cfg[self::API_SECRET])) {
45+
throw new Exception('S3Bucket: missing configuration key - ' . self::API_SECRET);
46+
}
47+
$this->containerName = $this->config[self::CONTAINER];
48+
49+
$this->client = S3Client::factory(array(
50+
'key' => $this->config[self::API_KEY],
51+
'secret' => $this->config[self::API_SECRET],
52+
'region' => $this->config[self::REGION]
53+
));
54+
}
55+
56+
57+
/**
58+
* @param File $f
59+
* @throws Exception
60+
*/
61+
public function put(File $f)
62+
{
63+
$fp = fopen($f->getFullPath(), 'r');
64+
if (!$fp) {
65+
throw new Exception('Unable to open file: ' . $f->getFilename());
66+
}
67+
68+
$uploader = UploadBuilder::newInstance()
69+
->setClient($this->client)
70+
->setSource($f->getFullPath())
71+
->setBucket($this->containerName)
72+
->setKey($this->getRelativeLinkFor($f))
73+
->build();
74+
75+
try {
76+
$uploader->upload();
77+
} catch (MultipartUploadException $e) {
78+
$uploader->abort();
79+
}
80+
}
81+
82+
83+
/**
84+
* @param File|string $f
85+
*/
86+
public function delete($f)
87+
{
88+
$this->client->deleteObject(array(
89+
'Bucket' => $this->containerName,
90+
'Key' => $this->getRelativeLinkFor($f),
91+
));
92+
}
93+
94+
/**
95+
* @param File $f
96+
* @param string $beforeName - contents of the Filename property (i.e. relative to site root)
97+
* @param string $afterName - contents of the Filename property (i.e. relative to site root)
98+
*/
99+
public function rename(File $f, $beforeName, $afterName)
100+
{
101+
$obj = $this->getFileObjectFor($beforeName);
102+
$result = $this->client->copyObject(array(
103+
'Bucket' => $this->containerName,
104+
'CopySource' => urlencode($this->containerName . '/' . $this->getRelativeLinkFor($beforeName)),
105+
'Key' => $this->getRelativeLinkFor($afterName),
106+
));
107+
if ($result) {
108+
$this->client->deleteObject(array(
109+
'Bucket' => $this->containerName,
110+
'Key' => $this->getRelativeLinkFor($beforeName),
111+
));
112+
}
113+
}
114+
115+
116+
/**
117+
* @param File $f
118+
* @return string
119+
*/
120+
public function getContents(File $f)
121+
{
122+
$obj = $this->getFileObjectFor($f);
123+
return $obj['Body'];
124+
}
125+
126+
127+
/**
128+
* This version just returns a normal link. I'm assuming most
129+
* buckets will implement this but I want it to be optional.
130+
* NOTE: I'm not sure how reliably this is working.
131+
*
132+
* @param File|string $f
133+
* @param int $expires [optional] - Expiration time in seconds
134+
* @return string
135+
*/
136+
public function getTemporaryLinkFor($f, $expires=3600)
137+
{
138+
$obj = $this->getFileObjectFor($this->getRelativeLinkFor($f));
139+
return $obj['Body']->getUri();
140+
}
141+
142+
143+
/**
144+
* @param $f - File object or filename
145+
* @return bool
146+
*/
147+
public function checkExists(File $f)
148+
{
149+
return $this->client->doesObjectExist(
150+
$this->containerName,
151+
$this->getRelativeLinkFor($f)
152+
);
153+
}
154+
155+
156+
/**
157+
* @param $f - File object or filename
158+
* @return int - if file doesn't exist, returns -1
159+
*/
160+
public function getFileSize(File $f)
161+
{
162+
if ($obj = $this->getFileObjectFor($f)) {
163+
return $obj['ContentLength'];
164+
} else {
165+
return -1;
166+
}
167+
}
168+
169+
170+
/**
171+
* @param File|string $f
172+
* @return \Guzzle\Http\EntityBody
173+
*/
174+
protected function getFileObjectFor(File $f)
175+
{
176+
try {
177+
$result = $this->client->getObject(array(
178+
'Bucket' => $this->containerName,
179+
'Key' => $this->getRelativeLinkFor($f)
180+
));
181+
return $result;
182+
} catch (\Aws\S3\Exception\NoSuchKeyException $e) {
183+
return -1;
184+
}
185+
}
165186
}

tests/CloudAssetsS3Test.php

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,37 @@
44
* Requires a mapping in your config that includes assets/Uploads
55
*/
66

7-
class CloudAssetsS3Test extends SapphireTest {
8-
9-
private $bucket;
10-
11-
function setUp() {
12-
$this->bucket = CloudAssets::inst()->map('assets/Uploads/CloudAssetsS3Test.txt');
13-
parent::setUpOnce();
14-
}
15-
16-
function testInstantiateBucket() {
17-
$this->assertInstanceOf('S3Bucket', $this->bucket);
18-
}
19-
20-
function testGetFile() {
21-
if(!$this->bucket) $this->fail('Cannot continue without valid bucket');
22-
23-
$file = new File();
24-
$file->setName('S3ConnectionTest.txt');
25-
try {
26-
$size = $this->bucket->getFileSize($file);
27-
} catch(Exception $e) {
28-
$error = true;
29-
echo $e->getMessage();
30-
}
31-
32-
$this->assertFalse(isset($error));
33-
}
34-
7+
class CloudAssetsS3Test extends SapphireTest
8+
{
9+
10+
private $bucket;
11+
12+
public function setUp()
13+
{
14+
$this->bucket = CloudAssets::inst()->map('assets/Uploads/CloudAssetsS3Test.txt');
15+
parent::setUpOnce();
16+
}
17+
18+
public function testInstantiateBucket()
19+
{
20+
$this->assertInstanceOf('S3Bucket', $this->bucket);
21+
}
22+
23+
public function testGetFile()
24+
{
25+
if (!$this->bucket) {
26+
$this->fail('Cannot continue without valid bucket');
27+
}
28+
29+
$file = new File();
30+
$file->setName('S3ConnectionTest.txt');
31+
try {
32+
$size = $this->bucket->getFileSize($file);
33+
} catch (Exception $e) {
34+
$error = true;
35+
echo $e->getMessage();
36+
}
37+
38+
$this->assertFalse(isset($error));
39+
}
3540
}

0 commit comments

Comments
 (0)