S3.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. * @author Roeland Jago Douma <roeland@famdouma.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\Result;
  26. use Exception;
  27. use OCP\Files\ObjectStore\IObjectStore;
  28. use OCP\Files\ObjectStore\IObjectStoreMultiPartUpload;
  29. class S3 implements IObjectStore, IObjectStoreMultiPartUpload {
  30. use S3ConnectionTrait;
  31. use S3ObjectTrait;
  32. public function __construct($parameters) {
  33. $parameters['primary_storage'] = true;
  34. $this->parseParams($parameters);
  35. }
  36. /**
  37. * @return string the container or bucket name where objects are stored
  38. * @since 7.0.0
  39. */
  40. public function getStorageId() {
  41. return $this->id;
  42. }
  43. public function initiateMultipartUpload(string $urn): string {
  44. $upload = $this->getConnection()->createMultipartUpload([
  45. 'Bucket' => $this->bucket,
  46. 'Key' => $urn,
  47. ] + $this->getSSECParameters());
  48. $uploadId = $upload->get('UploadId');
  49. if ($uploadId === null) {
  50. throw new Exception('No upload id returned');
  51. }
  52. return (string)$uploadId;
  53. }
  54. public function uploadMultipartPart(string $urn, string $uploadId, int $partId, $stream, $size): Result {
  55. return $this->getConnection()->uploadPart([
  56. 'Body' => $stream,
  57. 'Bucket' => $this->bucket,
  58. 'Key' => $urn,
  59. 'ContentLength' => $size,
  60. 'PartNumber' => $partId,
  61. 'UploadId' => $uploadId,
  62. ] + $this->getSSECParameters());
  63. }
  64. public function getMultipartUploads(string $urn, string $uploadId): array {
  65. $parts = [];
  66. $isTruncated = true;
  67. $partNumberMarker = 0;
  68. while ($isTruncated) {
  69. $result = $this->getConnection()->listParts([
  70. 'Bucket' => $this->bucket,
  71. 'Key' => $urn,
  72. 'UploadId' => $uploadId,
  73. 'MaxParts' => 1000,
  74. 'PartNumberMarker' => $partNumberMarker
  75. ] + $this->getSSECParameters());
  76. $parts = array_merge($parts, $result->get('Parts') ?? []);
  77. $isTruncated = $result->get('IsTruncated');
  78. $partNumberMarker = $result->get('NextPartNumberMarker');
  79. }
  80. return $parts;
  81. }
  82. public function completeMultipartUpload(string $urn, string $uploadId, array $result): int {
  83. $this->getConnection()->completeMultipartUpload([
  84. 'Bucket' => $this->bucket,
  85. 'Key' => $urn,
  86. 'UploadId' => $uploadId,
  87. 'MultipartUpload' => ['Parts' => $result],
  88. ] + $this->getSSECParameters());
  89. $stat = $this->getConnection()->headObject([
  90. 'Bucket' => $this->bucket,
  91. 'Key' => $urn,
  92. ] + $this->getSSECParameters());
  93. return (int)$stat->get('ContentLength');
  94. }
  95. public function abortMultipartUpload($urn, $uploadId): void {
  96. $this->getConnection()->abortMultipartUpload([
  97. 'Bucket' => $this->bucket,
  98. 'Key' => $urn,
  99. 'UploadId' => $uploadId
  100. ]);
  101. }
  102. }