S3ObjectTrait.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Files\ObjectStore;
  24. use Aws\S3\S3Client;
  25. const S3_UPLOAD_PART_SIZE = 524288000; // 500MB
  26. trait S3ObjectTrait {
  27. /**
  28. * Returns the connection
  29. *
  30. * @return S3Client connected client
  31. * @throws \Exception if connection could not be made
  32. */
  33. abstract protected function getConnection();
  34. /**
  35. * @param string $urn the unified resource name used to identify the object
  36. * @return resource stream with the read data
  37. * @throws \Exception when something goes wrong, message will be logged
  38. * @since 7.0.0
  39. */
  40. function readObject($urn) {
  41. $client = $this->getConnection();
  42. $command = $client->getCommand('GetObject', [
  43. 'Bucket' => $this->bucket,
  44. 'Key' => $urn
  45. ]);
  46. $request = \Aws\serialize($command);
  47. $headers = [];
  48. foreach ($request->getHeaders() as $key => $values) {
  49. foreach ($values as $value) {
  50. $headers[] = "$key: $value";
  51. }
  52. }
  53. $opts = [
  54. 'http' => [
  55. 'header' => $headers
  56. ]
  57. ];
  58. $context = stream_context_create($opts);
  59. return fopen($request->getUri(), 'r', false, $context);
  60. }
  61. /**
  62. * @param string $urn the unified resource name used to identify the object
  63. * @param resource $stream stream with the data to write
  64. * @throws \Exception when something goes wrong, message will be logged
  65. * @since 7.0.0
  66. */
  67. function writeObject($urn, $stream) {
  68. $this->getConnection()->upload($this->bucket, $urn, $stream, 'private', [
  69. 'mup_threshold' => S3_UPLOAD_PART_SIZE,
  70. 'part_size' => S3_UPLOAD_PART_SIZE
  71. ]);
  72. }
  73. /**
  74. * @param string $urn the unified resource name used to identify the object
  75. * @return void
  76. * @throws \Exception when something goes wrong, message will be logged
  77. * @since 7.0.0
  78. */
  79. function deleteObject($urn) {
  80. $this->getConnection()->deleteObject([
  81. 'Bucket' => $this->bucket,
  82. 'Key' => $urn
  83. ]);
  84. }
  85. public function objectExists($urn) {
  86. return $this->getConnection()->doesObjectExist($this->bucket, $urn);
  87. }
  88. }