2017-06-07 17:29:13 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
|
|
|
|
*
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Florent <florent@coppint.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2017-11-06 17:56:42 +03:00
|
|
|
*
|
2017-06-07 17:29:13 +03:00
|
|
|
* @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
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-06-07 17:29:13 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Files\ObjectStore;
|
|
|
|
|
2019-02-14 17:51:40 +03:00
|
|
|
use Aws\S3\Exception\S3MultipartUploadException;
|
2019-01-28 16:51:19 +03:00
|
|
|
use Aws\S3\MultipartUploader;
|
2019-02-14 17:51:40 +03:00
|
|
|
use Aws\S3\ObjectUploader;
|
2017-06-07 17:29:13 +03:00
|
|
|
use Aws\S3\S3Client;
|
2019-02-14 17:51:40 +03:00
|
|
|
use Icewind\Streams\CallbackWrapper;
|
2020-03-19 16:32:25 +03:00
|
|
|
use OC\Files\Stream\SeekableHttpStream;
|
2017-06-07 17:29:13 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
public function readObject($urn) {
|
2020-03-19 16:32:25 +03:00
|
|
|
return SeekableHttpStream::open(function ($range) use ($urn) {
|
|
|
|
$command = $this->getConnection()->getCommand('GetObject', [
|
|
|
|
'Bucket' => $this->bucket,
|
|
|
|
'Key' => $urn,
|
|
|
|
'Range' => 'bytes=' . $range,
|
|
|
|
]);
|
|
|
|
$request = \Aws\serialize($command);
|
|
|
|
$headers = [];
|
|
|
|
foreach ($request->getHeaders() as $key => $values) {
|
|
|
|
foreach ($values as $value) {
|
|
|
|
$headers[] = "$key: $value";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$opts = [
|
|
|
|
'http' => [
|
|
|
|
'protocol_version' => 1.1,
|
|
|
|
'header' => $headers,
|
|
|
|
],
|
|
|
|
];
|
2017-06-07 17:29:13 +03:00
|
|
|
|
2020-03-19 16:32:25 +03:00
|
|
|
$context = stream_context_create($opts);
|
|
|
|
return fopen($request->getUri(), 'r', false, $context);
|
|
|
|
});
|
2017-06-07 17:29:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
public function writeObject($urn, $stream) {
|
2019-02-14 17:51:40 +03:00
|
|
|
$count = 0;
|
|
|
|
$countStream = CallbackWrapper::wrap($stream, function ($read) use (&$count) {
|
|
|
|
$count += $read;
|
|
|
|
});
|
|
|
|
|
|
|
|
$uploader = new MultipartUploader($this->getConnection(), $countStream, [
|
2019-01-28 16:51:19 +03:00
|
|
|
'bucket' => $this->bucket,
|
|
|
|
'key' => $urn,
|
2020-06-08 18:38:00 +03:00
|
|
|
'part_size' => $this->uploadPartSize,
|
2017-06-07 17:29:13 +03:00
|
|
|
]);
|
2019-02-14 17:51:40 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
$uploader->upload();
|
|
|
|
} catch (S3MultipartUploadException $e) {
|
2019-07-29 15:55:43 +03:00
|
|
|
// This is an empty file so just touch it then
|
2019-02-14 17:51:40 +03:00
|
|
|
if ($count === 0 && feof($countStream)) {
|
|
|
|
$uploader = new ObjectUploader($this->getConnection(), $this->bucket, $urn, '');
|
|
|
|
$uploader->upload();
|
|
|
|
} else {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose($countStream);
|
2017-06-07 17:29:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
public function deleteObject($urn) {
|
2017-06-07 17:29:13 +03:00
|
|
|
$this->getConnection()->deleteObject([
|
|
|
|
'Bucket' => $this->bucket,
|
2020-03-19 16:32:25 +03:00
|
|
|
'Key' => $urn,
|
2017-06-07 17:29:13 +03:00
|
|
|
]);
|
|
|
|
}
|
2018-12-10 19:20:45 +03:00
|
|
|
|
|
|
|
public function objectExists($urn) {
|
|
|
|
return $this->getConnection()->doesObjectExist($this->bucket, $urn);
|
|
|
|
}
|
2020-11-05 18:30:05 +03:00
|
|
|
|
|
|
|
public function copyObject($from, $to) {
|
|
|
|
$this->getConnection()->copy($this->getBucket(), $from, $this->getBucket(), $to);
|
|
|
|
}
|
2017-09-18 19:24:53 +03:00
|
|
|
}
|