Skip to content

Commit

Permalink
FEATURE: Add s3:republish command
Browse files Browse the repository at this point in the history
  • Loading branch information
kdambekalns committed Aug 6, 2021
1 parent ce2de55 commit a17f109
Showing 1 changed file with 70 additions and 13 deletions.
83 changes: 70 additions & 13 deletions Classes/Command/S3CommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
* */

use Aws\S3\BatchDelete;
use Aws\S3\Model\ClearBucket;
use Aws\S3\S3Client;
use Flownative\Aws\S3\S3Target;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
use Neos\Flow\ResourceManagement\ResourceManager;
use Neos\Flow\ResourceManagement\Storage\StorageObject;

/**
* S3 command controller for the Flownative.Aws.S3 package
Expand All @@ -24,6 +26,21 @@ class S3CommandController extends CommandController
*/
protected $s3DefaultProfile;

/**
* @Flow\Inject
* @var ResourceManager
*/
protected $resourceManager;

/**
* @var S3Client
*/
private $s3Client;

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

/**
* Checks the connection
*
Expand All @@ -40,12 +57,11 @@ class S3CommandController extends CommandController
public function connectCommand($bucket = null, $prefix = '')
{
try {
$s3Client = new S3Client($this->s3DefaultProfile);
if ($bucket !== null) {
$s3Client->registerStreamWrapper();
$this->s3Client->registerStreamWrapper();

$this->outputLine('Access list of objects in bucket "%s" with key prefix "%s" ...', [$bucket, $prefix]);
$s3Client->getPaginator('ListObjects', ['Bucket' => $bucket, 'Prefix' => $prefix]);
$this->s3Client->getPaginator('ListObjects', ['Bucket' => $bucket, 'Prefix' => $prefix]);

$options = array(
'Bucket' => $bucket,
Expand All @@ -55,17 +71,17 @@ public function connectCommand($bucket = null, $prefix = '')
'Key' => $prefix . 'Flownative.Aws.S3.ConnectionTest.txt'
);
$this->outputLine('Writing test object into bucket (arn:aws:s3:::%s/%s) ...', [$bucket, $options['Key']]);
$s3Client->putObject($options);
$this->s3Client->putObject($options);

$this->outputLine('Deleting test object from bucket ...');
$options = array(
'Bucket' => $bucket,
'Key' => $prefix . 'Flownative.Aws.S3.ConnectionTest.txt'
);
$s3Client->deleteObject($options);
$this->s3Client->deleteObject($options);
} else {
$this->outputLine('Listing buckets ...');
$s3Client->listBuckets();
$this->s3Client->listBuckets();
}
} catch (\Exception $e) {
$this->outputLine('<b>' . $e->getMessage() . '</b>');
Expand All @@ -89,8 +105,7 @@ public function connectCommand($bucket = null, $prefix = '')
public function listBucketsCommand()
{
try {
$s3Client = new S3Client($this->s3DefaultProfile);
$result = $s3Client->listBuckets();
$result = $this->s3Client->listBuckets();
} catch (\Exception $e) {
$this->outputLine($e->getMessage());
$this->quit(1);
Expand Down Expand Up @@ -122,8 +137,7 @@ public function listBucketsCommand()
public function flushBucketCommand($bucket)
{
try {
$s3Client = new S3Client($this->s3DefaultProfile);
$batchDelete = BatchDelete::fromListObjects($s3Client, ['Bucket' => $bucket]);
$batchDelete = BatchDelete::fromListObjects($this->s3Client, ['Bucket' => $bucket]);
$promise = $batchDelete->promise();
} catch (\Exception $e) {
$this->outputLine($e->getMessage());
Expand Down Expand Up @@ -158,8 +172,7 @@ public function uploadCommand($bucket, $file, $key = '')
}

try {
$s3Client = new S3Client($this->s3DefaultProfile);
$s3Client->putObject(array(
$this->s3Client->putObject(array(
'Key' => $key,
'Bucket' => $bucket,
'Body' => fopen('file://' . realpath($file), 'rb')
Expand All @@ -171,4 +184,48 @@ public function uploadCommand($bucket, $file, $key = '')

$this->outputLine('Successfully uploaded %s to %s::%s.', array($file, $bucket, $key));
}

/**
* Republish a collection
*
* This command forces publishing resources of the given collection by copying resources from the respective storage
* to target bucket.
*
* @param string $collection Name of the collection to publish
*/
public function republishCommand(string $collection = 'persistent'): void
{
$collectionName = $collection;
$collection = $this->resourceManager->getCollection($collectionName);
if (!$collection) {
$this->outputLine('<error>The collection %s does not exist.</error>', [$collectionName]);
exit(1);
}

$target = $collection->getTarget();
if (!$target instanceof S3Target) {
$this->outputLine('<error>The target defined in collection %s is not an S3 target.</error>', [$collectionName]);
exit(1);
}

$this->outputLine('Republishing collection ...');
$this->output->progressStart();
try {
foreach ($collection->getObjects() as $object) {
/** @var StorageObject $object */
$resource = $this->resourceManager->getResourceBySha1($object->getSha1());
if ($resource) {
$target->publishResource($resource, $collection);
}
$this->output->progressAdvance();
}
} catch (\Exception $e) {
$this->outputLine('<error>Publishing failed</error>');
$this->outputLine($e->getMessage());
$this->outputLine(get_class($e));
exit(2);
}
$this->output->progressFinish();
$this->outputLine();
}
}

0 comments on commit a17f109

Please sign in to comment.