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

✨ allow to set the header to AES256 when uploading files to S3 #172

Closed
wants to merge 1 commit into from
Closed
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
27 changes: 22 additions & 5 deletions src/Fs.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class Fs extends FlysystemFs
public const STORAGE_REDUCED_REDUNDANCY = 'REDUCED_REDUNDANCY';
public const STORAGE_STANDARD_IA = 'STANDARD_IA';

protected const SERVER_SIDE_ENCRYPTION = [
'S3_MANAGED' => 'AES256',
];

/**
* Cache key to use for caching purposes
*/
Expand Down Expand Up @@ -145,6 +149,11 @@ public static function displayName(): string
*/
protected array $pathsToInvalidate = [];

/**
* @var string The server-side encryption header to use for storing the object in S3. ('' or 'S3_MANAGED').
*/
public string $serverSideEncryption = '';

// Public Methods
// =========================================================================

Expand Down Expand Up @@ -181,6 +190,7 @@ public function behaviors(): array
'subfolder',
'cfDistributionId',
'cfPrefix',
'serverSideEncryption'
],
];
return $behaviors;
Expand Down Expand Up @@ -281,7 +291,14 @@ public function getRootUrl(): ?string
protected function createAdapter(): FilesystemAdapter
{
$client = static::client($this->_getConfigArray(), $this->_getCredentials());
return new AwsS3V3Adapter($client, Craft::parseEnv($this->bucket), $this->_subfolder(), new PortableVisibilityConverter($this->visibility()), null, [], false);

$forwardedOptions = [];

if (array_key_exists($this->serverSideEncryption, self::SERVER_SIDE_ENCRYPTION)) {
$forwardedOptions['ServerSideEncryption'] = self::SERVER_SIDE_ENCRYPTION[$this->serverSideEncryption];
}

return new AwsS3V3Adapter($client, Craft::parseEnv($this->bucket), $this->_subfolder(), new PortableVisibilityConverter($this->visibility()), null, $forwardedOptions, false);
}

/**
Expand Down Expand Up @@ -363,10 +380,10 @@ public function purgeQueuedPaths(): void
'DistributionId' => Craft::parseEnv($this->cfDistributionId),
'InvalidationBatch' => [
'Paths' =>
[
'Quantity' => count($items),
'Items' => $items,
],
[
'Quantity' => count($items),
'Items' => $items,
],
'CallerReference' => 'Craft-' . StringHelper::randomString(24),
],
]
Expand Down
19 changes: 19 additions & 0 deletions src/templates/fsSettings.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,23 @@
errors: fs.getErrors('cfPrefix')
}) }}

{% set sseInput %}
{{ forms.select({
name: 'serverSideEncryption',
options: [
{ label: 'None', value: '' },
{ label: 'S3 Managed', value: 'S3_MANAGED' }
],
value: fs.serverSideEncryption
}) }}
{% endset %}

{{ forms.field({
label: "Server-side encryption"|t('aws-s3'),
id: 'serverSideEncryption',
instructions: "Specifies which type of server-side encryption the object must be stored with in S3.",
required: false,
errors: fs.getErrors('serverSideEncryption'),
}, sseInput) }}

{% do view.registerAssetBundle("craft\\awss3\\AwsS3Bundle") %}