· 5 years ago · Aug 14, 2020, 03:02 AM
1<?php
2
3namespace Drupal\xlbasedev\Library\Aws;
4
5use Aws\S3\S3Client;
6use Aws\Exception\AwsException;
7
8use Drupal\Core\Site\Settings;
9use Drupal\xlbasedev\Library\FileSystem\Custom as CustomFileSettings;
10
11class S3Library {
12
13 private $apiKey;
14
15 private $secretKey;
16
17 private $bucket;
18
19 private $key;
20
21 private $s3client = null;
22
23 public function __construct() {
24 $this->apiKey = Settings::get("s3fs.access_key","");
25 $this->secretKey = Settings::get("s3fs.secret_key","");
26 $this->bucket = Settings::get("s3fs.bucket_name","");
27 $this->fileSetting = new CustomFileSettings();
28 }
29
30 public function auth() {
31 $this->s3Client = S3Client::factory([
32 'credentials' => [
33 'key' => Settings::get("s3fs.access_key",""),
34 'secret' => Settings::get("s3fs.secret_key","")
35 ],
36 'region' => 'ap-southeast-1',
37 'version' => '2006-03-01'
38 ]);
39
40 return $this;
41 }
42
43 public function getObject($uri) {
44 $key = $this->fileSetting->getPath($uri);
45
46 $this->cmd = $this->s3Client->getCommand('GetObject', [
47 'Bucket' => Settings::get("s3fs.bucket_name",""),
48 'Key' => $key
49 ]);
50
51 return $this;
52 }
53
54 public function putObject($key) {
55
56 $this->cmd = $this->s3Client->getCommand('PutObject', [
57 'Bucket' => Settings::get("s3fs.bucket_name",""),
58 'Key' => $key
59 ]);
60
61 return $this;
62 }
63
64 public function url() {
65 $request = $this->s3Client->createPresignedRequest($this->cmd, '+15 minutes');
66 return (string) $request->getUri();
67 }
68
69}