Skip to content

Commit

Permalink
Merge pull request flownative#27 from mkuiphuis/master
Browse files Browse the repository at this point in the history
Make flow-aws-s3 compatible with Neos Flow 6.0.X
  • Loading branch information
kitsunet authored Nov 8, 2019
2 parents 396aa1b + c87887e commit 76daf3f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
13 changes: 7 additions & 6 deletions Classes/S3Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Neos\Flow\ResourceManagement\Storage\StorageObject;
use Neos\Flow\ResourceManagement\Storage\WritableStorageInterface;
use Neos\Flow\Utility\Environment;
use Psr\Log\LoggerInterface;

/**
* A resource storage based on AWS S3
Expand Down Expand Up @@ -75,7 +76,7 @@ class S3Storage implements WritableStorageInterface

/**
* @Flow\Inject
* @var \Neos\Flow\Log\SystemLoggerInterface
* @var LoggerInterface
*/
protected $systemLogger;

Expand Down Expand Up @@ -170,7 +171,7 @@ public function importResource($source, $collectionName)
stream_copy_to_stream($source, $target);
fclose($target);
} catch (\Exception $e) {
throw new Exception(sprintf('Could import the content stream to temporary file "%s".', $temporaryTargetPathAndFilename), 1428915486);
throw new Exception(sprintf('Could not import the content stream to temporary file "%s".', $temporaryTargetPathAndFilename), 1428915486);
}
} else {
try {
Expand Down Expand Up @@ -306,7 +307,7 @@ public function getStreamByResource(PersistentResource $resource)
return false;
}
$message = sprintf('Could not retrieve stream for resource %s (s3://%s/%s%s). %s', $resource->getFilename(), $this->bucketName, $this->keyPrefix, $resource->getSha1(), $e->getMessage());
$this->systemLogger->log($message, \LOG_ERR);
$this->systemLogger->error($message);
return false;
}
}
Expand All @@ -329,7 +330,7 @@ public function getStreamByResourcePath($relativePath)
return false;
}
$message = sprintf('Could not retrieve stream for resource (s3://%s/%s%s). %s', $this->bucketName, $this->keyPrefix, ltrim('/', $relativePath), $e->getMessage());
$this->systemLogger->log($message, \LOG_ERR);
$this->systemLogger->error($message);
return false;
}
}
Expand Down Expand Up @@ -417,9 +418,9 @@ protected function importTemporaryFile($temporaryPathAndFilename, $collectionNam
'ContentType' => $resource->getMediaType(),
'Key' => $objectName
]);
$this->systemLogger->log(sprintf('Successfully imported resource as object "%s" into bucket "%s" with MD5 hash "%s"', $objectName, $this->bucketName, $resource->getMd5() ?: 'unknown'), LOG_INFO);
$this->systemLogger->info(sprintf('Successfully imported resource as object "%s" into bucket "%s" with MD5 hash "%s"', $objectName, $this->bucketName, $resource->getMd5() ?: 'unknown'));
} else {
$this->systemLogger->log(sprintf('Did not import resource as object "%s" into bucket "%s" because that object already existed.', $objectName, $this->bucketName), LOG_INFO);
$this->systemLogger->info(sprintf('Did not import resource as object "%s" into bucket "%s" because that object already existed.', $objectName, $this->bucketName));
}

return $resource;
Expand Down
21 changes: 11 additions & 10 deletions Classes/S3Target.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Neos\Flow\ResourceManagement\ResourceManager;
use Neos\Flow\ResourceManagement\ResourceMetaDataInterface;
use Neos\Flow\ResourceManagement\Target\TargetInterface;
use Psr\Log\LoggerInterface;

/**
* A resource publishing target based on Amazon S3
Expand Down Expand Up @@ -75,7 +76,7 @@ class S3Target implements TargetInterface

/**
* @Flow\Inject
* @var \Neos\Flow\Log\SystemLoggerInterface
* @var LoggerInterface
*/
protected $systemLogger;

