Passed
Push — master ( 892589...8bee8e )
by Roeland
12:32 queued 11s
created
lib/private/Files/ObjectStore/S3ConnectionTrait.php 2 patches
Indentation   +122 added lines, -122 removed lines patch added patch discarded remove patch
@@ -33,131 +33,131 @@
 block discarded – undo
33 33
 use OCP\ILogger;
34 34
 
35 35
 trait S3ConnectionTrait {
36
-	/** @var array */
37
-	protected $params;
36
+    /** @var array */
37
+    protected $params;
38 38
 
39
-	/** @var S3Client */
40
-	protected $connection;
39
+    /** @var S3Client */
40
+    protected $connection;
41 41
 
42
-	/** @var string */
43
-	protected $id;
42
+    /** @var string */
43
+    protected $id;
44 44
 
45
-	/** @var string */
46
-	protected $bucket;
45
+    /** @var string */
46
+    protected $bucket;
47 47
 
48
-	/** @var int */
49
-	protected $timeout;
48
+    /** @var int */
49
+    protected $timeout;
50 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
-		];
104
-		if (isset($this->params['proxy'])) {
105
-			$options['request.options'] = ['proxy' => $this->params['proxy']];
106
-		}
107
-		if (isset($this->params['legacy_auth']) && $this->params['legacy_auth']) {
108
-			$options['signature_version'] = 'v2';
109
-		}
110
-		$this->connection = new S3Client($options);
111
-
112
-		if (!$this->connection->isBucketDnsCompatible($this->bucket)) {
113
-			$logger = \OC::$server->getLogger();
114
-			$logger->debug('Bucket "' . $this->bucket . '" This bucket name is not dns compatible, it may contain invalid characters.',
115
-					 ['app' => 'objectstore']);
116
-		}
117
-
118
-		if (!$this->connection->doesBucketExist($this->bucket)) {
119
-			$logger = \OC::$server->getLogger();
120
-			try {
121
-				$logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']);
122
-				if (!$this->connection->isBucketDnsCompatible($this->bucket)) {
123
-					throw new \Exception("The bucket will not be created because the name is not dns compatible, please correct it: " . $this->bucket);
124
-				}
125
-				$this->connection->createBucket(['Bucket' => $this->bucket]);
126
-				$this->testTimeout();
127
-			} catch (S3Exception $e) {
128
-				$logger->logException($e, [
129
-					'message' => 'Invalid remote storage.',
130
-					'level' => ILogger::DEBUG,
131
-					'app' => 'objectstore',
132
-				]);
133
-				throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage());
134
-			}
135
-		}
136
-
137
-		// google cloud's s3 compatibility doesn't like the EncodingType parameter
138
-		if (strpos($base_url, 'storage.googleapis.com')) {
139
-			$this->connection->getHandlerList()->remove('s3.auto_encode');
140
-		}
141
-
142
-		return $this->connection;
143
-	}
144
-
145
-	/**
146
-	 * when running the tests wait to let the buckets catch up
147
-	 */
148
-	private function testTimeout() {
149
-		if ($this->test) {
150
-			sleep($this->timeout);
151
-		}
152
-	}
153
-
154
-	public static function legacySignatureProvider($version, $service, $region) {
155
-		switch ($version) {
156
-			case 'v2':
157
-			case 's3':
158
-				return new S3Signature();
159
-			default:
160
-				return null;
161
-		}
162
-	}
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
+        ];
104
+        if (isset($this->params['proxy'])) {
105
+            $options['request.options'] = ['proxy' => $this->params['proxy']];
106
+        }
107
+        if (isset($this->params['legacy_auth']) && $this->params['legacy_auth']) {
108
+            $options['signature_version'] = 'v2';
109
+        }
110
+        $this->connection = new S3Client($options);
111
+
112
+        if (!$this->connection->isBucketDnsCompatible($this->bucket)) {
113
+            $logger = \OC::$server->getLogger();
114
+            $logger->debug('Bucket "' . $this->bucket . '" This bucket name is not dns compatible, it may contain invalid characters.',
115
+                        ['app' => 'objectstore']);
116
+        }
117
+
118
+        if (!$this->connection->doesBucketExist($this->bucket)) {
119
+            $logger = \OC::$server->getLogger();
120
+            try {
121
+                $logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']);
122
+                if (!$this->connection->isBucketDnsCompatible($this->bucket)) {
123
+                    throw new \Exception("The bucket will not be created because the name is not dns compatible, please correct it: " . $this->bucket);
124
+                }
125
+                $this->connection->createBucket(['Bucket' => $this->bucket]);
126
+                $this->testTimeout();
127
+            } catch (S3Exception $e) {
128
+                $logger->logException($e, [
129
+                    'message' => 'Invalid remote storage.',
130
+                    'level' => ILogger::DEBUG,
131
+                    'app' => 'objectstore',
132
+                ]);
133
+                throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage());
134
+            }
135
+        }
136
+
137
+        // google cloud's s3 compatibility doesn't like the EncodingType parameter
138
+        if (strpos($base_url, 'storage.googleapis.com')) {
139
+            $this->connection->getHandlerList()->remove('s3.auto_encode');
140
+        }
141
+
142
+        return $this->connection;
143
+    }
144
+
145
+    /**
146
+     * when running the tests wait to let the buckets catch up
147
+     */
148
+    private function testTimeout() {
149
+        if ($this->test) {
150
+            sleep($this->timeout);
151
+        }
152
+    }
153
+
154
+    public static function legacySignatureProvider($version, $service, $region) {
155
+        switch ($version) {
156
+            case 'v2':
157
+            case 's3':
158
+                return new S3Signature();
159
+            default:
160
+                return null;
161
+        }
162
+    }
163 163
 }
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -58,14 +58,14 @@  discard block
 block discarded – undo
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
 block discarded – undo
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',
@@ -111,16 +111,16 @@  discard block
 block discarded – undo
