Skip to content

Commit f9f2e9a

Browse files
author
Carl Schwan
committed
perf(s3): Provide direct pre-signed download link
This is faster than going back to nextcloud to download the files. Signed-off-by: Carl Schwan <[email protected]>
1 parent 64c5200 commit f9f2e9a

File tree

6 files changed

+42
-1
lines changed

6 files changed

+42
-1
lines changed

lib/private/Files/ObjectStore/Azure.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,8 @@ public function objectExists($urn) {
117117
public function copyObject($from, $to) {
118118
$this->getBlobClient()->copyBlob($this->containerName, $to, $this->containerName, $from);
119119
}
120+
121+
public function preSignedUrl(string $urn, \DateTimeInterface $expiration): ?string {
122+
return null;
123+
}
120124
}

lib/private/Files/ObjectStore/ObjectStoreStorage.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,4 +812,16 @@ public function cancelChunkedWrite(string $targetPath, string $writeToken): void
812812
public function setPreserveCacheOnDelete(bool $preserve) {
813813
$this->preserveCacheItemsOnDelete = $preserve;
814814
}
815+
816+
public function getDirectDownload(string $path): array|false {
817+
$path = $this->normalizePath($path);
818+
$cacheEntry = $this->getCache()->get($path);
819+
820+
if (!$cacheEntry || $cacheEntry->getMimeType() === FileInfo::MIMETYPE_FOLDER) {
821+
return [];
822+
}
823+
824+
$url = $this->objectStore->preSignedUrl($this->getURN($cacheEntry->getId()), new \DateTime('+60 minutes'));
825+
return $url ? ['url' => $url] : [];
826+
}
815827
}

lib/private/Files/ObjectStore/S3ObjectTrait.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace OC\Files\ObjectStore;
88

99
use Aws\Command;
10+
use Aws\Exception\AwsException;
1011
use Aws\Exception\MultipartUploadException;
1112
use Aws\S3\Exception\S3MultipartUploadException;
1213
use Aws\S3\MultipartCopy;
@@ -279,4 +280,18 @@ public function copyObject($from, $to, array $options = []) {
279280
], $options));
280281
}
281282
}
283+
284+
public function preSignedUrl(string $urn, \DateTimeInterface $expiration): ?string {
285+
$command = $this->getConnection()->getCommand('GetObject', [
286+
'Bucket' => $this->getBucket(),
287+
'Key' => $urn,
288+
]);
289+
290+
try {
291+
return $this->getConnection()->createPresignedRequest($command, $expiration)->getUri();
292+
} catch (AwsException $exception) {
293+
$this->logger->warning("Unable to create pre-signed url: " . $exception->getMessage());
294+
return null;
295+
}
296+
}
282297
}

lib/private/Files/ObjectStore/StorageObjectStore.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,8 @@ public function objectExists($urn) {
7474
public function copyObject($from, $to) {
7575
$this->storage->copy($from, $to);
7676
}
77+
78+
public function getDirectDownload(string $path): array|false {
79+
return [];
80+
}
7781
}

lib/private/Files/Storage/Common.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ public function instanceOfStorage(string $class): bool {
446446
}
447447

448448
/**
449-
* A custom storage implementation can return an url for direct download of a give file.
449+
* A custom storage implementation can return a url for direct download of a give file.
450450
*
451451
* For now the returned array can hold the parameter url - in future more attributes might follow.
452452
*/

lib/public/Files/ObjectStore/IObjectStore.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,10 @@ public function objectExists($urn);
6363
* @since 21.0.0
6464
*/
6565
public function copyObject($from, $to);
66+
67+
/**
68+
* Get pre signed url for an object
69+
* @since 32.0.0
70+
*/
71+
public function preSignedUrl(string $urn, \DateTimeInterface $expiration): ?string;
6672
}

0 commit comments

Comments
 (0)