diff options
author | Robin Appelman <robin@icewind.nl> | 2017-06-07 16:29:13 +0200 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2017-09-18 15:16:27 +0200 |
commit | d70607104e7000ba008b4eb20bcf58a47b88fe35 (patch) | |
tree | 05604cd5a4d910ea45d7fc826b15ca1a4c45ca5c /lib/private/Files | |
parent | dad18baec8274995ff384789ccc2577aabc6dde8 (diff) | |
download | nextcloud-server-d70607104e7000ba008b4eb20bcf58a47b88fe35.tar.gz nextcloud-server-d70607104e7000ba008b4eb20bcf58a47b88fe35.zip |
reuse object read/write/delete logic in s3 implementations
Signed-off-by: Robin Appelman <robin@icewind.nl>
Diffstat (limited to 'lib/private/Files')
-rw-r--r-- | lib/private/Files/ObjectStore/S3.php | 50 | ||||
-rw-r--r-- | lib/private/Files/ObjectStore/S3ObjectTrait.php | 82 |
2 files changed, 83 insertions, 49 deletions
diff --git a/lib/private/Files/ObjectStore/S3.php b/lib/private/Files/ObjectStore/S3.php index 17caf1cb126..e4a7b068b65 100644 --- a/lib/private/Files/ObjectStore/S3.php +++ b/lib/private/Files/ObjectStore/S3.php @@ -22,10 +22,10 @@ namespace OC\Files\ObjectStore; use OCP\Files\ObjectStore\IObjectStore; -use Psr\Http\Message\StreamInterface; class S3 implements IObjectStore { use S3ConnectionTrait; + use S3ObjectTrait; public function __construct($parameters) { $this->parseParams($parameters); @@ -38,52 +38,4 @@ class S3 implements IObjectStore { public function getStorageId() { return $this->id; } - - /** - * @param string $urn the unified resource name used to identify the object - * @return resource stream with the read data - * @throws \Exception when something goes wrong, message will be logged - * @since 7.0.0 - */ - function readObject($urn) { - $client = $this->getConnection(); - $command = $client->getCommand('GetObject', [ - 'Bucket' => $this->bucket, - 'Key' => $urn - ]); - $command['@http']['stream'] = true; - $result = $client->execute($command); - /** @var StreamInterface $body */ - $body = $result['Body']; - - return $body->detach(); - } - - /** - * @param string $urn the unified resource name used to identify the object - * @param resource $stream stream with the data to write - * @throws \Exception when something goes wrong, message will be logged - * @since 7.0.0 - */ - public function writeObject($urn, $stream) { - $this->getConnection()->putObject([ - 'Bucket' => $this->bucket, - 'Key' => $urn, - 'Body' => $stream - ]); - } - - /** - * @param string $urn the unified resource name used to identify the object - * @return void - * @throws \Exception when something goes wrong, message will be logged - * @since 7.0.0 - */ - public function deleteObject($urn) { - $this->getConnection()->deleteObject([ - 'Bucket' => $this->bucket, - 'Key' => $urn - ]); - } - } diff --git a/lib/private/Files/ObjectStore/S3ObjectTrait.php b/lib/private/Files/ObjectStore/S3ObjectTrait.php new file mode 100644 index 00000000000..3ba4da92b98 --- /dev/null +++ b/lib/private/Files/ObjectStore/S3ObjectTrait.php @@ -0,0 +1,82 @@ +<?php +/** + * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\Files\ObjectStore; + +use Aws\S3\S3Client; +use Psr\Http\Message\StreamInterface; + +trait S3ObjectTrait { + /** + * Returns the connection + * + * @return S3Client connected client + * @throws \Exception if connection could not be made + */ + abstract protected function getConnection(); + + /** + * @param string $urn the unified resource name used to identify the object + * @return resource stream with the read data + * @throws \Exception when something goes wrong, message will be logged + * @since 7.0.0 + */ + function readObject($urn) { + $client = $this->getConnection(); + $command = $client->getCommand('GetObject', [ + 'Bucket' => $this->bucket, + 'Key' => $urn + ]); + $command['@http']['stream'] = true; + $result = $client->execute($command); + /** @var StreamInterface $body */ + $body = $result['Body']; + + return $body->detach(); + } + + /** + * @param string $urn the unified resource name used to identify the object + * @param resource $stream stream with the data to write + * @throws \Exception when something goes wrong, message will be logged + * @since 7.0.0 + */ + function writeObject($urn, $stream) { + $this->getConnection()->putObject([ + 'Bucket' => $this->bucket, + 'Key' => $urn, + 'Body' => $stream + ]); + } + + /** + * @param string $urn the unified resource name used to identify the object + * @return void + * @throws \Exception when something goes wrong, message will be logged + * @since 7.0.0 + */ + function deleteObject($urn) { + $this->getConnection()->deleteObject([ + 'Bucket' => $this->bucket, + 'Key' => $urn + ]); + } +}
\ No newline at end of file |