Completed
Push — master ( 12c9ba...26e0c1 )
by Morris
59:29 queued 40:54
created

S3ObjectTrait::multiPartUpload()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 20
nc 3
nop 2
dl 0
loc 28
rs 8.439
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\Exception\MultipartUploadException;
27
use Aws\S3\MultipartUploader;
28
use Aws\S3\S3Client;
29
use Psr\Http\Message\StreamInterface;
30
31
const S3_UPLOAD_PART_SIZE = 524288000; // 500MB
32
33
trait S3ObjectTrait {
34
	/**
35
	 * Returns the connection
36
	 *
37
	 * @return S3Client connected client
38
	 * @throws \Exception if connection could not be made
39
	 */
40
	abstract protected function getConnection();
41
42
	/**
43
	 * @param string $urn the unified resource name used to identify the object
44
	 * @return resource stream with the read data
45
	 * @throws \Exception when something goes wrong, message will be logged
46
	 * @since 7.0.0
47
	 */
48
	function readObject($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...
49
		$client = $this->getConnection();
50
		$command = $client->getCommand('GetObject', [
51
			'Bucket' => $this->bucket,
0 ignored issues
show
Bug introduced by
The property bucket does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
52
			'Key' => $urn
53
		]);
54
		$request = \Aws\serialize($command);
55
		$headers = [];
56
		foreach ($request->getHeaders() as $key => $values) {
0 ignored issues
show
Bug introduced by
The method getHeaders cannot be called on $request (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
57
			foreach ($values as $value) {
58
				$headers[] = "$key: $value";
59
			}
60
		}
61
		$opts = [
62
			'http' => [
63
				'header' => $headers
64
			]
65
		];
66
67
		$context = stream_context_create($opts);
68
		return fopen($request->getUri(), 'r', false, $context);
0 ignored issues
show
Bug introduced by
The method getUri cannot be called on $request (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
69
	}
70
71
	/**
72
	 * @param string $urn the unified resource name used to identify the object
73
	 * @param resource $stream stream with the data to write
74
	 * @throws \Exception when something goes wrong, message will be logged
75
	 * @since 7.0.0
76
	 */
77
	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...
78
		$this->getConnection()->upload($this->bucket, $urn, $stream, 'private', [
79
			'mup_threshold' => S3_UPLOAD_PART_SIZE,
80
			'part_size' => S3_UPLOAD_PART_SIZE
81
		]);
82
	}
83
84
	/**
85
	 * @param string $urn the unified resource name used to identify the object
86
	 * @return void
87
	 * @throws \Exception when something goes wrong, message will be logged
88
	 * @since 7.0.0
89
	 */
90
	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...
91
		$this->getConnection()->deleteObject([
92
			'Bucket' => $this->bucket,
93
			'Key' => $urn
94
		]);
95
	}
96
}
97