aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Appelman <robin@icewind.nl>2025-03-20 15:59:25 +0100
committerRobin Appelman <robin@icewind.nl>2025-03-31 14:30:40 +0200
commitfcde776683ad5c844e82770e66ef814105d6a7c8 (patch)
tree6135c3d4cfd37a54dadc9aec4e2a68e212d259f5
parentc3bc362f487cbea1458206e2b285210b38c0ea1e (diff)
downloadnextcloud-server-fcde776683ad5c844e82770e66ef814105d6a7c8.tar.gz
nextcloud-server-fcde776683ad5c844e82770e66ef814105d6a7c8.zip
feat: add command to list objects
Signed-off-by: Robin Appelman <robin@icewind.nl>
-rw-r--r--apps/files/appinfo/info.xml1
-rw-r--r--apps/files/composer/composer/autoload_classmap.php1
-rw-r--r--apps/files/composer/composer/autoload_static.php1
-rw-r--r--apps/files/lib/Command/Object/ListObject.php119
-rw-r--r--lib/private/Files/ObjectStore/S3.php4
-rw-r--r--lib/public/Files/ObjectStore/IObjectStoreMetaData.php4
6 files changed, 127 insertions, 3 deletions
diff --git a/apps/files/appinfo/info.xml b/apps/files/appinfo/info.xml
index add3da17d54..5f6f7e52c30 100644
--- a/apps/files/appinfo/info.xml
+++ b/apps/files/appinfo/info.xml
@@ -50,6 +50,7 @@
<command>OCA\Files\Command\Object\Get</command>
<command>OCA\Files\Command\Object\Put</command>
<command>OCA\Files\Command\Object\Info</command>
+ <command>OCA\Files\Command\Object\ListObject</command>
</commands>
<settings>
diff --git a/apps/files/composer/composer/autoload_classmap.php b/apps/files/composer/composer/autoload_classmap.php
index 0afe0261292..6489b691798 100644
--- a/apps/files/composer/composer/autoload_classmap.php
+++ b/apps/files/composer/composer/autoload_classmap.php
@@ -36,6 +36,7 @@ return array(
'OCA\\Files\\Command\\Object\\Delete' => $baseDir . '/../lib/Command/Object/Delete.php',
'OCA\\Files\\Command\\Object\\Get' => $baseDir . '/../lib/Command/Object/Get.php',
'OCA\\Files\\Command\\Object\\Info' => $baseDir . '/../lib/Command/Object/Info.php',
+ 'OCA\\Files\\Command\\Object\\ListObject' => $baseDir . '/../lib/Command/Object/ListObject.php',
'OCA\\Files\\Command\\Object\\ObjectUtil' => $baseDir . '/../lib/Command/Object/ObjectUtil.php',
'OCA\\Files\\Command\\Object\\Put' => $baseDir . '/../lib/Command/Object/Put.php',
'OCA\\Files\\Command\\Put' => $baseDir . '/../lib/Command/Put.php',
diff --git a/apps/files/composer/composer/autoload_static.php b/apps/files/composer/composer/autoload_static.php
index eab4a88b4ee..dadac010aea 100644
--- a/apps/files/composer/composer/autoload_static.php
+++ b/apps/files/composer/composer/autoload_static.php
@@ -51,6 +51,7 @@ class ComposerStaticInitFiles
'OCA\\Files\\Command\\Object\\Delete' => __DIR__ . '/..' . '/../lib/Command/Object/Delete.php',
'OCA\\Files\\Command\\Object\\Get' => __DIR__ . '/..' . '/../lib/Command/Object/Get.php',
'OCA\\Files\\Command\\Object\\Info' => __DIR__ . '/..' . '/../lib/Command/Object/Info.php',
+ 'OCA\\Files\\Command\\Object\\ListObject' => __DIR__ . '/..' . '/../lib/Command/Object/ListObject.php',
'OCA\\Files\\Command\\Object\\ObjectUtil' => __DIR__ . '/..' . '/../lib/Command/Object/ObjectUtil.php',
'OCA\\Files\\Command\\Object\\Put' => __DIR__ . '/..' . '/../lib/Command/Object/Put.php',
'OCA\\Files\\Command\\Put' => __DIR__ . '/..' . '/../lib/Command/Put.php',
diff --git a/apps/files/lib/Command/Object/ListObject.php b/apps/files/lib/Command/Object/ListObject.php
new file mode 100644
index 00000000000..d3b8feae7c7
--- /dev/null
+++ b/apps/files/lib/Command/Object/ListObject.php
@@ -0,0 +1,119 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCA\Files\Command\Object;
+
+use OC\Core\Command\Base;
+use OCP\Files\ObjectStore\IObjectStoreMetaData;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class ListObject extends Base {
+ private const CHUNK_SIZE = 100;
+
+ public function __construct(
+ private readonly ObjectUtil $objectUtils,
+ ) {
+ parent::__construct();
+ }
+
+ protected function configure(): void {
+ parent::configure();
+ $this
+ ->setName('files:object:list')
+ ->setDescription('List all objects in the object store')
+ ->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, "Bucket to list the objects from, only required in cases where it can't be determined from the config");
+ }
+
+ public function execute(InputInterface $input, OutputInterface $output): int {
+ $objectStore = $this->objectUtils->getObjectStore($input->getOption('bucket'), $output);
+ if (!$objectStore) {
+ return self::FAILURE;
+ }
+
+ if (!$objectStore instanceof IObjectStoreMetaData) {
+ $output->writeln('<error>Configured object store does currently not support listing objects</error>');
+ return self::FAILURE;
+ }
+ $outputType = $input->getOption('output');
+ $humanOutput = $outputType === self::OUTPUT_FORMAT_PLAIN;
+
+ if (!$humanOutput) {
+ $output->writeln('[');
+ }
+ $objects = $objectStore->listObjects();
+ $first = true;
+
+ foreach ($this->chunkIterator($objects, self::CHUNK_SIZE) as $chunk) {
+ if ($outputType === self::OUTPUT_FORMAT_PLAIN) {
+ $this->outputChunk($input, $output, $chunk);
+ } else {
+ foreach ($chunk as $object) {
+ if (!$first) {
+ $output->writeln(',');
+ }
+ $row = $this->formatObject($object, $humanOutput);
+ if ($outputType === self::OUTPUT_FORMAT_JSON_PRETTY) {
+ $output->write(json_encode($row, JSON_PRETTY_PRINT));
+ } else {
+ $output->write(json_encode($row));
+ }
+ $first = false;
+ }
+ }
+ }
+
+ if (!$humanOutput) {
+ $output->writeln("\n]");
+ }
+
+ return self::SUCCESS;
+ }
+
+ private function formatObject(array $object, bool $humanOutput): array {
+ $row = array_merge([
+ 'urn' => $object['urn'],
+ ], ($object['metadata'] ?? []));
+
+ if ($humanOutput && isset($row['size'])) {
+ $row['size'] = \OC_Helper::humanFileSize($row['size']);
+ }
+ if (isset($row['mtime'])) {
+ $row['mtime'] = $row['mtime']->format(\DateTimeImmutable::ATOM);
+ }
+ return $row;
+ }
+
+ private function outputChunk(InputInterface $input, OutputInterface $output, iterable $chunk): void {
+ $result = [];
+ $humanOutput = $input->getOption('output') === "plain";
+
+ foreach ($chunk as $object) {
+ $result[] = $this->formatObject($object, $humanOutput);
+ }
+ $this->writeTableInOutputFormat($input, $output, $result);
+ }
+
+ function chunkIterator(\Iterator $iterator, int $count): \Iterator {
+ $chunk = [];
+
+ for($i = 0; $iterator->valid(); $i++){
+ $chunk[] = $iterator->current();
+ $iterator->next();
+ if(count($chunk) == $count){
+ yield $chunk;
+ $chunk = [];
+ }
+ }
+
+ if(count($chunk)){
+ yield $chunk;
+ }
+ }
+}
diff --git a/lib/private/Files/ObjectStore/S3.php b/lib/private/Files/ObjectStore/S3.php
index 63c93ca8f3e..e970fb6ac14 100644
--- a/lib/private/Files/ObjectStore/S3.php
+++ b/lib/private/Files/ObjectStore/S3.php
@@ -118,8 +118,8 @@ class S3 implements IObjectStore, IObjectStoreMultiPartUpload, IObjectStoreMetaD
foreach ($result['Contents'] as $object) {
yield [
'urn' => basename($object['Key']),
- 'meta' => [
- 'mtime' => strtotime($object['LastModified']),
+ 'metadata' => [
+ 'mtime' => $object['LastModified'],
'etag' => trim($object['ETag'], '"'),
'size' => (int)($object['Size'] ?? $object['ContentLength']),
],
diff --git a/lib/public/Files/ObjectStore/IObjectStoreMetaData.php b/lib/public/Files/ObjectStore/IObjectStoreMetaData.php
index 48e1290850c..8359e83f573 100644
--- a/lib/public/Files/ObjectStore/IObjectStoreMetaData.php
+++ b/lib/public/Files/ObjectStore/IObjectStoreMetaData.php
@@ -30,7 +30,9 @@ interface IObjectStoreMetaData {
* If the object store implementation can do it efficiently, the metadata for each object is also included.
*
* @param string $prefix
- * @return \Iterator<array{urn: string, meta: ?ObjectMetaData}>
+ * @return \Iterator<array{urn: string, metadata: ?ObjectMetaData}>
+ *
+ * @since 32.0.0
*/
public function listObjects(string $prefix = ''): \Iterator;
}