@@ -30,121 +30,121 @@ |
||
30 | 30 | use OCP\ILogger; |
31 | 31 | |
32 | 32 | trait S3ConnectionTrait { |
33 | - /** @var array */ |
|
34 | - protected $params; |
|
35 | - |
|
36 | - /** @var S3Client */ |
|
37 | - protected $connection; |
|
38 | - |
|
39 | - /** @var string */ |
|
40 | - protected $id; |
|
41 | - |
|
42 | - /** @var string */ |
|
43 | - protected $bucket; |
|
44 | - |
|
45 | - /** @var int */ |
|
46 | - protected $timeout; |
|
47 | - |
|
48 | - protected $test; |
|
49 | - |
|
50 | - protected function parseParams($params) { |
|
51 | - if (empty($params['key']) || empty($params['secret']) || empty($params['bucket'])) { |
|
52 | - throw new \Exception("Access Key, Secret and Bucket have to be configured."); |
|
53 | - } |
|
54 | - |
|
55 | - $this->id = 'amazon::' . $params['bucket']; |
|
56 | - |
|
57 | - $this->test = isset($params['test']); |
|
58 | - $this->bucket = $params['bucket']; |
|
59 | - $this->timeout = !isset($params['timeout']) ? 15 : $params['timeout']; |
|
60 | - $params['region'] = empty($params['region']) ? 'eu-west-1' : $params['region']; |
|
61 | - $params['hostname'] = empty($params['hostname']) ? 's3.' . $params['region'] . '.amazonaws.com' : $params['hostname']; |
|
62 | - if (!isset($params['port']) || $params['port'] === '') { |
|
63 | - $params['port'] = (isset($params['use_ssl']) && $params['use_ssl'] === false) ? 80 : 443; |
|
64 | - } |
|
65 | - $this->params = $params; |
|
66 | - } |
|
67 | - |
|
68 | - |
|
69 | - /** |
|
70 | - * Returns the connection |
|
71 | - * |
|
72 | - * @return S3Client connected client |
|
73 | - * @throws \Exception if connection could not be made |
|
74 | - */ |
|
75 | - protected function getConnection() { |
|
76 | - if (!is_null($this->connection)) { |
|
77 | - return $this->connection; |
|
78 | - } |
|
79 | - |
|
80 | - $scheme = (isset($this->params['use_ssl']) && $this->params['use_ssl'] === false) ? 'http' : 'https'; |
|
81 | - $base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/'; |
|
82 | - |
|
83 | - $options = [ |
|
84 | - 'version' => isset($this->params['version']) ? $this->params['version'] : 'latest', |
|
85 | - 'credentials' => [ |
|
86 | - 'key' => $this->params['key'], |
|
87 | - 'secret' => $this->params['secret'], |
|
88 | - ], |
|
89 | - 'endpoint' => $base_url, |
|
90 | - 'region' => $this->params['region'], |
|
91 | - 'use_path_style_endpoint' => isset($this->params['use_path_style']) ? $this->params['use_path_style'] : false, |
|
92 | - 'signature_provider' => \Aws\or_chain([self::class, 'legacySignatureProvider'], ClientResolver::_default_signature_provider()) |
|
93 | - ]; |
|
94 | - if (isset($this->params['proxy'])) { |
|
95 | - $options['request.options'] = ['proxy' => $this->params['proxy']]; |
|
96 | - } |
|
97 | - if (isset($this->params['legacy_auth']) && $this->params['legacy_auth']) { |
|
98 | - $options['signature_version'] = 'v2'; |
|
99 | - } |
|
100 | - $this->connection = new S3Client($options); |
|
101 | - |
|
102 | - if (!$this->connection->isBucketDnsCompatible($this->bucket)) { |
|
103 | - throw new \Exception("The configured bucket name is invalid: " . $this->bucket); |
|
104 | - } |
|
105 | - |
|
106 | - if (!$this->connection->doesBucketExist($this->bucket)) { |
|
107 | - $logger = \OC::$server->getLogger(); |
|
108 | - try { |
|
109 | - $logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']); |
|
110 | - $this->connection->createBucket(array( |
|
111 | - 'Bucket' => $this->bucket |
|
112 | - )); |
|
113 | - $this->testTimeout(); |
|
114 | - } catch (S3Exception $e) { |
|
115 | - $logger->logException($e, [ |
|
116 | - 'message' => 'Invalid remote storage.', |
|
117 | - 'level' => ILogger::DEBUG, |
|
118 | - 'app' => 'objectstore', |
|
119 | - ]); |
|
120 | - throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage()); |
|
121 | - } |
|
122 | - } |
|
123 | - |
|
124 | - // google cloud's s3 compatibility doesn't like the EncodingType parameter |
|
125 | - if (strpos($base_url, 'storage.googleapis.com')) { |
|
126 | - $this->connection->getHandlerList()->remove('s3.auto_encode'); |
|
127 | - } |
|
128 | - |
|
129 | - return $this->connection; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * when running the tests wait to let the buckets catch up |
|
134 | - */ |
|
135 | - private function testTimeout() { |
|
136 | - if ($this->test) { |
|
137 | - sleep($this->timeout); |
|
138 | - } |
|
139 | - } |
|
140 | - |
|
141 | - public static function legacySignatureProvider($version, $service, $region) { |
|
142 | - switch ($version) { |
|
143 | - case 'v2': |
|
144 | - case 's3': |
|
145 | - return new S3Signature(); |
|
146 | - default: |
|
147 | - return null; |
|
148 | - } |
|
149 | - } |
|
33 | + /** @var array */ |
|
34 | + protected $params; |
|
35 | + |
|
36 | + /** @var S3Client */ |
|
37 | + protected $connection; |
|
38 | + |
|
39 | + /** @var string */ |
|
40 | + protected $id; |
|
41 | + |
|
42 | + /** @var string */ |
|
43 | + protected $bucket; |
|
44 | + |
|
45 | + /** @var int */ |
|
46 | + protected $timeout; |
|
47 | + |
|
48 | + protected $test; |
|
49 | + |
|
50 | + protected function parseParams($params) { |
|
51 | + if (empty($params['key']) || empty($params['secret']) || empty($params['bucket'])) { |
|
52 | + throw new \Exception("Access Key, Secret and Bucket have to be configured."); |
|
53 | + } |
|
54 | + |
|
55 | + $this->id = 'amazon::' . $params['bucket']; |
|
56 | + |
|
57 | + $this->test = isset($params['test']); |
|
58 | + $this->bucket = $params['bucket']; |
|
59 | + $this->timeout = !isset($params['timeout']) ? 15 : $params['timeout']; |
|
60 | + $params['region'] = empty($params['region']) ? 'eu-west-1' : $params['region']; |
|
61 | + $params['hostname'] = empty($params['hostname']) ? 's3.' . $params['region'] . '.amazonaws.com' : $params['hostname']; |
|
62 | + if (!isset($params['port']) || $params['port'] === '') { |
|
63 | + $params['port'] = (isset($params['use_ssl']) && $params['use_ssl'] === false) ? 80 : 443; |
|
64 | + } |
|
65 | + $this->params = $params; |
|
66 | + } |
|
67 | + |
|
68 | + |
|
69 | + /** |
|
70 | + * Returns the connection |
|
71 | + * |
|
72 | + * @return S3Client connected client |
|
73 | + * @throws \Exception if connection could not be made |
|
74 | + */ |
|
75 | + protected function getConnection() { |
|
76 | + if (!is_null($this->connection)) { |
|
77 | + return $this->connection; |
|
78 | + } |
|
79 | + |
|
80 | + $scheme = (isset($this->params['use_ssl']) && $this->params['use_ssl'] === false) ? 'http' : 'https'; |
|
81 | + $base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/'; |
|
82 | + |
|
83 | + $options = [ |
|
84 | + 'version' => isset($this->params['version']) ? $this->params['version'] : 'latest', |
|
85 | + 'credentials' => [ |
|
86 | + 'key' => $this->params['key'], |
|
87 | + 'secret' => $this->params['secret'], |
|
88 | + ], |
|
89 | + 'endpoint' => $base_url, |
|
90 | + 'region' => $this->params['region'], |
|
91 | + 'use_path_style_endpoint' => isset($this->params['use_path_style']) ? $this->params['use_path_style'] : false, |
|
92 | + 'signature_provider' => \Aws\or_chain([self::class, 'legacySignatureProvider'], ClientResolver::_default_signature_provider()) |
|
93 | + ]; |
|
94 | + if (isset($this->params['proxy'])) { |
|
95 | + $options['request.options'] = ['proxy' => $this->params['proxy']]; |
|
96 | + } |
|
97 | + if (isset($this->params['legacy_auth']) && $this->params['legacy_auth']) { |
|
98 | + $options['signature_version'] = 'v2'; |
|
99 | + } |
|
100 | + $this->connection = new S3Client($options); |
|
101 | + |
|
102 | + if (!$this->connection->isBucketDnsCompatible($this->bucket)) { |
|
103 | + throw new \Exception("The configured bucket name is invalid: " . $this->bucket); |
|
104 | + } |
|
105 | + |
|
106 | + if (!$this->connection->doesBucketExist($this->bucket)) { |
|
107 | + $logger = \OC::$server->getLogger(); |
|
108 | + try { |
|
109 | + $logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']); |
|
110 | + $this->connection->createBucket(array( |
|
111 | + 'Bucket' => $this->bucket |
|
112 | + )); |
|
113 | + $this->testTimeout(); |
|
114 | + } catch (S3Exception $e) { |
|
115 | + $logger->logException($e, [ |
|
116 | + 'message' => 'Invalid remote storage.', |
|
117 | + 'level' => ILogger::DEBUG, |
|
118 | + 'app' => 'objectstore', |
|
119 | + ]); |
|
120 | + throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage()); |
|
121 | + } |
|
122 | + } |
|
123 | + |
|
124 | + // google cloud's s3 compatibility doesn't like the EncodingType parameter |
|
125 | + if (strpos($base_url, 'storage.googleapis.com')) { |
|
126 | + $this->connection->getHandlerList()->remove('s3.auto_encode'); |
|
127 | + } |
|
128 | + |
|
129 | + return $this->connection; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * when running the tests wait to let the buckets catch up |
|
134 | + */ |
|
135 | + private function testTimeout() { |
|
136 | + if ($this->test) { |
|
137 | + sleep($this->timeout); |
|
138 | + } |
|
139 | + } |
|
140 | + |
|
141 | + public static function legacySignatureProvider($version, $service, $region) { |
|
142 | + switch ($version) { |
|
143 | + case 'v2': |
|
144 | + case 's3': |
|
145 | + return new S3Signature(); |
|
146 | + default: |
|
147 | + return null; |
|
148 | + } |
|
149 | + } |
|
150 | 150 | } |