@@ -35,95 +35,95 @@ |
||
35 | 35 | use OC\Files\Stream\SeekableHttpStream; |
36 | 36 | |
37 | 37 | trait S3ObjectTrait { |
38 | - /** |
|
39 | - * Returns the connection |
|
40 | - * |
|
41 | - * @return S3Client connected client |
|
42 | - * @throws \Exception if connection could not be made |
|
43 | - */ |
|
44 | - abstract protected function getConnection(); |
|
38 | + /** |
|
39 | + * Returns the connection |
|
40 | + * |
|
41 | + * @return S3Client connected client |
|
42 | + * @throws \Exception if connection could not be made |
|
43 | + */ |
|
44 | + abstract protected function getConnection(); |
|
45 | 45 | |
46 | - /** |
|
47 | - * @param string $urn the unified resource name used to identify the object |
|
48 | - * @return resource stream with the read data |
|
49 | - * @throws \Exception when something goes wrong, message will be logged |
|
50 | - * @since 7.0.0 |
|
51 | - */ |
|
52 | - public function readObject($urn) { |
|
53 | - return SeekableHttpStream::open(function ($range) use ($urn) { |
|
54 | - $command = $this->getConnection()->getCommand('GetObject', [ |
|
55 | - 'Bucket' => $this->bucket, |
|
56 | - 'Key' => $urn, |
|
57 | - 'Range' => 'bytes=' . $range, |
|
58 | - ]); |
|
59 | - $request = \Aws\serialize($command); |
|
60 | - $headers = []; |
|
61 | - foreach ($request->getHeaders() as $key => $values) { |
|
62 | - foreach ($values as $value) { |
|
63 | - $headers[] = "$key: $value"; |
|
64 | - } |
|
65 | - } |
|
66 | - $opts = [ |
|
67 | - 'http' => [ |
|
68 | - 'protocol_version' => 1.1, |
|
69 | - 'header' => $headers, |
|
70 | - ], |
|
71 | - ]; |
|
46 | + /** |
|
47 | + * @param string $urn the unified resource name used to identify the object |
|
48 | + * @return resource stream with the read data |
|
49 | + * @throws \Exception when something goes wrong, message will be logged |
|
50 | + * @since 7.0.0 |
|
51 | + */ |
|
52 | + public function readObject($urn) { |
|
53 | + return SeekableHttpStream::open(function ($range) use ($urn) { |
|
54 | + $command = $this->getConnection()->getCommand('GetObject', [ |
|
55 | + 'Bucket' => $this->bucket, |
|
56 | + 'Key' => $urn, |
|
57 | + 'Range' => 'bytes=' . $range, |
|
58 | + ]); |
|
59 | + $request = \Aws\serialize($command); |
|
60 | + $headers = []; |
|
61 | + foreach ($request->getHeaders() as $key => $values) { |
|
62 | + foreach ($values as $value) { |
|
63 | + $headers[] = "$key: $value"; |
|
64 | + } |
|
65 | + } |
|
66 | + $opts = [ |
|
67 | + 'http' => [ |
|
68 | + 'protocol_version' => 1.1, |
|
69 | + 'header' => $headers, |
|
70 | + ], |
|
71 | + ]; |
|
72 | 72 | |
73 | - $context = stream_context_create($opts); |
|
74 | - return fopen($request->getUri(), 'r', false, $context); |
|
75 | - }); |
|
76 | - } |
|
73 | + $context = stream_context_create($opts); |
|
74 | + return fopen($request->getUri(), 'r', false, $context); |
|
75 | + }); |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * @param string $urn the unified resource name used to identify the object |
|
80 | - * @param resource $stream stream with the data to write |
|
81 | - * @throws \Exception when something goes wrong, message will be logged |
|
82 | - * @since 7.0.0 |
|
83 | - */ |
|
84 | - public function writeObject($urn, $stream) { |
|
85 | - $count = 0; |
|
86 | - $countStream = CallbackWrapper::wrap($stream, function ($read) use (&$count) { |
|
87 | - $count += $read; |
|
88 | - }); |
|
78 | + /** |
|
79 | + * @param string $urn the unified resource name used to identify the object |
|
80 | + * @param resource $stream stream with the data to write |
|
81 | + * @throws \Exception when something goes wrong, message will be logged |
|
82 | + * @since 7.0.0 |
|
83 | + */ |
|
84 | + public function writeObject($urn, $stream) { |
|
85 | + $count = 0; |
|
86 | + $countStream = CallbackWrapper::wrap($stream, function ($read) use (&$count) { |
|
87 | + $count += $read; |
|
88 | + }); |
|
89 | 89 | |
90 | - $uploader = new MultipartUploader($this->getConnection(), $countStream, [ |
|
91 | - 'bucket' => $this->bucket, |
|
92 | - 'key' => $urn, |
|
93 | - 'part_size' => $this->uploadPartSize, |
|
94 | - ]); |
|
90 | + $uploader = new MultipartUploader($this->getConnection(), $countStream, [ |
|
91 | + 'bucket' => $this->bucket, |
|
92 | + 'key' => $urn, |
|
93 | + 'part_size' => $this->uploadPartSize, |
|
94 | + ]); |
|
95 | 95 | |
96 | - try { |
|
97 | - $uploader->upload(); |
|
98 | - } catch (S3MultipartUploadException $e) { |
|
99 | - // This is an empty file so just touch it then |
|
100 | - if ($count === 0 && feof($countStream)) { |
|
101 | - $uploader = new ObjectUploader($this->getConnection(), $this->bucket, $urn, ''); |
|
102 | - $uploader->upload(); |
|
103 | - } else { |
|
104 | - throw $e; |
|
105 | - } |
|
106 | - } |
|
107 | - } |
|
96 | + try { |
|
97 | + $uploader->upload(); |
|
98 | + } catch (S3MultipartUploadException $e) { |
|
99 | + // This is an empty file so just touch it then |
|
100 | + if ($count === 0 && feof($countStream)) { |
|
101 | + $uploader = new ObjectUploader($this->getConnection(), $this->bucket, $urn, ''); |
|
102 | + $uploader->upload(); |
|
103 | + } else { |
|
104 | + throw $e; |
|
105 | + } |
|
106 | + } |
|
107 | + } |
|
108 | 108 | |
109 | - /** |
|
110 | - * @param string $urn the unified resource name used to identify the object |
|
111 | - * @return void |
|
112 | - * @throws \Exception when something goes wrong, message will be logged |
|
113 | - * @since 7.0.0 |
|
114 | - */ |
|
115 | - public function deleteObject($urn) { |
|
116 | - $this->getConnection()->deleteObject([ |
|
117 | - 'Bucket' => $this->bucket, |
|
118 | - 'Key' => $urn, |
|
119 | - ]); |
|
120 | - } |
|
109 | + /** |
|
110 | + * @param string $urn the unified resource name used to identify the object |
|
111 | + * @return void |
|
112 | + * @throws \Exception when something goes wrong, message will be logged |
|
113 | + * @since 7.0.0 |
|
114 | + */ |
|
115 | + public function deleteObject($urn) { |
|
116 | + $this->getConnection()->deleteObject([ |
|
117 | + 'Bucket' => $this->bucket, |
|
118 | + 'Key' => $urn, |
|
119 | + ]); |
|
120 | + } |
|
121 | 121 | |
122 | - public function objectExists($urn) { |
|
123 | - return $this->getConnection()->doesObjectExist($this->bucket, $urn); |
|
124 | - } |
|
122 | + public function objectExists($urn) { |
|
123 | + return $this->getConnection()->doesObjectExist($this->bucket, $urn); |
|
124 | + } |
|
125 | 125 | |
126 | - public function copyObject($from, $to) { |
|
127 | - $this->getConnection()->copy($this->getBucket(), $from, $this->getBucket(), $to); |
|
128 | - } |
|
126 | + public function copyObject($from, $to) { |
|
127 | + $this->getConnection()->copy($this->getBucket(), $from, $this->getBucket(), $to); |
|
128 | + } |
|
129 | 129 | } |