111 111
 
112 112
 		if (!$this->connection->isBucketDnsCompatible($this->bucket)) {
113 113
 			$logger = \OC::$server->getLogger();
114
-			$logger->debug('Bucket "' . $this->bucket . '" This bucket name is not dns compatible, it may contain invalid characters.',
114
+			$logger->debug('Bucket "'.$this->bucket.'" This bucket name is not dns compatible, it may contain invalid characters.',
115 115
 					 ['app' => 'objectstore']);
116 116
 		}
117 117
 
118 118
 		if (!$this->connection->doesBucketExist($this->bucket)) {
119 119
 			$logger = \OC::$server->getLogger();
120 120
 			try {
121
-				$logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']);
121
+				$logger->info('Bucket "'.$this->bucket.'" does not exist - creating it.', ['app' => 'objectstore']);
122 122
 				if (!$this->connection->isBucketDnsCompatible($this->bucket)) {
123
-					throw new \Exception("The bucket will not be created because the name is not dns compatible, please correct it: " . $this->bucket);
123
+					throw new \Exception("The bucket will not be created because the name is not dns compatible, please correct it: ".$this->bucket);
124 124
 				}
125 125
 				$this->connection->createBucket(['Bucket' => $this->bucket]);
126 126
 				$this->testTimeout();
@@ -130,7 +130,7 @@  discard block
 block discarded – undo
130 130
 					'level' => ILogger::DEBUG,
131 131
 					'app' => 'objectstore',
132 132
 				]);
133
-				throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage());
133
+				throw new \Exception('Creation of bucket "'.$this->bucket.'" failed. '.$e->getMessage());
134 134
 			}
135 135
 		}
136 136
 
Please login to merge, or discard this patch.
lib/private/Files/ObjectStore/S3ObjectTrait.php 1 patch
Indentation   +81 added lines, -81 removed lines patch added patch discarded remove patch
@@ -34,93 +34,93 @@
 block discarded – undo
34 34
 use OC\Files\Stream\SeekableHttpStream;
35 35
 
