Passed
Push — master ( 3f4fb9...9e2ced )
by Morris
13:23 queued 11s
created
lib/private/Files/ObjectStore/S3ConnectionTrait.php 2 patches
Indentation   +155 added lines, -155 removed lines patch added patch discarded remove patch
@@ -40,159 +40,159 @@
 block discarded – undo
40 40
 use OCP\ILogger;
41 41
 
42 42
 trait S3ConnectionTrait {
43
-	/** @var array */
44
-	protected $params;
45
-
46
-	/** @var S3Client */
47
-	protected $connection;
48
-
49
-	/** @var string */
50
-	protected $id;
51
-
52
-	/** @var string */
53
-	protected $bucket;
54
-
55
-	/** @var int */
56
-	protected $timeout;
57
-
58
-	/** @var int */
59
-	protected $uploadPartSize;
60
-
61
-	protected $test;
62
-
63
-	protected function parseParams($params) {
64
-		if (empty($params['bucket'])) {
65
-			throw new \Exception("Bucket has to be configured.");
66
-		}
67
-
68
-		$this->id = 'amazon::' . $params['bucket'];
69
-
70
-		$this->test = isset($params['test']);
71
-		$this->bucket = $params['bucket'];
72
-		$this->timeout = !isset($params['timeout']) ? 15 : $params['timeout'];
73
-		$this->uploadPartSize = !isset($params['uploadPartSize']) ? 524288000 : $params['uploadPartSize'];
74
-		$params['region'] = empty($params['region']) ? 'eu-west-1' : $params['region'];
75
-		$params['hostname'] = empty($params['hostname']) ? 's3.' . $params['region'] . '.amazonaws.com' : $params['hostname'];
76
-		if (!isset($params['port']) || $params['port'] === '') {
77
-			$params['port'] = (isset($params['use_ssl']) && $params['use_ssl'] === false) ? 80 : 443;
78
-		}
79
-		$params['verify_bucket_exists'] = empty($params['verify_bucket_exists']) ? true : $params['verify_bucket_exists'];
80
-		$this->params = $params;
81
-	}
82
-
83
-	public function getBucket() {
84
-		return $this->bucket;
85
-	}
86
-
87
-	/**
88
-	 * Returns the connection
89
-	 *
90
-	 * @return S3Client connected client
91
-	 * @throws \Exception if connection could not be made
92
-	 */
93
-	public function getConnection() {
94
-		if (!is_null($this->connection)) {
95
-			return $this->connection;
96
-		}
97
-
98
-		$scheme = (isset($this->params['use_ssl']) && $this->params['use_ssl'] === false) ? 'http' : 'https';
99
-		$base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/';
100
-
101
-		// Adding explicit credential provider to the beginning chain.
102
-		// Including environment variables and IAM instance profiles.
103
-		$provider = CredentialProvider::memoize(
104
-			CredentialProvider::chain(
105
-				$this->paramCredentialProvider(),
106
-				CredentialProvider::env(),
107
-				CredentialProvider::instanceProfile()
108
-			)
109
-		);
110
-
111
-		$options = [
112
-			'version' => isset($this->params['version']) ? $this->params['version'] : 'latest',
113
-			'credentials' => $provider,
114
-			'endpoint' => $base_url,
115
-			'region' => $this->params['region'],
116
-			'use_path_style_endpoint' => isset($this->params['use_path_style']) ? $this->params['use_path_style'] : false,
117
-			'signature_provider' => \Aws\or_chain([self::class, 'legacySignatureProvider'], ClientResolver::_default_signature_provider()),
118
-			'csm' => false,
119
-		];
120
-		if (isset($this->params['proxy'])) {
121
-			$options['request.options'] = ['proxy' => $this->params['proxy']];
122
-		}
123
-		if (isset($this->params['legacy_auth']) && $this->params['legacy_auth']) {
124
-			$options['signature_version'] = 'v2';
125
-		}
126
-		$this->connection = new S3Client($options);
127
-
128
-		if (!$this->connection::isBucketDnsCompatible($this->bucket)) {
129
-			$logger = \OC::$server->getLogger();
130
-			$logger->debug('Bucket "' . $this->bucket . '" This bucket name is not dns compatible, it may contain invalid characters.',
131
-					 ['app' => 'objectstore']);
132
-		}
133
-
134
-		if ($this->params['verify_bucket_exists'] && !$this->connection->doesBucketExist($this->bucket)) {
135
-			$logger = \OC::$server->getLogger();
136
-			try {
137
-				$logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']);
138
-				if (!$this->connection::isBucketDnsCompatible($this->bucket)) {
139
-					throw new \Exception("The bucket will not be created because the name is not dns compatible, please correct it: " . $this->bucket);
140
-				}
141
-				$this->connection->createBucket(['Bucket' => $this->bucket]);
142
-				$this->testTimeout();
143
-			} catch (S3Exception $e) {
144
-				$logger->logException($e, [
145
-					'message' => 'Invalid remote storage.',
146
-					'level' => ILogger::DEBUG,
147
-					'app' => 'objectstore',
148
-				]);
149
-				throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage());
150
-			}
151
-		}
152
-
153
-		// google cloud's s3 compatibility doesn't like the EncodingType parameter
154
-		if (strpos($base_url, 'storage.googleapis.com')) {
155
-			$this->connection->getHandlerList()->remove('s3.auto_encode');
156
-		}
157
-
158
-		return $this->connection;
159
-	}
160
-
161
-	/**
162
-	 * when running the tests wait to let the buckets catch up
163
-	 */
164
-	private function testTimeout() {
165
-		if ($this->test) {
166
-			sleep($this->timeout);
167
-		}
168
-	}
169
-
170
-	public static function legacySignatureProvider($version, $service, $region) {
171
-		switch ($version) {
172
-			case 'v2':
173
-			case 's3':
174
-				return new S3Signature();
175
-			default:
176
-				return null;
177
-		}
178
-	}
179
-
180
-	/**
181
-	 * This function creates a credential provider based on user parameter file
182
-	 */
183
-	protected function paramCredentialProvider() : callable {
184
-		return function () {
185
-			$key = empty($this->params['key']) ? null : $this->params['key'];
186
-			$secret = empty($this->params['secret']) ? null : $this->params['secret'];
187
-
188
-			if ($key && $secret) {
189
-				return Promise\promise_for(
190
-					new Credentials($key, $secret)
191
-				);
192
-			}
193
-
194
-			$msg = 'Could not find parameters set for credentials in config file.';
195
-			return new RejectedPromise(new CredentialsException($msg));
196
-		};
197
-	}
43
+    /** @var array */
44
+    protected $params;
45
+
46
+    /** @var S3Client */
47
+    protected $connection;
48
+
49
+    /** @var string */
50
+    protected $id;
51
+
52
+    /** @var string */
53
+    protected $bucket;
54
+
55
+    /** @var int */
56
+    protected $timeout;
57
+
58
+    /** @var int */
59
+    protected $uploadPartSize;
60
+
61
+    protected $test;
62
+
63
+    protected function parseParams($params) {
64
+        if (empty($params['bucket'])) {
65
+            throw new \Exception("Bucket has to be configured.");
66
+        }
67
+
68
+        $this->id = 'amazon::' . $params['bucket'];
69
+
70
+        $this->test = isset($params['test']);
71
+        $this->bucket = $params['bucket'];
72
+        $this->timeout = !isset($params['timeout']) ? 15 : $params['timeout'];
73
+        $this->uploadPartSize = !isset($params['uploadPartSize']) ? 524288000 : $params['uploadPartSize'];
74
+        $params['region'] = empty($params['region']) ? 'eu-west-1' : $params['region'];
75
+        $params['hostname'] = empty($params['hostname']) ? 's3.' . $params['region'] . '.amazonaws.com' : $params['hostname'];
76
+        if (!isset($params['port']) || $params['port'] === '') {
77
+            $params['port'] = (isset($params['use_ssl']) && $params['use_ssl'] === false) ? 80 : 443;
78
+        }
79
+        $params['verify_bucket_exists'] = empty($params['verify_bucket_exists']) ? true : $params['verify_bucket_exists'];
80
+        $this->params = $params;
81
+    }
82
+
83
+    public function getBucket() {
84
+        return $this->bucket;
85
+    }
86
+
87
+    /**
88
+     * Returns the connection
89
+     *
90
+     * @return S3Client connected client
91
+     * @throws \Exception if connection could not be made
92
+     */
93
+    public function getConnection() {
94
+        if (!is_null($this->connection)) {
95
+            return $this->connection;
96
+        }
97
+
98
+        $scheme = (isset($this->params['use_ssl']) && $this->params['use_ssl'] === false) ? 'http' : 'https';
99
+        $base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/';
100
+
101
+        // Adding explicit credential provider to the beginning chain.
102
+        // Including environment variables and IAM instance profiles.
103
+        $provider = CredentialProvider::memoize(
104
+            CredentialProvider::chain(
105
+                $this->paramCredentialProvider(),
106
+                CredentialProvider::env(),
107
+                CredentialProvider::instanceProfile()
108
+            )
109
+        );
110
+
111
+        $options = [
112
+            'version' => isset($this->params['version']) ? $this->params['version'] : 'latest',
113
+            'credentials' => $provider,
114
+            'endpoint' => $base_url,
115
+            'region' => $this->params['region'],
116
+            'use_path_style_endpoint' => isset($this->params['use_path_style']) ? $this->params['use_path_style'] : false,
117
+            'signature_provider' => \Aws\or_chain([self::class, 'legacySignatureProvider'], ClientResolver::_default_signature_provider()),
118
+            'csm' => false,
119
+        ];
120
+        if (isset($this->params['proxy'])) {
121
+            $options['request.options'] = ['proxy' => $this->params['proxy']];
122
+        }
123
+        if (isset($this->params['legacy_auth']) && $this->params['legacy_auth']) {
124
+            $options['signature_version'] = 'v2';
125
+        }
126
+        $this->connection = new S3Client($options);
127
+
128
+        if (!$this->connection::isBucketDnsCompatible($this->bucket)) {
129
+            $logger = \OC::$server->getLogger();
130
+            $logger->debug('Bucket "' . $this->bucket . '" This bucket name is not dns compatible, it may contain invalid characters.',
131
+                        ['app' => 'objectstore']);
132
+        }
133
+
134
+        if ($this->params['verify_bucket_exists'] && !$this->connection->doesBucketExist($this->bucket)) {
135
+            $logger = \OC::$server->getLogger();
136
+            try {
137
+                $logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']);
138
+                if (!$this->connection::isBucketDnsCompatible($this->bucket)) {
139
+                    throw new \Exception("The bucket will not be created because the name is not dns compatible, please correct it: " . $this->bucket);
140
+                }
141
+                $this->connection->createBucket(['Bucket' => $this->bucket]);
142
+                $this->testTimeout();
143
+            } catch (S3Exception $e) {
144
+                $logger->logException($e, [
145
+                    'message' => 'Invalid remote storage.',
146
+                    'level' => ILogger::DEBUG,
147
+                    'app' => 'objectstore',
148
+                ]);
149
+                throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage());
150
+            }
151
+        }
152
+
153
+        // google cloud's s3 compatibility doesn't like the EncodingType parameter
154
+        if (strpos($base_url, 'storage.googleapis.com')) {
155
+            $this->connection->getHandlerList()->remove('s3.auto_encode');
156
+        }
157
+
158
+        return $this->connection;
159
+    }
160
+
161
+    /**
162
+     * when running the tests wait to let the buckets catch up
163
+     */
164
+    private function testTimeout() {
165
+        if ($this->test) {
166
+            sleep($this->timeout);
167
+        }
168
+    }
169
+
170
+    public static function legacySignatureProvider($version, $service, $region) {
171
+        switch ($version) {
172
+            case 'v2':
173
+            case 's3':
174
+                return new S3Signature();
175
+            default:
176
+                return null;
177
+        }
178
+    }
179
+
180
+    /**
181
+     * This function creates a credential provider based on user parameter file
182
+     */
183
+    protected function paramCredentialProvider() : callable {
184
+        return function () {
185
+            $key = empty($this->params['key']) ? null : $this->params['key'];
186
+            $secret = empty($this->params['secret']) ? null : $this->params['secret'];
187
+
188
+            if ($key && $secret) {
189
+                return Promise\promise_for(
190
+                    new Credentials($key, $secret)
191
+                );
192
+            }
193
+
194
+            $msg = 'Could not find parameters set for credentials in config file.';
195
+            return new RejectedPromise(new CredentialsException($msg));
196
+        };
197
+    }
198 198
 }
