Passed
Push — master ( 8113df...5a27e5 )
by Morris
27:51 queued 12:24
created

S3ObjectTrait::objectExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Robin Appelman <[email protected]>
4
 *
5
 * @author Robin Appelman <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OC\Files\ObjectStore;
25
26
use Aws\S3\S3Client;
27
28
const S3_UPLOAD_PART_SIZE = 524288000; // 500MB
29
30
trait S3ObjectTrait {
31
	/**
32
	 * Returns the connection
33
	 *
34
	 * @return S3Client connected client
35
	 * @throws \Exception if connection could not be made
36
	 */
37
	abstract protected function getConnection();
38
39
	/**
40
	 * @param string $urn the unified resource name used to identify the object
41
	 * @return resource stream with the read data
42
	 * @throws \Exception when something goes wrong, message will be logged
43
	 * @since 7.0.0
44
	 */
45
	function readObject($urn) {
46
		$client = $this->getConnection();
47
		$command = $client->getCommand('GetObject', [
48
			'Bucket' => $this->bucket,
49
			'Key' => $urn
50
		]);
51
		$request = \Aws\serialize($command);
52
		$headers = [];
53
		foreach ($request->getHeaders() as $key => $values) {
54
			foreach ($values as $value) {
55
				$headers[] = "$key: $value";
56
			}
57
		}
58
		$opts = [
59
			'http' => [
60
				'header' => $headers
61
			]
62
		];
63
64
		$context = stream_context_create($opts);
65
		return fopen($request->getUri(), 'r', false, $context);
0 ignored issues
show
Bug Best Practice introduced by
The expression return fopen($request->g..., 'r', false, $context) could also return false which is incompatible with the documented return type resource. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
66
	}
67
68
	/**
69
	 * @param string $urn the unified resource name used to identify the object
70
	 * @param resource $stream stream with the data to write
71
	 * @throws \Exception when something goes wrong, message will be logged
72
	 * @since 7.0.0
73
	 */
74
	function writeObject($urn, $stream) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
75
		$this->getConnection()->upload($this->bucket, $urn, $stream, 'private', [
76
			'mup_threshold' => S3_UPLOAD_PART_SIZE,
77
			'part_size' => S3_UPLOAD_PART_SIZE
78
		]);
79
	}
80
81
	/**
82
	 * @param string $urn the unified resource name used to identify the object
83
	 * @return void
84
	 * @throws \Exception when something goes wrong, message will be logged
85
	 * @since 7.0.0
86
	 */
87
	function deleteObject($urn) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
88
		$this->getConnection()->deleteObject([
89
			'Bucket' => $this->bucket,
90
			'Key' => $urn
91
		]);
92
	}
93
94
	public function objectExists($urn) {
95
		return $this->getConnection()->doesObjectExist($this->bucket, $urn);
96
	}
97
}
98