36 36
 trait S3ObjectTrait {
37
-	/**
38
-	 * Returns the connection
39
-	 *
40
-	 * @return S3Client connected client
41
-	 * @throws \Exception if connection could not be made
42
-	 */
43
-	abstract protected function getConnection();
37
+    /**
38
+     * Returns the connection
39
+     *
40
+     * @return S3Client connected client
41
+     * @throws \Exception if connection could not be made
42
+     */
43
+    abstract protected function getConnection();
44 44
 
45
-	/**
46
-	 * @param string $urn the unified resource name used to identify the object
47
-	 * @return resource stream with the read data
48
-	 * @throws \Exception when something goes wrong, message will be logged
49
-	 * @since 7.0.0
50
-	 */
51
-	public function readObject($urn) {
52
-		return SeekableHttpStream::open(function ($range) use ($urn) {
53
-			$command = $this->getConnection()->getCommand('GetObject', [
54
-				'Bucket' => $this->bucket,
55
-				'Key' => $urn,
56
-				'Range' => 'bytes=' . $range,
57
-			]);
58
-			$request = \Aws\serialize($command);
59
-			$headers = [];
60
-			foreach ($request->getHeaders() as $key => $values) {
61
-				foreach ($values as $value) {
62
-					$headers[] = "$key: $value";
63
-				}
64
-			}
65
-			$opts = [
66
-				'http' => [
67
-					'protocol_version' => 1.1,
68
-					'header' => $headers,
69
-				],
70
-			];
45
+    /**
46
+     * @param string $urn the unified resource name used to identify the object
47
+     * @return resource stream with the read data
48
+     * @throws \Exception when something goes wrong, message will be logged
49
+     * @since 7.0.0
50
+     */
51
+    public function readObject($urn) {
52
+        return SeekableHttpStream::open(function ($range) use ($urn) {
53
+            $command = $this->getConnection()->getCommand('GetObject', [
54
+                'Bucket' => $this->bucket,
55
+                'Key' => $urn,
56
+                'Range' => 'bytes=' . $range,
57
+            ]);
58
+            $request = \Aws\serialize($command);
59
+            $headers = [];
60
+            foreach ($request->getHeaders() as $key => $values) {
61
+                foreach ($values as $value) {
62
+                    $headers[] = "$key: $value";
63
+                }
64
+            }
65
+            $opts = [
66
+                'http' => [
67
+                    'protocol_version' => 1.1,
68
+                    'header' => $headers,
69
+                ],
70
+            ];
71 71
 
72
-			$context = stream_context_create($opts);
73
-			return fopen($request->getUri(), 'r', false, $context);
74
-		});
75
-	}
72
+            $context = stream_context_create($opts);
73
+            return fopen($request->getUri(), 'r', false, $context);
74
+        });
75
+    }
76 76
 
77
-	/**
78
-	 * @param string $urn the unified resource name used to identify the object
79
-	 * @param resource $stream stream with the data to write
80
-	 * @throws \Exception when something goes wrong, message will be logged
81
-	 * @since 7.0.0
82
-	 */
83
-	public function writeObject($urn, $stream) {
84
-		$count = 0;
85
-		$countStream = CallbackWrapper::wrap($stream, function ($read) use (&$count) {
86
-			$count += $read;
87
-		});
77
+    /**
78
+     * @param string $urn the unified resource name used to identify the object
79
+     * @param resource $stream stream with the data to write
80
+     * @throws \Exception when something goes wrong, message will be logged
81
+     * @since 7.0.0
82
+     */
83
+    public function writeObject($urn, $stream) {
84
+        $count = 0;
85
+        $countStream = CallbackWrapper::wrap($stream, function ($read) use (&$count) {
86
+            $count += $read;
87
+        });
88 88
 
89
-		$uploader = new MultipartUploader($this->getConnection(), $countStream, [
90
-			'bucket' => $this->bucket,
91
-			'key' => $urn,
92
-			'part_size' => $this->uploadPartSize,
93
-		]);
89
+        $uploader = new MultipartUploader($this->getConnection(), $countStream, [
90
+            'bucket' => $this->bucket,
91
+            'key' => $urn,
92
+            'part_size' => $this->uploadPartSize,
93
+        ]);
94 94
 
95
-		try {
96
-			$uploader->upload();
97
-		} catch (S3MultipartUploadException $e) {
98
-			// This is an empty file so just touch it then
99
-			if ($count === 0 && feof($countStream)) {
100
-				$uploader = new ObjectUploader($this->getConnection(), $this->bucket, $urn, '');
101
-				$uploader->upload();
102
-			} else {
103
-				throw $e;
104
-			}
105
-		}
95
+        try {
96
+            $uploader->upload();
97
+        } catch (S3MultipartUploadException $e) {
98
+            // This is an empty file so just touch it then
99
+            if ($count === 0 && feof($countStream)) {
100
+                $uploader = new ObjectUploader($this->getConnection(), $this->bucket, $urn, '');
101
+                $uploader->upload();
102
+            } else {
103
+                throw $e;
104
+            }
105
+        }
106 106
 
107
-		fclose($countStream);
108
-	}
107
+        fclose($countStream);
108
+    }
109 109
 
110
-	/**
111
-	 * @param string $urn the unified resource name used to identify the object
112
-	 * @return void
113
-	 * @throws \Exception when something goes wrong, message will be logged
114
-	 * @since 7.0.0
115
-	 */
116
-	public function deleteObject($urn) {
117
-		$this->getConnection()->deleteObject([
118
-			'Bucket' => $this->bucket,
119
-			'Key' => $urn,
120
-		]);
121
-	}
110
+    /**
111
+     * @param string $urn the unified resource name used to identify the object
112
+     * @return void
113
+     * @throws \Exception when something goes wrong, message will be logged
114
+     * @since 7.0.0
115
+     */
116
+    public function deleteObject($urn) {
117
+        $this->getConnection()->deleteObject([
118
+            'Bucket' => $this->bucket,
119
+            'Key' => $urn,
120
+        ]);
121
+    }
122 122
 
123
-	public function objectExists($urn) {
124
-		return $this->getConnection()->doesObjectExist($this->bucket, $urn);
125
-	}
123
+    public function objectExists($urn) {
124
+        return $this->getConnection()->doesObjectExist($this->bucket, $urn);
125
+    }
126 126
 }
Please login to merge, or discard this patch.