Please login to merge, or discard this patch.
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -65,14 +65,14 @@  discard block
 block discarded – undo
65 65
 			throw new \Exception("Bucket has to be configured.");
66 66
 		}
67 67
 
68
-		$this->id = 'amazon::' . $params['bucket'];
68
+		$this->id = 'amazon::'.$params['bucket'];
69 69
 
70 70
 		$this->test = isset($params['test']);
71 71
 		$this->bucket = $params['bucket'];
72 72
 		$this->timeout = !isset($params['timeout']) ? 15 : $params['timeout'];
73 73
 		$this->uploadPartSize = !isset($params['uploadPartSize']) ? 524288000 : $params['uploadPartSize'];
74 74
 		$params['region'] = empty($params['region']) ? 'eu-west-1' : $params['region'];
75
-		$params['hostname'] = empty($params['hostname']) ? 's3.' . $params['region'] . '.amazonaws.com' : $params['hostname'];
75
+		$params['hostname'] = empty($params['hostname']) ? 's3.'.$params['region'].'.amazonaws.com' : $params['hostname'];
76 76
 		if (!isset($params['port']) || $params['port'] === '') {
77 77
 			$params['port'] = (isset($params['use_ssl']) && $params['use_ssl'] === false) ? 80 : 443;
78 78
 		}