Expand Down Expand Up @@ -203,7 +204,7 @@ public function publishCollection(CollectionInterface $collection, callable $cal
/** @var \Neos\Flow\ResourceManagement\Storage\StorageObject $object */
$objectName = $this->keyPrefix . $this->getRelativePublicationPathAndFilename($object);
if (array_key_exists($objectName, $potentiallyObsoleteObjects)) {
$this->systemLogger->log(sprintf('The resource object "%s" (MD5: %s) has already been published to bucket "%s", no need to re-publish', $objectName, $object->getMd5() ?: 'unknown', $this->bucketName), LOG_DEBUG);
$this->systemLogger->debug(sprintf('The resource object "%s" (MD5: %s) has already been published to bucket "%s", no need to re-publish', $objectName, $object->getMd5() ?: 'unknown', $this->bucketName));
unset($potentiallyObsoleteObjects[$objectName]);
} else {
$options = array(
Expand All @@ -216,10 +217,10 @@ public function publishCollection(CollectionInterface $collection, callable $cal
);
try {
$this->s3Client->copyObject($options);
$this->systemLogger->log(sprintf('Successfully copied resource as object "%s" (MD5: %s) from bucket "%s" to bucket "%s"', $objectName, $object->getMd5() ?: 'unknown', $storageBucketName, $this->bucketName), LOG_DEBUG);
$this->systemLogger->debug(sprintf('Successfully copied resource as object "%s" (MD5: %s) from bucket "%s" to bucket "%s"', $objectName, $object->getMd5() ?: 'unknown', $storageBucketName, $this->bucketName));
} catch (S3Exception $e) {
$message = sprintf('Could not copy resource with SHA1 hash %s of collection %s from bucket %s to %s: %s', $object->getSha1(), $collection->getName(), $storageBucketName, $this->bucketName, $e->getMessage());
$this->systemLogger->logException($e);
$this->systemLogger->critical($e);
$this->messageCollector->append($message);
}
}
Expand All @@ -234,7 +235,7 @@ public function publishCollection(CollectionInterface $collection, callable $cal
}

foreach (array_keys($potentiallyObsoleteObjects) as $relativePathAndFilename) {
$this->systemLogger->log(sprintf('Deleted obsolete resource "%s" from bucket "%s"', $relativePathAndFilename, $this->bucketName), LOG_DEBUG);
$this->systemLogger->debug(sprintf('Deleted obsolete resource "%s" from bucket "%s"', $relativePathAndFilename, $this->bucketName));
$this->s3Client->deleteObject(array(
'Bucket' => $this->bucketName,
'Key' => $this->keyPrefix . $relativePathAndFilename
Expand Down Expand Up @@ -284,10 +285,10 @@ public function publishResource(PersistentResource $resource, CollectionInterfac
'Key' => $objectName
);
$this->s3Client->copyObject($options);
$this->systemLogger->log(sprintf('Successfully published resource as object "%s" (MD5: %s) by copying from bucket "%s" to bucket "%s"', $objectName, $resource->getMd5() ?: 'unknown', $storage->getBucketName(), $this->bucketName), LOG_DEBUG);
$this->systemLogger->debug(sprintf('Successfully published resource as object "%s" (MD5: %s) by copying from bucket "%s" to bucket "%s"', $objectName, $resource->getMd5() ?: 'unknown', $storage->getBucketName(), $this->bucketName));
} catch (S3Exception $e) {
$message = sprintf('Could not publish resource with SHA1 hash %s of collection %s (source object: %s) through "CopyObject" because the S3 client reported an error: %s', $resource->getSha1(), $collection->getName(), $sourceObjectArn, $e->getMessage());
$this->systemLogger->logException($e);
$this->systemLogger->critical($e);
$this->messageCollector->append($message);
}
} else {
Expand Down Expand Up @@ -315,7 +316,7 @@ public function unpublishResource(PersistentResource $resource)
'Bucket' => $this->bucketName,
'Key' => $objectName
));
$this->systemLogger->log(sprintf('Successfully unpublished resource as object "%s" (MD5: %s) from bucket "%s"', $objectName, $resource->getMd5() ?: 'unknown', $this->bucketName), LOG_DEBUG);
$this->systemLogger->debug(sprintf('Successfully unpublished resource as object "%s" (MD5: %s) from bucket "%s"', $objectName, $resource->getMd5() ?: 'unknown', $this->bucketName));
} catch (\Exception $e) {
}
}
Expand Down Expand Up @@ -367,9 +368,9 @@ protected function publishFile($sourceStream, $relativeTargetPathAndFilename, Re

try {
$this->s3Client->upload($this->bucketName, $objectName, $sourceStream, 'public-read', $options);
$this->systemLogger->log(sprintf('Successfully published resource as object "%s" in bucket "%s" with MD5 hash "%s"', $objectName, $this->bucketName, $metaData->getMd5() ?: 'unknown'), LOG_DEBUG);
$this->systemLogger->debug(sprintf('Successfully published resource as object "%s" in bucket "%s" with MD5 hash "%s"', $objectName, $this->bucketName, $metaData->getMd5() ?: 'unknown'));
} catch (\Exception $e) {
$this->systemLogger->log(sprintf('Failed publishing resource as object "%s" in bucket "%s" with MD5 hash "%s": %s', $objectName, $this->bucketName, $metaData->getMd5() ?: 'unknown', $e->getMessage()), LOG_DEBUG);
$this->systemLogger->debug(sprintf('Failed publishing resource as object "%s" in bucket "%s" with MD5 hash "%s": %s', $objectName, $this->bucketName, $metaData->getMd5() ?: 'unknown', $e->getMessage()));
if (is_resource($sourceStream)) {
fclose($sourceStream);
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"MIT"
],
"require": {
"neos/flow": "^4.0 || ^5.0",
"neos/flow": "^5.0 || ^6.0",
"aws/aws-sdk-php": "~3.0"
},
"autoload": {
Expand Down

0 comments on commit 76daf3f

Please sign in to comment.