Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Add command to delete obejcts that are not registered locally #67

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion Classes/Command/S3CommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Flownative\Aws\S3\S3Target;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
use Neos\Flow\ResourceManagement\PersistentResource;
use Neos\Flow\ResourceManagement\ResourceManager;
use Neos\Flow\ResourceManagement\Storage\StorageObject;

Expand Down Expand Up @@ -39,7 +40,8 @@ class S3CommandController extends CommandController
*/
private $s3Client;

public function initializeObject() {
public function initializeObject()
{
$this->s3Client = new S3Client($this->s3DefaultProfile);
}

Expand Down Expand Up @@ -228,4 +230,50 @@ public function republishCommand(string $collection = 'persistent'): void
$this->output->progressFinish();
$this->outputLine();
}

/**
* @param string $s3Object
* @param string $keyPrefix
* @param bool $removeUnregistered Remove objetcs from storage, that are not
* @param bool $debug
* @return void
*/
public function diffS3ToLocalResourcesCommand(string $bucket, string $keyPrefix = '', bool $removeUnregistered = false, bool $debug = false)
{
$localMissingResources = 0;

$objects = $this->s3Client->getIterator('ListObjects', [
"Bucket" => $bucket,
"Prefix" => $keyPrefix,
]);

foreach ($objects as $s3Object) {
$persistentObjectIdentifier = substr($s3Object['Key'], strlen($keyPrefix) + 1);
$resource = $this->resourceManager->getResourceBySha1($persistentObjectIdentifier);
$this->output->progressAdvance();

if (!$resource instanceof PersistentResource) {
$debug && $this->outputFormatted(sprintf('<error>S3 object with identifier <b>%s</b> (%s) is missing in the resource management<error>', $persistentObjectIdentifier, $s3Object['Key']));

if ($removeUnregistered) {
$result = $this->s3Client->deleteObject([
'Bucket' => $bucket,
'Key' => $s3Object['Key']
]);

if ($result['@metadata']['statusCode'] >= 200 && $result['@metadata']['statusCode'] <= 300) {
$this->outputLine(sprintf('<success>Successfully deleted object with key "%s"', $s3Object['Key']));
} else {
$this->outputLine(sprintf('<error>Error while deleting object with key "%s". Details: "%s"', $s3Object['Key'], json_encode($result)));
}
}

$localMissingResources++;
}
}

$this->output->progressFinish();
$this->outputLine();
$this->outputLine(sprintf('%s resources are missing in the resource management.', $localMissingResources));
}
}
15 changes: 11 additions & 4 deletions Classes/S3Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ public function __construct(string $name, array $options = [])
switch ($key) {
case 'bucket':
$this->bucketName = $value;
break;
break;
case 'keyPrefix':
$this->keyPrefix = $value;
break;
break;
default:
if ($value !== null) {
throw new Exception(sprintf('An unknown option "%s" was specified in the configuration of the "%s" resource S3Storage. Please check your settings.', $key, $name), 1428928229);
Expand Down Expand Up @@ -288,11 +288,18 @@ public function importUploadedResource(array $uploadInfo, string $collectionName
*/
public function deleteResource(PersistentResource $resource): bool
{
$this->s3Client->deleteObject([
$result = $this->s3Client->deleteObject([
'Bucket' => $this->bucketName,
'Key' => $this->keyPrefix . $resource->getSha1()
]);
return true;

if ($result['@metadata']['statusCode'] >= 200 && $result['@metadata']['statusCode'] <= 300) {
$this->systemLogger->info(sprintf('<success>Successfully deleted object with key "%s"', $this->keyPrefix . $resource->getSha1()));
return true;
} else {
$this->systemLogger->error(sprintf('Could not delete resource with key "%s" from bucket "%s". Details: %s', $this->bucketName, $this->keyPrefix . $resource->getSha1(), json_encode($result)), LogEnvironment::fromMethodName(__METHOD__));
return false;
}
}

/**
Expand Down