@@ -96,7 +96,7 @@  discard block
 block discarded – undo
96 96
 		}
97 97
 
98 98
 		$scheme = (isset($this->params['use_ssl']) && $this->params['use_ssl'] === false) ? 'http' : 'https';
99
-		$base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/';
99
+		$base_url = $scheme.'://'.$this->params['hostname'].':'.$this->params['port'].'/';
100 100
 
101 101
 		// Adding explicit credential provider to the beginning chain.
102 102
 		// Including environment variables and IAM instance profiles.
@@ -127,16 +127,16 @@  discard block
 block discarded – undo
127 127
 
128 128
 		if (!$this->connection::isBucketDnsCompatible($this->bucket)) {
129 129
 			$logger = \OC::$server->getLogger();
130
-			$logger->debug('Bucket "' . $this->bucket . '" This bucket name is not dns compatible, it may contain invalid characters.',
130
+			$logger->debug('Bucket "'.$this->bucket.'" This bucket name is not dns compatible, it may contain invalid characters.',
131 131
 					 ['app' => 'objectstore']);
132 132
 		}
133 133
 
134 134
 		if ($this->params['verify_bucket_exists'] && !$this->connection->doesBucketExist($this->bucket)) {
135 135
 			$logger = \OC::$server->getLogger();
136 136
 			try {
137
-				$logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']);
137
+				$logger->info('Bucket "'.$this->bucket.'" does not exist - creating it.', ['app' => 'objectstore']);
138 138
 				if (!$this->connection::isBucketDnsCompatible($this->bucket)) {
139
-					throw new \Exception("The bucket will not be created because the name is not dns compatible, please correct it: " . $this->bucket);
139
+					throw new \Exception("The bucket will not be created because the name is not dns compatible, please correct it: ".$this->bucket);
140 140
 				}
141 141
 				$this->connection->createBucket(['Bucket' => $this->bucket]);
142 142
 				$this->testTimeout();
@@ -146,7 +146,7 @@  discard block
 block discarded – undo
146 146
 					'level' => ILogger::DEBUG,
147 147
 					'app' => 'objectstore',
148 148
 				]);
149
-				throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage());
149
+				throw new \Exception('Creation of bucket "'.$this->bucket.'" failed. '.$e->getMessage());
150 150
 			}
151 151
 		}
152 152
 
@@ -181,7 +181,7 @@  discard block
 block discarded – undo
181 181
 	 * This function creates a credential provider based on user parameter file
182 182
 	 */
183 183
 	protected function paramCredentialProvider() : callable {
184
-		return function () {
184
+		return function() {
185 185
 			$key = empty($this->params['key']) ? null : $this->params['key'];
186 186
 			$secret = empty($this->params['secret']) ? null : $this->params['secret'];
187 187
 
Please login to merge, or discard this patch.