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