2016-08-08 14:54:46 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2017-11-06 22:15:27 +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 S. Cat <33800996+sparrowjack63@users.noreply.github.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
*
|
2016-08-08 14:54:46 +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/>.
|
2016-08-08 14:54:46 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Files\ObjectStore;
|
|
|
|
|
2017-12-19 13:54:55 +03:00
|
|
|
use Aws\ClientResolver;
|
2016-08-08 14:54:46 +03:00
|
|
|
use Aws\S3\Exception\S3Exception;
|
|
|
|
use Aws\S3\S3Client;
|
2018-04-25 16:22:28 +03:00
|
|
|
use OCP\ILogger;
|
2016-08-08 14:54:46 +03:00
|
|
|
|
|
|
|
trait S3ConnectionTrait {
|
|
|
|
/** @var array */
|
|
|
|
protected $params;
|
|
|
|
|
|
|
|
/** @var S3Client */
|
|
|
|
protected $connection;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
protected $id;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
protected $bucket;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
protected $timeout;
|
|
|
|
|
|
|
|
protected $test;
|
|
|
|
|
|
|
|
protected function parseParams($params) {
|
|
|
|
if (empty($params['key']) || empty($params['secret']) || empty($params['bucket'])) {
|
|
|
|
throw new \Exception("Access Key, Secret and Bucket have to be configured.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->id = 'amazon::' . $params['bucket'];
|
|
|
|
|
|
|
|
$this->test = isset($params['test']);
|
|
|
|
$this->bucket = $params['bucket'];
|
2018-01-27 01:46:40 +03:00
|
|
|
$this->timeout = !isset($params['timeout']) ? 15 : $params['timeout'];
|
2016-08-08 14:54:46 +03:00
|
|
|
$params['region'] = empty($params['region']) ? 'eu-west-1' : $params['region'];
|
2017-04-20 18:08:22 +03:00
|
|
|
$params['hostname'] = empty($params['hostname']) ? 's3.' . $params['region'] . '.amazonaws.com' : $params['hostname'];
|
2016-08-08 14:54:46 +03:00
|
|
|
if (!isset($params['port']) || $params['port'] === '') {
|
|
|
|
$params['port'] = (isset($params['use_ssl']) && $params['use_ssl'] === false) ? 80 : 443;
|
|
|
|
}
|
|
|
|
$this->params = $params;
|
|
|
|
}
|
|
|
|
|
2019-05-24 15:02:03 +03:00
|
|
|
public function getBucket() {
|
|
|
|
return $this->bucket;
|
|
|
|
}
|
2016-08-08 14:54:46 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the connection
|
|
|
|
*
|
|
|
|
* @return S3Client connected client
|
|
|
|
* @throws \Exception if connection could not be made
|
|
|
|
*/
|
2019-05-24 15:02:03 +03:00
|
|
|
public function getConnection() {
|
2016-08-08 14:54:46 +03:00
|
|
|
if (!is_null($this->connection)) {
|
|
|
|
return $this->connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
$scheme = (isset($this->params['use_ssl']) && $this->params['use_ssl'] === false) ? 'http' : 'https';
|
|
|
|
$base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/';
|
|
|
|
|
|
|
|
$options = [
|
2017-04-20 18:08:22 +03:00
|
|
|
'version' => isset($this->params['version']) ? $this->params['version'] : 'latest',
|
|
|
|
'credentials' => [
|
|
|
|
'key' => $this->params['key'],
|
|
|
|
'secret' => $this->params['secret'],
|
|
|
|
],
|
|
|
|
'endpoint' => $base_url,
|
2016-08-08 14:54:46 +03:00
|
|
|
'region' => $this->params['region'],
|
2017-12-19 13:54:55 +03:00
|
|
|
'use_path_style_endpoint' => isset($this->params['use_path_style']) ? $this->params['use_path_style'] : false,
|
|
|
|
'signature_provider' => \Aws\or_chain([self::class, 'legacySignatureProvider'], ClientResolver::_default_signature_provider())
|
2016-08-08 14:54:46 +03:00
|
|
|
];
|
|
|
|
if (isset($this->params['proxy'])) {
|
2017-04-20 18:08:22 +03:00
|
|
|
$options['request.options'] = ['proxy' => $this->params['proxy']];
|
2016-08-08 14:54:46 +03:00
|
|
|
}
|
2017-12-19 13:54:55 +03:00
|
|
|
if (isset($this->params['legacy_auth']) && $this->params['legacy_auth']) {
|
|
|
|
$options['signature_version'] = 'v2';
|
|
|
|
}
|
2017-04-20 18:08:22 +03:00
|
|
|
$this->connection = new S3Client($options);
|
2016-08-08 14:54:46 +03:00
|
|
|
|
2019-02-26 11:32:14 +03:00
|
|
|
if (!$this->connection->isBucketDnsCompatible($this->bucket)) {
|
2019-02-26 11:50:47 +03:00
|
|
|
$logger = \OC::$server->getLogger();
|
2019-02-28 11:55:46 +03:00
|
|
|
$logger->debug('Bucket "' . $this->bucket . '" This bucket name is not dns compatible, it may contain invalid characters.',
|
2019-02-26 11:50:47 +03:00
|
|
|
['app' => 'objectstore']);
|
|
|
|
}
|
2019-02-26 11:32:14 +03:00
|
|
|
|
2019-02-26 11:50:47 +03:00
|
|
|
if (!$this->connection->doesBucketExist($this->bucket)) {
|
2018-04-05 18:26:09 +03:00
|
|
|
$logger = \OC::$server->getLogger();
|
2016-08-08 14:54:46 +03:00
|
|
|
try {
|
2018-04-05 18:26:09 +03:00
|
|
|
$logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']);
|
2019-02-26 11:32:14 +03:00
|
|
|
if (!$this->connection->isBucketDnsCompatible($this->bucket)) {
|
|
|
|
throw new \Exception("The bucket will not be created because the name is not dns compatible, please correct it: " . $this->bucket);
|
|
|
|
}
|
|
|
|
$this->connection->createBucket(array('Bucket' => $this->bucket));
|
2016-08-08 14:54:46 +03:00
|
|
|
$this->testTimeout();
|
|
|
|
} catch (S3Exception $e) {
|
2018-04-05 18:26:09 +03:00
|
|
|
$logger->logException($e, [
|
2018-01-17 17:21:56 +03:00
|
|
|
'message' => 'Invalid remote storage.',
|
2018-04-25 16:22:28 +03:00
|
|
|
'level' => ILogger::DEBUG,
|
2018-04-05 18:26:09 +03:00
|
|
|
'app' => 'objectstore',
|
2018-01-17 17:21:56 +03:00
|
|
|
]);
|
2018-04-05 18:26:09 +03:00
|
|
|
throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage());
|
2016-08-08 14:54:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-11 18:10:57 +03:00
|
|
|
// google cloud's s3 compatibility doesn't like the EncodingType parameter
|
|
|
|
if (strpos($base_url, 'storage.googleapis.com')) {
|
|
|
|
$this->connection->getHandlerList()->remove('s3.auto_encode');
|
|
|
|
}
|
|
|
|
|
2016-08-08 14:54:46 +03:00
|
|
|
return $this->connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* when running the tests wait to let the buckets catch up
|
|
|
|
*/
|
|
|
|
private function testTimeout() {
|
|
|
|
if ($this->test) {
|
|
|
|
sleep($this->timeout);
|
|
|
|
}
|
|
|
|
}
|
2017-12-19 13:54:55 +03:00
|
|
|
|
|
|
|
public static function legacySignatureProvider($version, $service, $region) {
|
|
|
|
switch ($version) {
|
|
|
|
case 'v2':
|
|
|
|
case 's3':
|
|
|
|
return new S3Signature();
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2016-08-08 14:54:46 +03:00
|
|
|
}
|