forked from jkapuscik2/design-patterns-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAWSFileStorage.php
More file actions
55 lines (43 loc) · 1.36 KB
/
AWSFileStorage.php
File metadata and controls
55 lines (43 loc) · 1.36 KB
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
<?php
namespace Structural\Adapter;
use Aws\S3\S3Client;
use Aws\Credentials\Credentials;
use Symfony\Component\Dotenv\Dotenv;
class AWSFileStorage implements FileAdapter {
private $client;
private $bucket;
public function __construct () {
$dotenv = new Dotenv();
$dotenv->load('.env');
$this->bucket = getenv('AWS_BUCKET_NAME');
$this->client = new S3Client([
'version' => 'latest',
'region' => getenv('AWS_REGION'),
'credentials' => new Credentials(getenv("AWS_ACCESS_KEY_ID"), getenv("AWS_SECRET_ACCESS_KEY"))
]);
}
public function get (string $name): File {
try {
$file = $this->client->getObject([
'Bucket' => $this->bucket,
'Key' => $name
]);
return new File($name, $file['Body']->getContents());
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
}
public function save (string $path, string $name): void {
$this->client->putObject([
'Bucket' => $this->bucket,
'Key' => $name,
'Body' => file_get_contents($path, "r")
]);
}
public function delete (string $name): void {
$this->client->deleteObject([
'Bucket' => $this->bucket,
'Key' => $name
]);
}
}