reuse object read/write/delete logic in s3 implementations

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2017-06-07 16:29:13 +02:00
parent dad18baec8
commit d70607104e
No known key found for this signature in database
GPG Key ID: CBCA68FBAEBF98C9
3 changed files with 97 additions and 84 deletions

View File

@ -41,14 +41,11 @@ use Aws\S3\Exception\S3Exception;
use Icewind\Streams\CallbackWrapper; use Icewind\Streams\CallbackWrapper;
use Icewind\Streams\IteratorDirectory; use Icewind\Streams\IteratorDirectory;
use OC\Files\ObjectStore\S3ConnectionTrait; use OC\Files\ObjectStore\S3ConnectionTrait;
use OC\Files\ObjectStore\S3ObjectTrait;
class AmazonS3 extends \OC\Files\Storage\Common { class AmazonS3 extends \OC\Files\Storage\Common {
use S3ConnectionTrait; use S3ConnectionTrait;
use S3ObjectTrait;
/**
* @var array
*/
private static $tmpFiles = array();
/** /**
* @var int in seconds * @var int in seconds
@ -319,11 +316,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
} }
try { try {
$this->getConnection()->deleteObject(array( $this->deleteObject($path);
'Bucket' => $this->bucket,
'Key' => $path
));
$this->testTimeout();
} catch (S3Exception $e) { } catch (S3Exception $e) {
\OCP\Util::logException('files_external', $e); \OCP\Util::logException('files_external', $e);
return false; return false;
@ -338,21 +331,12 @@ class AmazonS3 extends \OC\Files\Storage\Common {
switch ($mode) { switch ($mode) {
case 'r': case 'r':
case 'rb': case 'rb':
$tmpFile = \OCP\Files::tmpFile();
self::$tmpFiles[$tmpFile] = $path;
try { try {
$this->getConnection()->getObject(array( return $this->readObject($path);
'Bucket' => $this->bucket,
'Key' => $path,
'SaveAs' => $tmpFile
));
} catch (S3Exception $e) { } catch (S3Exception $e) {
\OCP\Util::logException('files_external', $e); \OCP\Util::logException('files_external', $e);
return false; return false;
} }
return fopen($tmpFile, 'r');
case 'w': case 'w':
case 'wb': case 'wb':
case 'a': case 'a':
@ -372,7 +356,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
} }
$tmpFile = \OCP\Files::tmpFile($ext); $tmpFile = \OCP\Files::tmpFile($ext);
if ($this->file_exists($path)) { if ($this->file_exists($path)) {
$source = $this->fopen($path, 'r'); $source = $this->readObject($path);
file_put_contents($tmpFile, $source); file_put_contents($tmpFile, $source);
} }
@ -522,14 +506,9 @@ class AmazonS3 extends \OC\Files\Storage\Common {
public function writeBack($tmpFile, $path) { public function writeBack($tmpFile, $path) {
try { try {
$this->getConnection()->putObject(array( $source = $this->fopen($tmpFile, 'r');
'Bucket' => $this->bucket, $this->writeObject($path, $source);
'Key' => $this->cleanKey($path), fclose($source);
'SourceFile' => $tmpFile,
'ContentType' => \OC::$server->getMimeTypeDetector()->detect($tmpFile),
'ContentLength' => filesize($tmpFile)
));
$this->testTimeout();
unlink($tmpFile); unlink($tmpFile);
} catch (S3Exception $e) { } catch (S3Exception $e) {

View File

@ -22,10 +22,10 @@
namespace OC\Files\ObjectStore; namespace OC\Files\ObjectStore;
use OCP\Files\ObjectStore\IObjectStore; use OCP\Files\ObjectStore\IObjectStore;
use Psr\Http\Message\StreamInterface;
class S3 implements IObjectStore { class S3 implements IObjectStore {
use S3ConnectionTrait; use S3ConnectionTrait;
use S3ObjectTrait;
public function __construct($parameters) { public function __construct($parameters) {
$this->parseParams($parameters); $this->parseParams($parameters);
@ -38,52 +38,4 @@ class S3 implements IObjectStore {
public function getStorageId() { public function getStorageId() {
return $this->id; 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
]);
}
} }

View File

@ -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
]);
}
}