/
Bucket.php
72 lines (63 loc) · 1.67 KB
/
Bucket.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
<?php namespace GitS3\Wrapper;
use Aws\S3\S3Client;
use Aws\S3\Enum\CannedAcl;
use Symfony\Component\Finder\SplFileInfo as File;
use Aws\S3\Exception\InvalidAccessKeyIdException;
use Aws\Common\Exception\InstanceProfileCredentialsException;
class Bucket
{
private $client;
private $name;
public function __construct($key, $secret, $name, $region = 'eu-west-1')
{
$this->name = $name;
//Check for valid keys
$validKey = preg_match("/^(?<![A-Z0-9])[A-Z0-9]{20}(?![A-Z0-9])$/", $key);
$validSecret = preg_match("/^(?<![A-Za-z0-9\/+=])[A-Za-z0-9\/+=]{40}(?![A-Za-z0-9\/+=])$/", $secret);
//If keys are not valid, try a role.
if(!$validKey || !$validSecret)
$this->client = S3Client::factory(array(
'region' => $region,
));
else
$this->client = S3Client::factory(array(
'key' => $key,
'secret' => $secret,
'region' => $region,
));
}
public function upload(File $file)
{
try
{
$this->client->putObject(array(
'Bucket' => $this->name,
'Key' => $file->getRelativePathname(),
'SourceFile' => $file->getRealpath(),
'ACL' => CannedAcl::PUBLIC_READ,
));
}
catch(InstanceProfileCredentialsException $e)
{
throw new InvalidAccessKeyIdException("The AWS Access Key Id you provided does not exist in our records.");
}
}
public function delete($fileName)
{
try
{
$this->client->deleteObject(array(
'Bucket' => $this->name,
'Key' => $fileName,
));
}
catch(InstanceProfileCredentialsException $e)
{
throw new InvalidAccessKeyIdException("The AWS Access Key Id you provided does not exist in our records.");
}
}
public function setClient($client)
{
$this->client = $client;
}
}