summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Appelman <robin@icewind.nl>2017-06-07 16:29:13 +0200
committerRobin Appelman <robin@icewind.nl>2017-09-18 15:16:27 +0200
commitd70607104e7000ba008b4eb20bcf58a47b88fe35 (patch)
tree05604cd5a4d910ea45d7fc826b15ca1a4c45ca5c
parentdad18baec8274995ff384789ccc2577aabc6dde8 (diff)
downloadnextcloud-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>
-rw-r--r--apps/files_external/lib/Lib/Storage/AmazonS3.php49
-rw-r--r--lib/private/Files/ObjectStore/S3.php50
-rw-r--r--lib/private/Files/ObjectStore/S3ObjectTrait.php82
3 files changed, 97 insertions, 84 deletions
diff --git a/apps/files_external/lib/Lib/Storage/AmazonS3.php b/apps/files_external/lib/Lib/Storage/AmazonS3.php
index edcc084ae44..f26dcc3de02 100644
--- a/apps/files_external/lib/Lib/Storage/AmazonS3.php
+++ b/apps/files_external/lib/Lib/Storage/AmazonS3.php
@@ -41,14 +41,11 @@ use Aws\S3\Exception\S3Exception;
use Icewind\Streams\CallbackWrapper;
use Icewind\Streams\IteratorDirectory;
use OC\Files\ObjectStore\S3ConnectionTrait;
+use OC\Files\ObjectStore\S3ObjectTrait;
class AmazonS3 extends \OC\Files\Storage\Common {
use S3ConnectionTrait;
-
- /**
- * @var array
- */
- private static $tmpFiles = array();
+ use S3ObjectTrait;
/**
* @var int in seconds
@@ -91,7 +88,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
*
* @param array $params
*/
- public function updateLegacyId (array $params) {
+ public function updateLegacyId(array $params) {
$oldId = 'amazon::' . $params['key'] . md5($params['secret']);
// find by old id or bucket
@@ -187,7 +184,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
return false;
}
- private function batchDelete ($path = null) {
+ private function batchDelete($path = null) {
$params = array(
'Bucket' => $this->bucket
);
@@ -283,7 +280,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
$stat['atime'] = time();
return $stat;
- } catch(S3Exception $e) {
+ } catch (S3Exception $e) {
\OCP\Util::logException('files_external', $e);
return false;
}
@@ -300,7 +297,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
if ($this->getConnection()->doesObjectExist($this->bucket, $path)) {
return 'file';
}
- if ($this->getConnection()->doesObjectExist($this->bucket, $path.'/')) {
+ if ($this->getConnection()->doesObjectExist($this->bucket, $path . '/')) {
return 'dir';
}
} catch (S3Exception $e) {
@@ -319,11 +316,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
}
try {
- $this->getConnection()->deleteObject(array(
- 'Bucket' => $this->bucket,
- 'Key' => $path
- ));
- $this->testTimeout();
+ $this->deleteObject($path);
} catch (S3Exception $e) {
\OCP\Util::logException('files_external', $e);
return false;
@@ -338,21 +331,12 @@ class AmazonS3 extends \OC\Files\Storage\Common {
switch ($mode) {
case 'r':
case 'rb':
- $tmpFile = \OCP\Files::tmpFile();
- self::$tmpFiles[$tmpFile] = $path;
-
try {
- $this->getConnection()->getObject(array(
- 'Bucket' => $this->bucket,
- 'Key' => $path,
- 'SaveAs' => $tmpFile
- ));
+ return $this->readObject($path);
} catch (S3Exception $e) {
\OCP\Util::logException('files_external', $e);
return false;
}
-
- return fopen($tmpFile, 'r');
case 'w':
case 'wb':
case 'a':
@@ -372,12 +356,12 @@ class AmazonS3 extends \OC\Files\Storage\Common {
}
$tmpFile = \OCP\Files::tmpFile($ext);
if ($this->file_exists($path)) {
- $source = $this->fopen($path, 'r');
+ $source = $this->readObject($path);
file_put_contents($tmpFile, $source);
}
$handle = fopen($tmpFile, $mode);
- return CallbackWrapper::wrap($handle, null, null, function() use ($path, $tmpFile) {
+ return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
$this->writeBack($tmpFile, $path);
});
}
@@ -398,7 +382,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
$fileType = $this->filetype($path);
try {
if ($fileType !== false) {
- if ($fileType === 'dir' && ! $this->isRoot($path)) {
+ if ($fileType === 'dir' && !$this->isRoot($path)) {
$path .= '/';
}
$this->getConnection()->copyObject([
@@ -522,14 +506,9 @@ class AmazonS3 extends \OC\Files\Storage\Common {
public function writeBack($tmpFile, $path) {
try {
- $this->getConnection()->putObject(array(
- 'Bucket' => $this->bucket,
- 'Key' => $this->cleanKey($path),
- 'SourceFile' => $tmpFile,
- 'ContentType' => \OC::$server->getMimeTypeDetector()->detect($tmpFile),
- 'ContentLength' => filesize($tmpFile)
- ));
- $this->testTimeout();
+ $source = $this->fopen($tmpFile, 'r');
+ $this->writeObject($path, $source);
+ fclose($source);
unlink($tmpFile);
} catch (S3Exception $e) {
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