Skip to content

Commit 69e8f48

Browse files
committed
feat: store original storage id and path in object store metadata
Signed-off-by: Robin Appelman <[email protected]>
1 parent 94114b9 commit 69e8f48

4 files changed

Lines changed: 29 additions & 4 deletions

File tree

lib/private/Files/ObjectStore/ObjectStoreStorage.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,8 @@ public function writeStream(string $path, $stream, ?int $size = null): int {
482482
$mimetype = $mimetypeDetector->detectPath($path);
483483
$metadata = [
484484
'mimetype' => $mimetype,
485+
'original-storage' => $this->getId(),
486+
'original-path' => $path,
485487
];
486488

487489
$stat['mimetype'] = $mimetype;

lib/private/Files/ObjectStore/S3.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ public function abortMultipartUpload($urn, $uploadId): void {
9595
]);
9696
}
9797

98+
private function parseS3Metadata(array $metadata): array {
99+
$result = [];
100+
foreach ($metadata as $key => $value) {
101+
if (str_starts_with($key, 'x-amz-meta-')) {
102+
$result[substr($key, strlen('x-amz-meta-'))] = $value;
103+
}
104+
}
105+
return $result;
106+
}
107+
98108
public function getObjectMetaData(string $urn): array {
99109
$object = $this->getConnection()->headObject([
100110
'Bucket' => $this->bucket,
@@ -104,7 +114,7 @@ public function getObjectMetaData(string $urn): array {
104114
'mtime' => $object['LastModified'],
105115
'etag' => trim($object['ETag'], '"'),
106116
'size' => (int)($object['Size'] ?? $object['ContentLength']),
107-
];
117+
] + $this->parseS3Metadata($object['Metadata'] ?? []);
108118
}
109119

110120
public function listObjects(string $prefix = ''): \Iterator {

lib/private/Files/ObjectStore/S3ObjectTrait.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ public function readObject($urn) {
7777
return $fh;
7878
}
7979

80+
private function buildS3Metadata(array $metadata): array {
81+
$result = [];
82+
foreach ($metadata as $key => $value) {
83+
$result['x-amz-meta-' . $key] = $value;
84+
}
85+
return $result;
86+
}
8087

8188
/**
8289
* Single object put helper
@@ -87,12 +94,15 @@ public function readObject($urn) {
8794
* @throws \Exception when something goes wrong, message will be logged
8895
*/
8996
protected function writeSingle(string $urn, StreamInterface $stream, array $metaData): void {
97+
$mimetype = $metaData['mimetype'] ?? null;
98+
unset($metaData['mimetype']);
9099
$this->getConnection()->putObject([
91100
'Bucket' => $this->bucket,
92101
'Key' => $urn,
93102
'Body' => $stream,
94103
'ACL' => 'private',
95-
'ContentType' => $metaData['mimetype'] ?? null,
104+
'ContentType' => $mimetype,
105+
'Metadata' => $this->buildS3Metadata($metaData),
96106
'StorageClass' => $this->storageClass,
97107
] + $this->getSSECParameters());
98108
}
@@ -107,13 +117,16 @@ protected function writeSingle(string $urn, StreamInterface $stream, array $meta
107117
* @throws \Exception when something goes wrong, message will be logged
108118
*/
109119
protected function writeMultiPart(string $urn, StreamInterface $stream, array $metaData): void {
120+
$mimetype = $metaData['mimetype'] ?? null;
121+
unset($metaData['mimetype']);
110122
$uploader = new MultipartUploader($this->getConnection(), $stream, [
111123
'bucket' => $this->bucket,
112124
'concurrency' => $this->concurrency,
113125
'key' => $urn,
114126
'part_size' => $this->uploadPartSize,
115127
'params' => [
116-
'ContentType' => $metaData['mimetype'] ?? null,
128+
'ContentType' => $mimetype,
129+
'Metadata' => $this->buildS3Metadata($metaData),
117130
'StorageClass' => $this->storageClass,
118131
] + $this->getSSECParameters(),
119132
]);

lib/public/Files/ObjectStore/IObjectStoreMetaData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* Interface IObjectStoreMetaData
1111
*
12-
* @psalm-type ObjectMetaData = array{mtime?: \DateTime, etag?: string, size?: int, mimetype?: string, filename?: string}
12+
* @psalm-type ObjectMetaData = array{mtime?: \DateTime, etag?: string, size?: int, mimetype?: string, filename?: string, original-path?: string, original-storage?: int}
1313
*
1414
* @since 32.0.0
1515
*/

0 commit comments

Comments
 (0)