2016-08-08 09:50:15 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2016-08-08 09:50:15 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2016-08-08 09:50:15 -04:00
|
|
|
*/
|
2025-03-20 10:22:27 -04:00
|
|
|
|
2016-08-08 09:50:15 -04:00
|
|
|
namespace OC\Files\ObjectStore;
|
|
|
|
|
|
2021-05-06 12:26:42 -04:00
|
|
|
use Aws\Result;
|
|
|
|
|
use Exception;
|
2016-08-08 09:50:15 -04:00
|
|
|
use OCP\Files\ObjectStore\IObjectStore;
|
2025-03-20 10:22:27 -04:00
|
|
|
use OCP\Files\ObjectStore\IObjectStoreMetaData;
|
2021-05-06 12:26:42 -04:00
|
|
|
use OCP\Files\ObjectStore\IObjectStoreMultiPartUpload;
|
2016-08-08 09:50:15 -04:00
|
|
|
|
2025-03-20 10:22:27 -04:00
|
|
|
class S3 implements IObjectStore, IObjectStoreMultiPartUpload, IObjectStoreMetaData {
|
2016-08-08 09:50:15 -04:00
|
|
|
use S3ConnectionTrait;
|
2017-06-07 10:29:13 -04:00
|
|
|
use S3ObjectTrait;
|
2016-08-08 09:50:15 -04:00
|
|
|
|
2024-10-08 09:13:16 -04:00
|
|
|
public function __construct(array $parameters) {
|
2022-03-15 10:50:08 -04:00
|
|
|
$parameters['primary_storage'] = true;
|
2016-08-08 09:50:15 -04:00
|
|
|
$this->parseParams($parameters);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string the container or bucket name where objects are stored
|
|
|
|
|
* @since 7.0.0
|
|
|
|
|
*/
|
2017-07-23 15:03:26 -04:00
|
|
|
public function getStorageId() {
|
2016-08-08 09:50:15 -04:00
|
|
|
return $this->id;
|
|
|
|
|
}
|
2021-05-06 12:26:42 -04:00
|
|
|
|
|
|
|
|
public function initiateMultipartUpload(string $urn): string {
|
|
|
|
|
$upload = $this->getConnection()->createMultipartUpload([
|
|
|
|
|
'Bucket' => $this->bucket,
|
|
|
|
|
'Key' => $urn,
|
2023-06-21 12:32:24 -04:00
|
|
|
] + $this->getSSECParameters());
|
2021-05-06 12:26:42 -04:00
|
|
|
$uploadId = $upload->get('UploadId');
|
|
|
|
|
if ($uploadId === null) {
|
|
|
|
|
throw new Exception('No upload id returned');
|
|
|
|
|
}
|
|
|
|
|
return (string)$uploadId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function uploadMultipartPart(string $urn, string $uploadId, int $partId, $stream, $size): Result {
|
|
|
|
|
return $this->getConnection()->uploadPart([
|
|
|
|
|
'Body' => $stream,
|
|
|
|
|
'Bucket' => $this->bucket,
|
|
|
|
|
'Key' => $urn,
|
|
|
|
|
'ContentLength' => $size,
|
|
|
|
|
'PartNumber' => $partId,
|
|
|
|
|
'UploadId' => $uploadId,
|
2023-06-21 12:32:24 -04:00
|
|
|
] + $this->getSSECParameters());
|
2021-05-06 12:26:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getMultipartUploads(string $urn, string $uploadId): array {
|
2023-04-01 10:56:15 -04:00
|
|
|
$parts = [];
|
|
|
|
|
$isTruncated = true;
|
|
|
|
|
$partNumberMarker = 0;
|
|
|
|
|
|
|
|
|
|
while ($isTruncated) {
|
|
|
|
|
$result = $this->getConnection()->listParts([
|
|
|
|
|
'Bucket' => $this->bucket,
|
|
|
|
|
'Key' => $urn,
|
|
|
|
|
'UploadId' => $uploadId,
|
|
|
|
|
'MaxParts' => 1000,
|
2025-03-20 10:22:27 -04:00
|
|
|
'PartNumberMarker' => $partNumberMarker,
|
2023-06-21 12:32:24 -04:00
|
|
|
] + $this->getSSECParameters());
|
2023-04-03 09:59:59 -04:00
|
|
|
$parts = array_merge($parts, $result->get('Parts') ?? []);
|
2023-04-01 10:56:15 -04:00
|
|
|
$isTruncated = $result->get('IsTruncated');
|
|
|
|
|
$partNumberMarker = $result->get('NextPartNumberMarker');
|
|
|
|
|
}
|
2023-06-21 12:32:24 -04:00
|
|
|
|
2023-04-01 10:56:15 -04:00
|
|
|
return $parts;
|
2021-05-06 12:26:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function completeMultipartUpload(string $urn, string $uploadId, array $result): int {
|
|
|
|
|
$this->getConnection()->completeMultipartUpload([
|
|
|
|
|
'Bucket' => $this->bucket,
|
|
|
|
|
'Key' => $urn,
|
|
|
|
|
'UploadId' => $uploadId,
|
|
|
|
|
'MultipartUpload' => ['Parts' => $result],
|
2023-06-21 12:32:24 -04:00
|
|
|
] + $this->getSSECParameters());
|
2021-05-06 12:26:42 -04:00
|
|
|
$stat = $this->getConnection()->headObject([
|
|
|
|
|
'Bucket' => $this->bucket,
|
|
|
|
|
'Key' => $urn,
|
2023-07-30 16:54:58 -04:00
|
|
|
] + $this->getSSECParameters());
|
2021-05-06 12:26:42 -04:00
|
|
|
return (int)$stat->get('ContentLength');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function abortMultipartUpload($urn, $uploadId): void {
|
|
|
|
|
$this->getConnection()->abortMultipartUpload([
|
|
|
|
|
'Bucket' => $this->bucket,
|
|
|
|
|
'Key' => $urn,
|
2025-03-20 10:22:27 -04:00
|
|
|
'UploadId' => $uploadId,
|
2021-05-06 12:26:42 -04:00
|
|
|
]);
|
|
|
|
|
}
|
2025-03-20 10:22:27 -04:00
|
|
|
|
2025-03-28 12:44:02 -04:00
|
|
|
private function parseS3Metadata(array $metadata): array {
|
|
|
|
|
$result = [];
|
|
|
|
|
foreach ($metadata as $key => $value) {
|
|
|
|
|
if (str_starts_with($key, 'x-amz-meta-')) {
|
2025-11-18 10:06:57 -05:00
|
|
|
if (str_starts_with($value, 'base64:')) {
|
|
|
|
|
$value = base64_decode(substr($value, 7));
|
|
|
|
|
}
|
2025-03-28 12:44:02 -04:00
|
|
|
$result[substr($key, strlen('x-amz-meta-'))] = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-20 10:22:27 -04:00
|
|
|
public function getObjectMetaData(string $urn): array {
|
|
|
|
|
$object = $this->getConnection()->headObject([
|
|
|
|
|
'Bucket' => $this->bucket,
|
|
|
|
|
'Key' => $urn
|
|
|
|
|
] + $this->getSSECParameters())->toArray();
|
|
|
|
|
return [
|
|
|
|
|
'mtime' => $object['LastModified'],
|
|
|
|
|
'etag' => trim($object['ETag'], '"'),
|
|
|
|
|
'size' => (int)($object['Size'] ?? $object['ContentLength']),
|
2025-03-28 12:44:02 -04:00
|
|
|
] + $this->parseS3Metadata($object['Metadata'] ?? []);
|
2025-03-20 10:22:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function listObjects(string $prefix = ''): \Iterator {
|
|
|
|
|
$results = $this->getConnection()->getPaginator('ListObjectsV2', [
|
|
|
|
|
'Bucket' => $this->bucket,
|
|
|
|
|
'Prefix' => $prefix,
|
|
|
|
|
] + $this->getSSECParameters());
|
|
|
|
|
|
|
|
|
|
foreach ($results as $result) {
|
|
|
|
|
if (is_array($result['Contents'])) {
|
|
|
|
|
foreach ($result['Contents'] as $object) {
|
|
|
|
|
yield [
|
|
|
|
|
'urn' => basename($object['Key']),
|
2025-03-20 10:59:25 -04:00
|
|
|
'metadata' => [
|
|
|
|
|
'mtime' => $object['LastModified'],
|
2025-03-20 10:22:27 -04:00
|
|
|
'etag' => trim($object['ETag'], '"'),
|
|
|
|
|
'size' => (int)($object['Size'] ?? $object['ContentLength']),
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-08 09:50:15 -04:00
|
|
|
}
|