S3ConnectionTrait.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Files\ObjectStore;
  25. use Aws\ClientResolver;
  26. use Aws\S3\Exception\S3Exception;
  27. use Aws\S3\S3Client;
  28. use OCP\ILogger;
  29. trait S3ConnectionTrait {
  30. /** @var array */
  31. protected $params;
  32. /** @var S3Client */
  33. protected $connection;
  34. /** @var string */
  35. protected $id;
  36. /** @var string */
  37. protected $bucket;
  38. /** @var int */
  39. protected $timeout;
  40. protected $test;
  41. protected function parseParams($params) {
  42. if (empty($params['key']) || empty($params['secret']) || empty($params['bucket'])) {
  43. throw new \Exception("Access Key, Secret and Bucket have to be configured.");
  44. }
  45. $this->id = 'amazon::' . $params['bucket'];
  46. $this->test = isset($params['test']);
  47. $this->bucket = $params['bucket'];
  48. $this->timeout = !isset($params['timeout']) ? 15 : $params['timeout'];
  49. $params['region'] = empty($params['region']) ? 'eu-west-1' : $params['region'];
  50. $params['hostname'] = empty($params['hostname']) ? 's3.' . $params['region'] . '.amazonaws.com' : $params['hostname'];
  51. if (!isset($params['port']) || $params['port'] === '') {
  52. $params['port'] = (isset($params['use_ssl']) && $params['use_ssl'] === false) ? 80 : 443;
  53. }
  54. $this->params = $params;
  55. }
  56. /**
  57. * Returns the connection
  58. *
  59. * @return S3Client connected client
  60. * @throws \Exception if connection could not be made
  61. */
  62. protected function getConnection() {
  63. if (!is_null($this->connection)) {
  64. return $this->connection;
  65. }
  66. $scheme = (isset($this->params['use_ssl']) && $this->params['use_ssl'] === false) ? 'http' : 'https';
  67. $base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/';
  68. $options = [
  69. 'version' => isset($this->params['version']) ? $this->params['version'] : 'latest',
  70. 'credentials' => [
  71. 'key' => $this->params['key'],
  72. 'secret' => $this->params['secret'],
  73. ],
  74. 'endpoint' => $base_url,
  75. 'region' => $this->params['region'],
  76. 'use_path_style_endpoint' => isset($this->params['use_path_style']) ? $this->params['use_path_style'] : false,
  77. 'signature_provider' => \Aws\or_chain([self::class, 'legacySignatureProvider'], ClientResolver::_default_signature_provider())
  78. ];
  79. if (isset($this->params['proxy'])) {
  80. $options['request.options'] = ['proxy' => $this->params['proxy']];
  81. }
  82. if (isset($this->params['legacy_auth']) && $this->params['legacy_auth']) {
  83. $options['signature_version'] = 'v2';
  84. }
  85. $this->connection = new S3Client($options);
  86. if (!$this->connection->isBucketDnsCompatible($this->bucket)) {
  87. throw new \Exception("The configured bucket name is invalid: " . $this->bucket);
  88. }
  89. if (!$this->connection->doesBucketExist($this->bucket)) {
  90. $logger = \OC::$server->getLogger();
  91. try {
  92. $logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']);
  93. $this->connection->createBucket(array(
  94. 'Bucket' => $this->bucket
  95. ));
  96. $this->testTimeout();
  97. } catch (S3Exception $e) {
  98. $logger->logException($e, [
  99. 'message' => 'Invalid remote storage.',
  100. 'level' => ILogger::DEBUG,
  101. 'app' => 'objectstore',
  102. ]);
  103. throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage());
  104. }
  105. }
  106. return $this->connection;
  107. }
  108. /**
  109. * when running the tests wait to let the buckets catch up
  110. */
  111. private function testTimeout() {
  112. if ($this->test) {
  113. sleep($this->timeout);
  114. }
  115. }
  116. public static function legacySignatureProvider($version, $service, $region) {
  117. switch ($version) {
  118. case 'v2':
  119. case 's3':
  120. return new S3Signature();
  121. default:
  122. return null;
  123. }
  124. }
  125. }