S3ConnectionTrait.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. trait S3ConnectionTrait {
  29. /** @var array */
  30. protected $params;
  31. /** @var S3Client */
  32. protected $connection;
  33. /** @var string */
  34. protected $id;
  35. /** @var string */
  36. protected $bucket;
  37. /** @var int */
  38. protected $timeout;
  39. protected $test;
  40. protected function parseParams($params) {
  41. if (empty($params['key']) || empty($params['secret']) || empty($params['bucket'])) {
  42. throw new \Exception("Access Key, Secret and Bucket have to be configured.");
  43. }
  44. $this->id = 'amazon::' . $params['bucket'];
  45. $this->test = isset($params['test']);
  46. $this->bucket = $params['bucket'];
  47. $this->timeout = !isset($params['timeout']) ? 15 : $params['timeout'];
  48. $params['region'] = empty($params['region']) ? 'eu-west-1' : $params['region'];
  49. $params['hostname'] = empty($params['hostname']) ? 's3.' . $params['region'] . '.amazonaws.com' : $params['hostname'];
  50. if (!isset($params['port']) || $params['port'] === '') {
  51. $params['port'] = (isset($params['use_ssl']) && $params['use_ssl'] === false) ? 80 : 443;
  52. }
  53. $this->params = $params;
  54. }
  55. /**
  56. * Returns the connection
  57. *
  58. * @return S3Client connected client
  59. * @throws \Exception if connection could not be made
  60. */
  61. protected function getConnection() {
  62. if (!is_null($this->connection)) {
  63. return $this->connection;
  64. }
  65. $scheme = (isset($this->params['use_ssl']) && $this->params['use_ssl'] === false) ? 'http' : 'https';
  66. $base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/';
  67. $options = [
  68. 'version' => isset($this->params['version']) ? $this->params['version'] : 'latest',
  69. 'credentials' => [
  70. 'key' => $this->params['key'],
  71. 'secret' => $this->params['secret'],
  72. ],
  73. 'endpoint' => $base_url,
  74. 'region' => $this->params['region'],
  75. 'use_path_style_endpoint' => isset($this->params['use_path_style']) ? $this->params['use_path_style'] : false,
  76. 'signature_provider' => \Aws\or_chain([self::class, 'legacySignatureProvider'], ClientResolver::_default_signature_provider())
  77. ];
  78. if (isset($this->params['proxy'])) {
  79. $options['request.options'] = ['proxy' => $this->params['proxy']];
  80. }
  81. if (isset($this->params['legacy_auth']) && $this->params['legacy_auth']) {
  82. $options['signature_version'] = 'v2';
  83. }
  84. $this->connection = new S3Client($options);
  85. if (!$this->connection->isBucketDnsCompatible($this->bucket)) {
  86. throw new \Exception("The configured bucket name is invalid: " . $this->bucket);
  87. }
  88. if (!$this->connection->doesBucketExist($this->bucket)) {
  89. try {
  90. $this->connection->createBucket(array(
  91. 'Bucket' => $this->bucket
  92. ));
  93. $this->testTimeout();
  94. } catch (S3Exception $e) {
  95. \OC::$server->getLogger()->logException($e, [
  96. 'message' => 'Invalid remote storage.',
  97. 'level' => \OCP\Util::DEBUG,
  98. 'app' => 'files_external',
  99. ]);
  100. throw new \Exception('Creation of bucket failed. ' . $e->getMessage());
  101. }
  102. }
  103. return $this->connection;
  104. }
  105. /**
  106. * when running the tests wait to let the buckets catch up
  107. */
  108. private function testTimeout() {
  109. if ($this->test) {
  110. sleep($this->timeout);
  111. }
  112. }
  113. public static function legacySignatureProvider($version, $service, $region) {
  114. switch ($version) {
  115. case 'v2':
  116. case 's3':
  117. return new S3Signature();
  118. default:
  119. return null;
  120. }
  121. }
  122. }