From c6bb7c5774758ae27230d941ed87c793d7d27ad9 Mon Sep 17 00:00:00 2001 From: samandeggs Date: Tue, 1 Nov 2022 13:10:45 +1300 Subject: [PATCH 1/6] Updating README.md to remove commit messages and add references to how to configure the PublicCDNAdapter to support a custom cdn asset path name. Updating PublicCDNAdapter.php to support a custom path variable, instead of always utilizing /assets/ taken from ASSET_DIR in silverstripe. --- README.md | 90 +++++++++++++++++++------------- src/Adapter/PublicCDNAdapter.php | 8 ++- 2 files changed, 60 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index c5c5e6b..2142ef0 100644 --- a/README.md +++ b/README.md @@ -11,37 +11,35 @@ policy. The module requires a few environment variables to be set. Full details can be seen in the `SilverStripeS3AdapterTrait` trait. These are mandatory. -* `AWS_REGION`: The AWS region your S3 bucket is hosted in (e.g. `eu-central-1`) -* `AWS_BUCKET_NAME`: The name of the S3 bucket to store assets in. +- `AWS_REGION`: The AWS region your S3 bucket is hosted in (e.g. `eu-central-1`) +- `AWS_BUCKET_NAME`: The name of the S3 bucket to store assets in. If running outside of an EC2 instance it will be necessary to specify an API key and secret. -* `AWS_ACCESS_KEY_ID`: Your AWS access key that has access to the bucket you - want to access -* `AWS_SECRET_ACCESS_KEY`: Your AWS secret corresponding to the access key +- `AWS_ACCESS_KEY_ID`: Your AWS access key that has access to the bucket you + want to access +- `AWS_SECRET_ACCESS_KEY`: Your AWS secret corresponding to the access key **Example YML Config when running outside of EC2:** ```yml --- Only: - envvarset: AWS_BUCKET_NAME + envvarset: AWS_BUCKET_NAME After: - - '#assetsflysystem' + - "#assetsflysystem" --- SilverStripe\Core\Injector\Injector: - Aws\S3\S3Client: - constructor: - configuration: - region: '`AWS_REGION`' - version: latest - credentials: - key: '`AWS_ACCESS_KEY_ID`' - secret: '`AWS_SECRET_ACCESS_KEY`' + Aws\S3\S3Client: + constructor: + configuration: + region: "`AWS_REGION`" + version: latest + credentials: + key: "`AWS_ACCESS_KEY_ID`" + secret: "`AWS_SECRET_ACCESS_KEY`" ``` -<<<<<<< HEAD -======= ## (Optional) CDN Implementation @@ -53,8 +51,8 @@ reachable within the `assets` directory of the cdn (for example; https://cdn.example.com/assets/Uploads/file.jpg) and set the following environment variable: -* `AWS_PUBLIC_CDN_PREFIX`: Your CloudFront distribution domain that has access - to the bucket you want to access +- `AWS_PUBLIC_CDN_PREFIX`: Your CloudFront distribution domain that has access + to the bucket you want to access For example, adding this to your `.env`: @@ -68,14 +66,34 @@ to something like: `https://cdn.example.com/assets/Uploads/file.jpg` ->>>>>>> rookie-me-feature/cdn_prefix +You can override the default `/assets/` path by redeclaring the PublicCDNAdapter constructor, with the paramater for the `cdnAssetPath` set to a string of your folder name: + +```yml +--- +Name: silverstripes3-cdn +Only: + envvarset: AWS_PUBLIC_CDN_PREFIX +After: + - "#assetsflysystem" + - "#silverstripes3-flysystem" +--- +SilverStripe\Core\Injector\Injector: + SilverStripe\S3\Adapter\PublicAdapter: + class: SilverStripe\S3\Adapter\PublicCDNAdapter + constructor: + s3Client: '%$Aws\S3\S3Client' + bucket: "`AWS_BUCKET_NAME`" + prefix: "`AWS_PUBLIC_BUCKET_PREFIX`" + cdnPrefix: "`AWS_PUBLIC_CDN_PREFIX`" + cdnAssetPath: "public" # example of altered path name, which will produce https://cdn.example.com/public/Uploads/file.jpg +``` ## Installation -* Define the environment variables listed above. -* [Install Composer from - https://getcomposer.org](https://getcomposer.org/download/) -* Run `composer require silverstripe/s3` +- Define the environment variables listed above. +- [Install Composer from + https://getcomposer.org](https://getcomposer.org/download/) +- Run `composer require silverstripe/s3` This will install the most recent applicable version of the module given your other Composer requirements. @@ -112,17 +130,17 @@ Make sure you replace `` below with the appropriate values. ```json { "Policy": { - "Version":"2012-10-17", - "Statement":[ - { - "Sid":"AddPerm", - "Effect":"Allow", - "Principal":"*", - "Action":"s3:GetObject", - "Resource":"arn:aws:s3:::/public/*" - } - ] - } + "Version": "2012-10-17", + "Statement": [ + { + "Sid": "AddPerm", + "Effect": "Allow", + "Principal": "*", + "Action": "s3:GetObject", + "Resource": "arn:aws:s3:::/public/*" + } + ] + } } ``` @@ -139,4 +157,4 @@ development. ## Uninstalling -* Run `composer remove silverstripe/s3` to remove the module. +- Run `composer remove silverstripe/s3` to remove the module. diff --git a/src/Adapter/PublicCDNAdapter.php b/src/Adapter/PublicCDNAdapter.php index eba12d1..758e31d 100644 --- a/src/Adapter/PublicCDNAdapter.php +++ b/src/Adapter/PublicCDNAdapter.php @@ -5,6 +5,7 @@ use Aws\S3\S3Client; use SilverStripe\Assets\Flysystem\PublicAdapter as SilverstripePublicAdapter; use SilverStripe\Control\Controller; + use const ASSETS_DIR; /** @@ -15,9 +16,12 @@ class PublicCDNAdapter extends PublicAdapter implements SilverstripePublicAdapte { protected $cdnPrefix; - public function __construct(S3Client $client, $bucket, $prefix = '', $cdnPrefix = '', array $options = []) + protected $cdnAssetPath; + + public function __construct(S3Client $client, $bucket, $prefix = '', $cdnPrefix = '', $cdnAssetPath = '', array $options = []) { $this->cdnPrefix = $cdnPrefix; + $this->cdnAssetPath = $cdnAssetPath ? $cdnAssetPath : ASSETS_DIR; parent::__construct($client, $bucket, $prefix, $options); } @@ -28,7 +32,7 @@ public function __construct(S3Client $client, $bucket, $prefix = '', $cdnPrefix */ public function getPublicUrl($path) { - return Controller::join_links($this->cdnPrefix, ASSETS_DIR, $path); + return Controller::join_links($this->cdnPrefix, $this->cdnAssetPath, $path); } From 422a140112d45515e666d704eba1890cc037a44e Mon Sep 17 00:00:00 2001 From: samandeggs Date: Tue, 1 Nov 2022 13:19:56 +1300 Subject: [PATCH 2/6] altering .editorconfig to use 2 spaces instead of 4 for ease of reading --- .editorconfig | 2 +- README.md | 90 +++++++++++++++++++++++++-------------------------- 2 files changed, 46 insertions(+), 46 deletions(-) diff --git a/.editorconfig b/.editorconfig index a3e7803..49b4934 100644 --- a/.editorconfig +++ b/.editorconfig @@ -5,7 +5,7 @@ [*] charset = utf-8 end_of_line = lf -indent_size = 4 +indent_size = 2 indent_style = space insert_final_newline = true trim_trailing_whitespace = true diff --git a/README.md b/README.md index 2142ef0..df46615 100644 --- a/README.md +++ b/README.md @@ -11,34 +11,34 @@ policy. The module requires a few environment variables to be set. Full details can be seen in the `SilverStripeS3AdapterTrait` trait. These are mandatory. -- `AWS_REGION`: The AWS region your S3 bucket is hosted in (e.g. `eu-central-1`) -- `AWS_BUCKET_NAME`: The name of the S3 bucket to store assets in. +- `AWS_REGION`: The AWS region your S3 bucket is hosted in (e.g. `eu-central-1`) +- `AWS_BUCKET_NAME`: The name of the S3 bucket to store assets in. If running outside of an EC2 instance it will be necessary to specify an API key and secret. -- `AWS_ACCESS_KEY_ID`: Your AWS access key that has access to the bucket you - want to access -- `AWS_SECRET_ACCESS_KEY`: Your AWS secret corresponding to the access key +- `AWS_ACCESS_KEY_ID`: Your AWS access key that has access to the bucket you + want to access +- `AWS_SECRET_ACCESS_KEY`: Your AWS secret corresponding to the access key **Example YML Config when running outside of EC2:** ```yml --- Only: - envvarset: AWS_BUCKET_NAME + envvarset: AWS_BUCKET_NAME After: - - "#assetsflysystem" + - "#assetsflysystem" --- SilverStripe\Core\Injector\Injector: - Aws\S3\S3Client: - constructor: - configuration: - region: "`AWS_REGION`" - version: latest - credentials: - key: "`AWS_ACCESS_KEY_ID`" - secret: "`AWS_SECRET_ACCESS_KEY`" + Aws\S3\S3Client: + constructor: + configuration: + region: "`AWS_REGION`" + version: latest + credentials: + key: "`AWS_ACCESS_KEY_ID`" + secret: "`AWS_SECRET_ACCESS_KEY`" ``` ## (Optional) CDN Implementation @@ -51,8 +51,8 @@ reachable within the `assets` directory of the cdn (for example; https://cdn.example.com/assets/Uploads/file.jpg) and set the following environment variable: -- `AWS_PUBLIC_CDN_PREFIX`: Your CloudFront distribution domain that has access - to the bucket you want to access +- `AWS_PUBLIC_CDN_PREFIX`: Your CloudFront distribution domain that has access + to the bucket you want to access For example, adding this to your `.env`: @@ -72,28 +72,28 @@ You can override the default `/assets/` path by redeclaring the PublicCDNAdapter --- Name: silverstripes3-cdn Only: - envvarset: AWS_PUBLIC_CDN_PREFIX + envvarset: AWS_PUBLIC_CDN_PREFIX After: - - "#assetsflysystem" - - "#silverstripes3-flysystem" + - "#assetsflysystem" + - "#silverstripes3-flysystem" --- SilverStripe\Core\Injector\Injector: - SilverStripe\S3\Adapter\PublicAdapter: - class: SilverStripe\S3\Adapter\PublicCDNAdapter - constructor: - s3Client: '%$Aws\S3\S3Client' - bucket: "`AWS_BUCKET_NAME`" - prefix: "`AWS_PUBLIC_BUCKET_PREFIX`" - cdnPrefix: "`AWS_PUBLIC_CDN_PREFIX`" - cdnAssetPath: "public" # example of altered path name, which will produce https://cdn.example.com/public/Uploads/file.jpg + SilverStripe\S3\Adapter\PublicAdapter: + class: SilverStripe\S3\Adapter\PublicCDNAdapter + constructor: + s3Client: '%$Aws\S3\S3Client' + bucket: "`AWS_BUCKET_NAME`" + prefix: "`AWS_PUBLIC_BUCKET_PREFIX`" + cdnPrefix: "`AWS_PUBLIC_CDN_PREFIX`" + cdnAssetPath: "public" # example of altered path name, which will produce https://cdn.example.com/public/Uploads/file.jpg ``` ## Installation -- Define the environment variables listed above. -- [Install Composer from - https://getcomposer.org](https://getcomposer.org/download/) -- Run `composer require silverstripe/s3` +- Define the environment variables listed above. +- [Install Composer from + https://getcomposer.org](https://getcomposer.org/download/) +- Run `composer require silverstripe/s3` This will install the most recent applicable version of the module given your other Composer requirements. @@ -129,18 +129,18 @@ Make sure you replace `` below with the appropriate values. ```json { - "Policy": { - "Version": "2012-10-17", - "Statement": [ - { - "Sid": "AddPerm", - "Effect": "Allow", - "Principal": "*", - "Action": "s3:GetObject", - "Resource": "arn:aws:s3:::/public/*" - } - ] - } + "Policy": { + "Version": "2012-10-17", + "Statement": [ + { + "Sid": "AddPerm", + "Effect": "Allow", + "Principal": "*", + "Action": "s3:GetObject", + "Resource": "arn:aws:s3:::/public/*" + } + ] + } } ``` @@ -157,4 +157,4 @@ development. ## Uninstalling -- Run `composer remove silverstripe/s3` to remove the module. +- Run `composer remove silverstripe/s3` to remove the module. From b83673dae9ad79d767938729ae3d14dd99cc98e0 Mon Sep 17 00:00:00 2001 From: Samuel Mulliss Date: Tue, 1 Nov 2022 16:14:33 +1300 Subject: [PATCH 3/6] Update .editorconfig --- .editorconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index 49b4934..b1d628a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -14,7 +14,7 @@ trim_trailing_whitespace = true trim_trailing_whitespace = false [*.{yml,js,json,css,scss,eslintrc,feature}] -indent_size = 2 +indent_size = 4 indent_style = space [composer.json] From 133a8d51adfa7562c55a536e3bb8d170eb5bd8c3 Mon Sep 17 00:00:00 2001 From: Samuel Mulliss Date: Tue, 1 Nov 2022 16:15:52 +1300 Subject: [PATCH 4/6] Update .editorconfig --- .editorconfig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.editorconfig b/.editorconfig index b1d628a..a3e7803 100644 --- a/.editorconfig +++ b/.editorconfig @@ -5,7 +5,7 @@ [*] charset = utf-8 end_of_line = lf -indent_size = 2 +indent_size = 4 indent_style = space insert_final_newline = true trim_trailing_whitespace = true @@ -14,7 +14,7 @@ trim_trailing_whitespace = true trim_trailing_whitespace = false [*.{yml,js,json,css,scss,eslintrc,feature}] -indent_size = 4 +indent_size = 2 indent_style = space [composer.json] From 266df4e1498e1796aad950fad97ddbb3c9b21d8c Mon Sep 17 00:00:00 2001 From: samandeggs Date: Tue, 1 Nov 2022 16:28:51 +1300 Subject: [PATCH 5/6] altering naming of variables and in README.md examples to match --- README.md | 4 ++-- src/Adapter/PublicCDNAdapter.php | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index df46615..7c5d201 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ to something like: `https://cdn.example.com/assets/Uploads/file.jpg` -You can override the default `/assets/` path by redeclaring the PublicCDNAdapter constructor, with the paramater for the `cdnAssetPath` set to a string of your folder name: +You can override the default `/assets/` path by redeclaring the PublicCDNAdapter constructor, with the paramater for the `cdnAssetsDir` set to a string of your folder name: ```yml --- @@ -85,7 +85,7 @@ SilverStripe\Core\Injector\Injector: bucket: "`AWS_BUCKET_NAME`" prefix: "`AWS_PUBLIC_BUCKET_PREFIX`" cdnPrefix: "`AWS_PUBLIC_CDN_PREFIX`" - cdnAssetPath: "public" # example of altered path name, which will produce https://cdn.example.com/public/Uploads/file.jpg + cdnAssetsDir: "cms-assets" # example of a custom assets folder name, which will produce https://cdn.example.com/cms-assets/Uploads/file.jpg ``` ## Installation diff --git a/src/Adapter/PublicCDNAdapter.php b/src/Adapter/PublicCDNAdapter.php index 758e31d..ecb6392 100644 --- a/src/Adapter/PublicCDNAdapter.php +++ b/src/Adapter/PublicCDNAdapter.php @@ -16,12 +16,12 @@ class PublicCDNAdapter extends PublicAdapter implements SilverstripePublicAdapte { protected $cdnPrefix; - protected $cdnAssetPath; + protected $cdnAssetsDir; - public function __construct(S3Client $client, $bucket, $prefix = '', $cdnPrefix = '', $cdnAssetPath = '', array $options = []) + public function __construct(S3Client $client, $bucket, $prefix = '', $cdnPrefix = '', $cdnAssetsDir = '', array $options = []) { $this->cdnPrefix = $cdnPrefix; - $this->cdnAssetPath = $cdnAssetPath ? $cdnAssetPath : ASSETS_DIR; + $this->cdnAssetsDir = $cdnAssetsDir ? $cdnAssetsDir : ASSETS_DIR; parent::__construct($client, $bucket, $prefix, $options); } @@ -32,7 +32,7 @@ public function __construct(S3Client $client, $bucket, $prefix = '', $cdnPrefix */ public function getPublicUrl($path) { - return Controller::join_links($this->cdnPrefix, $this->cdnAssetPath, $path); + return Controller::join_links($this->cdnPrefix, $this->cdnAssetsDir, $path); } From b7c0df71655a33be3ed128b29c3047b945a1a180 Mon Sep 17 00:00:00 2001 From: samandeggs Date: Tue, 1 Nov 2022 16:38:44 +1300 Subject: [PATCH 6/6] shifting $cdnAssetsDir param to last position in the constructor (for sake of versioning), and adding options to README.md example --- README.md | 1 + src/Adapter/PublicCDNAdapter.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7c5d201..bd7ff48 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,7 @@ SilverStripe\Core\Injector\Injector: bucket: "`AWS_BUCKET_NAME`" prefix: "`AWS_PUBLIC_BUCKET_PREFIX`" cdnPrefix: "`AWS_PUBLIC_CDN_PREFIX`" + options: [] cdnAssetsDir: "cms-assets" # example of a custom assets folder name, which will produce https://cdn.example.com/cms-assets/Uploads/file.jpg ``` diff --git a/src/Adapter/PublicCDNAdapter.php b/src/Adapter/PublicCDNAdapter.php index ecb6392..45ad8f3 100644 --- a/src/Adapter/PublicCDNAdapter.php +++ b/src/Adapter/PublicCDNAdapter.php @@ -18,7 +18,7 @@ class PublicCDNAdapter extends PublicAdapter implements SilverstripePublicAdapte protected $cdnAssetsDir; - public function __construct(S3Client $client, $bucket, $prefix = '', $cdnPrefix = '', $cdnAssetsDir = '', array $options = []) + public function __construct(S3Client $client, $bucket, $prefix = '', $cdnPrefix = '', array $options = [], $cdnAssetsDir = '') { $this->cdnPrefix = $cdnPrefix; $this->cdnAssetsDir = $cdnAssetsDir ? $cdnAssetsDir : ASSETS_DIR;