mirror of
https://github.com/nextcloud/server.git
synced 2026-04-15 22:11:17 -04:00
feat: add command to list objects
Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
parent
a324dbec2e
commit
f0cab800ab
6 changed files with 127 additions and 3 deletions
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -35,6 +35,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',
|
||||
|
|
|
|||
|
|
@ -50,6 +50,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',
|
||||
|
|
|
|||
119
apps/files/lib/Command/Object/ListObject.php
Normal file
119
apps/files/lib/Command/Object/ListObject.php
Normal file
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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']),
|
||||
],
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue