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