@@ -30,124 +30,124 @@ |
||
| 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 | - $logger = \OC::$server->getLogger(); |
|
| 104 | - $logger->debug('Bucket "' . $this->bucket . '" This bucket name is not dns compatible, it may contain invalid characters.', |
|
| 105 | - ['app' => 'objectstore']); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - if (!$this->connection->doesBucketExist($this->bucket)) { |
|
| 109 | - $logger = \OC::$server->getLogger(); |
|
| 110 | - try { |
|
| 111 | - $logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']); |
|
| 112 | - if (!$this->connection->isBucketDnsCompatible($this->bucket)) { |
|
| 113 | - throw new \Exception("The bucket will not be created because the name is not dns compatible, please correct it: " . $this->bucket); |
|
| 114 | - } |
|
| 115 | - $this->connection->createBucket(array('Bucket' => $this->bucket)); |
|
| 116 | - $this->testTimeout(); |
|
| 117 | - } catch (S3Exception $e) { |
|
| 118 | - $logger->logException($e, [ |
|
| 119 | - 'message' => 'Invalid remote storage.', |
|
| 120 | - 'level' => ILogger::DEBUG, |
|
| 121 | - 'app' => 'objectstore', |
|
| 122 | - ]); |
|
| 123 | - throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage()); |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - // google cloud's s3 compatibility doesn't like the EncodingType parameter |
|
| 128 | - if (strpos($base_url, 'storage.googleapis.com')) { |
|
| 129 | - $this->connection->getHandlerList()->remove('s3.auto_encode'); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - return $this->connection; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * when running the tests wait to let the buckets catch up |
|
| 137 | - */ |
|
| 138 | - private function testTimeout() { |
|
| 139 | - if ($this->test) { |
|
| 140 | - sleep($this->timeout); |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - public static function legacySignatureProvider($version, $service, $region) { |
|
| 145 | - switch ($version) { |
|
| 146 | - case 'v2': |
|
| 147 | - case 's3': |
|
| 148 | - return new S3Signature(); |
|
| 149 | - default: |
|
| 150 | - return null; |
|
| 151 | - } |
|
| 152 | - } |
|
| 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 | + $logger = \OC::$server->getLogger(); |
|
| 104 | + $logger->debug('Bucket "' . $this->bucket . '" This bucket name is not dns compatible, it may contain invalid characters.', |
|
| 105 | + ['app' => 'objectstore']); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + if (!$this->connection->doesBucketExist($this->bucket)) { |
|
| 109 | + $logger = \OC::$server->getLogger(); |
|
| 110 | + try { |
|
| 111 | + $logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']); |
|
| 112 | + if (!$this->connection->isBucketDnsCompatible($this->bucket)) { |
|
| 113 | + throw new \Exception("The bucket will not be created because the name is not dns compatible, please correct it: " . $this->bucket); |
|
| 114 | + } |
|
| 115 | + $this->connection->createBucket(array('Bucket' => $this->bucket)); |
|
| 116 | + $this->testTimeout(); |
|
| 117 | + } catch (S3Exception $e) { |
|
| 118 | + $logger->logException($e, [ |
|
| 119 | + 'message' => 'Invalid remote storage.', |
|
| 120 | + 'level' => ILogger::DEBUG, |
|
| 121 | + 'app' => 'objectstore', |
|
| 122 | + ]); |
|
| 123 | + throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage()); |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + // google cloud's s3 compatibility doesn't like the EncodingType parameter |
|
| 128 | + if (strpos($base_url, 'storage.googleapis.com')) { |
|
| 129 | + $this->connection->getHandlerList()->remove('s3.auto_encode'); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + return $this->connection; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * when running the tests wait to let the buckets catch up |
|
| 137 | + */ |
|
| 138 | + private function testTimeout() { |
|
| 139 | + if ($this->test) { |
|
| 140 | + sleep($this->timeout); |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + public static function legacySignatureProvider($version, $service, $region) { |
|
| 145 | + switch ($version) { |
|
| 146 | + case 'v2': |
|
| 147 | + case 's3': |
|
| 148 | + return new S3Signature(); |
|
| 149 | + default: |
|
| 150 | + return null; |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | 153 | } |
@@ -52,13 +52,13 @@ discard block |
||
| 52 | 52 | throw new \Exception("Access Key, Secret and Bucket have to be configured."); |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | - $this->id = 'amazon::' . $params['bucket']; |
|
| 55 | + $this->id = 'amazon::'.$params['bucket']; |
|
| 56 | 56 | |
| 57 | 57 | $this->test = isset($params['test']); |
| 58 | 58 | $this->bucket = $params['bucket']; |
| 59 | 59 | $this->timeout = !isset($params['timeout']) ? 15 : $params['timeout']; |
| 60 | 60 | $params['region'] = empty($params['region']) ? 'eu-west-1' : $params['region']; |
| 61 | - $params['hostname'] = empty($params['hostname']) ? 's3.' . $params['region'] . '.amazonaws.com' : $params['hostname']; |
|
| 61 | + $params['hostname'] = empty($params['hostname']) ? 's3.'.$params['region'].'.amazonaws.com' : $params['hostname']; |
|
| 62 | 62 | if (!isset($params['port']) || $params['port'] === '') { |
| 63 | 63 | $params['port'] = (isset($params['use_ssl']) && $params['use_ssl'] === false) ? 80 : 443; |
| 64 | 64 | } |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | $scheme = (isset($this->params['use_ssl']) && $this->params['use_ssl'] === false) ? 'http' : 'https'; |
| 81 | - $base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/'; |
|
| 81 | + $base_url = $scheme.'://'.$this->params['hostname'].':'.$this->params['port'].'/'; |
|
| 82 | 82 | |
| 83 | 83 | $options = [ |
| 84 | 84 | 'version' => isset($this->params['version']) ? $this->params['version'] : 'latest', |
@@ -101,16 +101,16 @@ discard block |
||
| 101 | 101 | |
| 102 | 102 | if (!$this->connection->isBucketDnsCompatible($this->bucket)) { |
| 103 | 103 | $logger = \OC::$server->getLogger(); |
| 104 | - $logger->debug('Bucket "' . $this->bucket . '" This bucket name is not dns compatible, it may contain invalid characters.', |
|
| 104 | + $logger->debug('Bucket "'.$this->bucket.'" This bucket name is not dns compatible, it may contain invalid characters.', |
|
| 105 | 105 | ['app' => 'objectstore']); |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | if (!$this->connection->doesBucketExist($this->bucket)) { |
| 109 | 109 | $logger = \OC::$server->getLogger(); |
| 110 | 110 | try { |
| 111 | - $logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']); |
|
| 111 | + $logger->info('Bucket "'.$this->bucket.'" does not exist - creating it.', ['app' => 'objectstore']); |
|
| 112 | 112 | if (!$this->connection->isBucketDnsCompatible($this->bucket)) { |
| 113 | - throw new \Exception("The bucket will not be created because the name is not dns compatible, please correct it: " . $this->bucket); |
|
| 113 | + throw new \Exception("The bucket will not be created because the name is not dns compatible, please correct it: ".$this->bucket); |
|
| 114 | 114 | } |
| 115 | 115 | $this->connection->createBucket(array('Bucket' => $this->bucket)); |
| 116 | 116 | $this->testTimeout(); |
@@ -120,7 +120,7 @@ discard block |
||
| 120 | 120 | 'level' => ILogger::DEBUG, |
| 121 | 121 | 'app' => 'objectstore', |
| 122 | 122 | ]); |
| 123 | - throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage()); |
|
| 123 | + throw new \Exception('Creation of bucket "'.$this->bucket.'" failed. '.$e->getMessage()); |
|
| 124 | 124 | } |
| 125 | 125 | } |
| 126 | 126 | |