Completed
Push — master ( 647b18...1a99e0 )
by Robin
22:10 queued 05:29
created

S3ObjectTrait::readObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Robin Appelman <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OC\Files\ObjectStore;
23
24
use Aws\Exception\MultipartUploadException;
25
use Aws\S3\MultipartUploader;
26
use Aws\S3\S3Client;
27
use Psr\Http\Message\StreamInterface;
28
29
const S3_UPLOAD_PART_SIZE = 524288000; // 500MB
30
31
trait S3ObjectTrait {
32
	/**
33
	 * Returns the connection
34
	 *
35
	 * @return S3Client connected client
36
	 * @throws \Exception if connection could not be made
37
	 */
38
	abstract protected function getConnection();
39
40
	/**
41
	 * @param string $urn the unified resource name used to identify the object
42
	 * @return resource stream with the read data
43
	 * @throws \Exception when something goes wrong, message will be logged
44
	 * @since 7.0.0
45
	 */
46
	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...
47
		$client = $this->getConnection();
48
		$command = $client->getCommand('GetObject', [
49
			'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...
50
			'Key' => $urn
51
		]);
52
		$command['@http']['stream'] = true;
53
		$result = $client->execute($command);
54
		/** @var StreamInterface $body */
55
		$body = $result['Body'];
56
57
		return $body->detach();
58
	}
59
60
	/**
61
	 * @param string $urn the unified resource name used to identify the object
62
	 * @param resource $stream stream with the data to write
63
	 * @throws \Exception when something goes wrong, message will be logged
64
	 * @since 7.0.0
65
	 */
66
	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...
67
		$stat = fstat($stream);
68
69
		if ($stat['size'] && $stat['size'] < S3_UPLOAD_PART_SIZE) {
70
			$this->singlePartUpload($urn, $stream);
71
		} else {
72
			$this->multiPartUpload($urn, $stream);
73
		}
74
75
	}
76
77
	protected function singlePartUpload($urn, $stream) {
78
		$this->getConnection()->putObject([
79
			'Bucket' => $this->bucket,
80
			'Key' => $urn,
81
			'Body' => $stream
82
		]);
83
	}
84
85
	protected function multiPartUpload($urn, $stream) {
86
		$uploader = new MultipartUploader($this->getConnection(), $stream, [
87
			'bucket' => $this->bucket,
88
			'key' => $urn,
89
			'part_size' => S3_UPLOAD_PART_SIZE
90
		]);
91
92
		$tries = 0;
93
94
		do {
95
			try {
96
				$result = $uploader->upload();
97
			} catch (MultipartUploadException $e) {
0 ignored issues
show
Bug introduced by
The class Aws\Exception\MultipartUploadException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
98
				\OC::$server->getLogger()->logException($e);
99
				rewind($stream);
100
				$tries++;
101
102
				if ($tries < 5) {
103
					$uploader = new MultipartUploader($this->getConnection(), $stream, [
104
						'state' => $e->getState()
105
					]);
106
				} else {
107
					$this->getConnection()->abortMultipartUpload($e->getState()->getId());
108
				}
109
			}
110
		} while (!isset($result) && $tries < 5);
111
	}
112
113
	/**
114
	 * @param string $urn the unified resource name used to identify the object
115
	 * @return void
116
	 * @throws \Exception when something goes wrong, message will be logged
117
	 * @since 7.0.0
118
	 */
119
	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...
120
		$this->getConnection()->deleteObject([
121
			'Bucket' => $this->bucket,
122
			'Key' => $urn
123
		]);
124
	}
125
}
126