diff --git a/src/Storage/Device/Backblaze.php b/src/Storage/Device/Backblaze.php index 7420098e..0c0ecb36 100644 --- a/src/Storage/Device/Backblaze.php +++ b/src/Storage/Device/Backblaze.php @@ -2,6 +2,7 @@ namespace Utopia\Storage\Device; +use Exception; use Utopia\Storage\Storage; class Backblaze extends S3 @@ -61,4 +62,39 @@ public function getType(): string { return Storage::DEVICE_BACKBLAZE; } + + /** + * Get list of objects in the given path. + * + * @param string $prefix + * @param int $maxKeys + * @param string $continuationToken + * @return array + * + * @throws Exception + */ + protected function listObjects(string $prefix = '', int $maxKeys = self::MAX_PAGE_SIZE, string $continuationToken = ''): array + { + if ($maxKeys > S3::MAX_PAGE_SIZE) { + throw new Exception('Cannot list more than '.S3::MAX_PAGE_SIZE.' objects'); + } + + $uri = '/'; + $prefix = ltrim($prefix, '/'); /** S3 specific requirement that prefix should never contain a leading slash */ + $this->headers['content-type'] = 'text/plain'; + $this->headers['content-md5'] = \base64_encode(md5('', true)); + + $parameters = [ + 'prefix' => $prefix, + 'max-keys' => $maxKeys, + ]; + + if (! empty($continuationToken)) { + $parameters['continuation-token'] = $continuationToken; + } + + $response = $this->call(S3::METHOD_GET, $uri, '', $parameters); + + return $response->body; + } } diff --git a/src/Storage/Device/S3.php b/src/Storage/Device/S3.php index a182a1b1..9d459979 100644 --- a/src/Storage/Device/S3.php +++ b/src/Storage/Device/S3.php @@ -512,7 +512,7 @@ public function delete(string $path, bool $recursive = false): bool * * @throws Exception */ - private function listObjects(string $prefix = '', int $maxKeys = self::MAX_PAGE_SIZE, string $continuationToken = ''): array + protected function listObjects(string $prefix = '', int $maxKeys = self::MAX_PAGE_SIZE, string $continuationToken = ''): array { if ($maxKeys > self::MAX_PAGE_SIZE) { throw new Exception('Cannot list more than '.self::MAX_PAGE_SIZE.' objects'); @@ -826,7 +826,7 @@ private function getSignatureV4(string $method, string $uri, array $parameters = * * @throws \Exception */ - private function call(string $method, string $uri, string $data = '', array $parameters = [], bool $decode = true) + protected function call(string $method, string $uri, string $data = '', array $parameters = [], bool $decode = true) { $uri = $this->getAbsolutePath($uri); $url = 'https://'.$this->headers['host'].$uri.'?'.\http_build_query($parameters, '', '&', PHP_QUERY_RFC3986);