Skip to content

Commit

Permalink
Merge pull request #48 from batabana/clear-acl-settings
Browse files Browse the repository at this point in the history
Fix acl target settings
  • Loading branch information
kdambekalns authored Aug 4, 2021
2 parents ab97471 + 87a866d commit 586c9c4
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions Classes/S3Target.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ class S3Target implements TargetInterface
{
/**
* The ACL when uploading a file
* @Flow\InjectConfiguration(package="Flownative.Aws.S3", path="profiles.default.acl")
* @var string
*/
protected $acl;

/**
* The default ACL
* @Flow\InjectConfiguration(package="Flownative.Aws.S3", path="profiles.default.acl")
* @var string
*/
protected $defaultAcl;

/**
* Name which identifies this resource target
*
Expand Down Expand Up @@ -72,13 +78,6 @@ class S3Target implements TargetInterface
*/
protected $unpublishResources = true;

/**
* If `true` (default) the S3 ACL is set to `public-read`. If `false` no ACL option will be set.
*
* @var boolean
*/
protected $accessPolicyEnabled = true;

/**
* Internal cache for known storages, indexed by storage name
*
Expand Down Expand Up @@ -147,9 +146,6 @@ public function __construct($name, array $options = array())
case 'unpublishResources':
$this->unpublishResources = (bool)$value;
break;
case 'accessPolicyEnabled':
$this->accessPolicyEnabled = (bool)$value;
break;
case 'acl':
$this->acl = (string)$value;
break;
Expand Down Expand Up @@ -194,6 +190,16 @@ public function getKeyPrefix()
return $this->keyPrefix;
}

/**
* Returns the ACL when uploading a file
*
* @return string
*/
public function getAcl()
{
return isset($this->acl) ? $this->acl : $this->defaultAcl;
}

/**
* Publishes the whole collection to this target
*
Expand Down Expand Up @@ -240,13 +246,15 @@ public function publishCollection(CollectionInterface $collection, callable $cal
$potentiallyObsoleteObjects[$objectName] = false;
} else {
$options = [
'ACL' => $this->acl,
'Bucket' => $this->bucketName,
'CopySource' => urlencode($storageBucketName . '/' . $storage->getKeyPrefix() . $object->getSha1()),
'ContentType' => $object->getMediaType(),
'MetadataDirective' => 'REPLACE',
'Key' => $objectName
];
if ($this->getAcl()) {
$options['ACL'] = $this->getAcl();
}
try {
$this->s3Client->copyObject($options);
$this->systemLogger->debug(sprintf('Successfully copied resource as object "%s" (SHA1: %s) from bucket "%s" to bucket "%s"', $objectName, $object->getSha1() ?: 'unknown', $storageBucketName, $this->bucketName));
Expand Down Expand Up @@ -317,13 +325,15 @@ public function publishResource(PersistentResource $resource, CollectionInterfac
$sourceObjectArn = $storage->getBucketName() . '/' . $storage->getKeyPrefix() . $resource->getSha1();
$objectName = $this->keyPrefix . $this->getRelativePublicationPathAndFilename($resource);
$options = [
'ACL' => $this->acl,
'Bucket' => $this->bucketName,
'CopySource' => urlencode($sourceObjectArn),
'ContentType'=> $resource->getMediaType(),
'MetadataDirective' => 'REPLACE',
'Key' => $objectName
];
if ($this->getAcl()) {
$options['ACL'] = $this->getAcl();
}
$this->s3Client->copyObject($options);
$this->systemLogger->debug(sprintf('Successfully published resource as object "%s" (SHA1: %s) by copying from bucket "%s" to bucket "%s"', $objectName, $resource->getSha1() ?: 'unknown', $storage->getBucketName(), $this->bucketName));
} catch (S3Exception $e) {
Expand Down Expand Up @@ -412,7 +422,7 @@ protected function publishFile($sourceStream, $relativeTargetPathAndFilename, Re
);

try {
$this->s3Client->upload($this->bucketName, $objectName, $sourceStream, $this->accessPolicyEnabled !== false ? 'public-read' : null, $options);
$this->s3Client->upload($this->bucketName, $objectName, $sourceStream, $this->getAcl() ? $this->getAcl() : null, $options);
$this->systemLogger->debug(sprintf('Successfully published resource as object "%s" in bucket "%s" with SHA1 hash "%s"', $objectName, $this->bucketName, $metaData->getSha1() ?: 'unknown'));
} catch (\Exception $e) {
$this->systemLogger->debug(sprintf('Failed publishing resource as object "%s" in bucket "%s" with SHA1 hash "%s": %s', $objectName, $this->bucketName, $metaData->getSha1() ?: 'unknown', $e->getMessage()));
Expand Down

0 comments on commit 586c9c4

Please sign in to comment.