S3ObjectTrait.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\MultipartUploader;
  25. use Aws\S3\S3Client;
  26. const S3_UPLOAD_PART_SIZE = 524288000; // 500MB
  27. trait S3ObjectTrait {
  28. /**
  29. * Returns the connection
  30. *
  31. * @return S3Client connected client
  32. * @throws \Exception if connection could not be made
  33. */
  34. abstract protected function getConnection();
  35. /**
  36. * @param string $urn the unified resource name used to identify the object
  37. * @return resource stream with the read data
  38. * @throws \Exception when something goes wrong, message will be logged
  39. * @since 7.0.0
  40. */
  41. function readObject($urn) {
  42. $client = $this->getConnection();
  43. $command = $client->getCommand('GetObject', [
  44. 'Bucket' => $this->bucket,
  45. 'Key' => $urn
  46. ]);
  47. $request = \Aws\serialize($command);
  48. $headers = [];
  49. foreach ($request->getHeaders() as $key => $values) {
  50. foreach ($values as $value) {
  51. $headers[] = "$key: $value";
  52. }
  53. }
  54. $opts = [
  55. 'http' => [
  56. 'header' => $headers
  57. ]
  58. ];
  59. $context = stream_context_create($opts);
  60. return fopen($request->getUri(), 'r', false, $context);
  61. }
  62. /**
  63. * @param string $urn the unified resource name used to identify the object
  64. * @param resource $stream stream with the data to write
  65. * @throws \Exception when something goes wrong, message will be logged
  66. * @since 7.0.0
  67. */
  68. function writeObject($urn, $stream) {
  69. $uploader = new MultipartUploader($this->getConnection(), $stream, [
  70. 'bucket' => $this->bucket,
  71. 'key' => $urn,
  72. 'part_size' => S3_UPLOAD_PART_SIZE
  73. ]);
  74. $uploader->upload();
  75. }
  76. /**
  77. * @param string $urn the unified resource name used to identify the object
  78. * @return void
  79. * @throws \Exception when something goes wrong, message will be logged
  80. * @since 7.0.0
  81. */
  82. function deleteObject($urn) {
  83. $this->getConnection()->deleteObject([
  84. 'Bucket' => $this->bucket,
  85. 'Key' => $urn
  86. ]);
  87. }
  88. public function objectExists($urn) {
  89. return $this->getConnection()->doesObjectExist($this->bucket, $urn);
  90. }
  91. }