S3ConnectionTrait.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OC\Files\ObjectStore;
  22. use Aws\S3\Exception\S3Exception;
  23. use Aws\S3\S3Client;
  24. trait S3ConnectionTrait {
  25. /** @var array */
  26. protected $params;
  27. /** @var S3Client */
  28. protected $connection;
  29. /** @var string */
  30. protected $id;
  31. /** @var string */
  32. protected $bucket;
  33. /** @var int */
  34. protected $timeout;
  35. protected $test;
  36. protected function parseParams($params) {
  37. if (empty($params['key']) || empty($params['secret']) || empty($params['bucket'])) {
  38. throw new \Exception("Access Key, Secret and Bucket have to be configured.");
  39. }
  40. $this->id = 'amazon::' . $params['bucket'];
  41. $this->test = isset($params['test']);
  42. $this->bucket = $params['bucket'];
  43. $this->timeout = (!isset($params['timeout'])) ? 15 : $params['timeout'];
  44. $params['region'] = empty($params['region']) ? 'eu-west-1' : $params['region'];
  45. $params['hostname'] = empty($params['hostname']) ? 's3.amazonaws.com' : $params['hostname'];
  46. if (!isset($params['port']) || $params['port'] === '') {
  47. $params['port'] = (isset($params['use_ssl']) && $params['use_ssl'] === false) ? 80 : 443;
  48. }
  49. $this->params = $params;
  50. }
  51. /**
  52. * Returns the connection
  53. *
  54. * @return S3Client connected client
  55. * @throws \Exception if connection could not be made
  56. */
  57. protected function getConnection() {
  58. if (!is_null($this->connection)) {
  59. return $this->connection;
  60. }
  61. $scheme = (isset($this->params['use_ssl']) && $this->params['use_ssl'] === false) ? 'http' : 'https';
  62. $base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/';
  63. $options = [
  64. 'key' => $this->params['key'],
  65. 'secret' => $this->params['secret'],
  66. 'base_url' => $base_url,
  67. 'region' => $this->params['region'],
  68. S3Client::COMMAND_PARAMS => [
  69. 'PathStyle' => isset($this->params['use_path_style']) ? $this->params['use_path_style'] : false,
  70. ]
  71. ];
  72. if (isset($this->params['proxy'])) {
  73. $options[S3Client::REQUEST_OPTIONS] = ['proxy' => $this->params['proxy']];
  74. }
  75. $this->connection = S3Client::factory($options);
  76. if (!$this->connection->isValidBucketName($this->bucket)) {
  77. throw new \Exception("The configured bucket name is invalid.");
  78. }
  79. if (!$this->connection->doesBucketExist($this->bucket)) {
  80. try {
  81. $this->connection->createBucket(array(
  82. 'Bucket' => $this->bucket
  83. ));
  84. $this->connection->waitUntilBucketExists(array(
  85. 'Bucket' => $this->bucket,
  86. 'waiter.interval' => 1,
  87. 'waiter.max_attempts' => 15
  88. ));
  89. $this->testTimeout();
  90. } catch (S3Exception $e) {
  91. \OCP\Util::logException('files_external', $e);
  92. throw new \Exception('Creation of bucket failed. ' . $e->getMessage());
  93. }
  94. }
  95. return $this->connection;
  96. }
  97. /**
  98. * when running the tests wait to let the buckets catch up
  99. */
  100. private function testTimeout() {
  101. if ($this->test) {
  102. sleep($this->timeout);
  103. }
  104. }
  105. }