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\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) {

View File

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

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