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

Adding param to PublicCDNAdapter to allow for custom asset path folder name. #56

Merged
merged 6 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
74 changes: 46 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ 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
- `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_SECRET_ACCESS_KEY`: Your AWS secret corresponding to the access key

**Example YML Config when running outside of EC2:**

Expand All @@ -28,20 +28,18 @@ and secret.
Only:
envvarset: AWS_BUCKET_NAME
After:
- '#assetsflysystem'
- "#assetsflysystem"
---
SilverStripe\Core\Injector\Injector:
Aws\S3\S3Client:
constructor:
configuration:
region: '`AWS_REGION`'
region: "`AWS_REGION`"
version: latest
credentials:
key: '`AWS_ACCESS_KEY_ID`'
secret: '`AWS_SECRET_ACCESS_KEY`'
key: "`AWS_ACCESS_KEY_ID`"
secret: "`AWS_SECRET_ACCESS_KEY`"
```
<<<<<<< HEAD
=======

## (Optional) CDN Implementation

Expand All @@ -53,7 +51,7 @@ 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
- `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`:
Expand All @@ -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
samandeggs marked this conversation as resolved.
Show resolved Hide resolved
```

## Installation

* Define the environment variables listed above.
* [Install Composer from
- Define the environment variables listed above.
- [Install Composer from
https://getcomposer.org](https://getcomposer.org/download/)
* Run `composer require silverstripe/s3`
- Run `composer require silverstripe/s3`

This will install the most recent applicable version of the module given your
other Composer requirements.
Expand Down Expand Up @@ -111,18 +129,18 @@ Make sure you replace `<bucket-name>` below with the appropriate values.

```json
{
"Policy": {
"Version":"2012-10-17",
"Statement":[
{
"Sid":"AddPerm",
"Effect":"Allow",
"Principal":"*",
"Action":"s3:GetObject",
"Resource":"arn:aws:s3:::<bucket-name>/public/*"
}
]
}
"Policy": {
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AddPerm",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::<bucket-name>/public/*"
}
]
}
}
```

Expand All @@ -139,4 +157,4 @@ development.

## Uninstalling

* Run `composer remove silverstripe/s3` to remove the module.
- Run `composer remove silverstripe/s3` to remove the module.
8 changes: 6 additions & 2 deletions src/Adapter/PublicCDNAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Aws\S3\S3Client;
use SilverStripe\Assets\Flysystem\PublicAdapter as SilverstripePublicAdapter;
use SilverStripe\Control\Controller;

use const ASSETS_DIR;

/**
Expand All @@ -15,9 +16,12 @@ class PublicCDNAdapter extends PublicAdapter implements SilverstripePublicAdapte
{
protected $cdnPrefix;

public function __construct(S3Client $client, $bucket, $prefix = '', $cdnPrefix = '', array $options = [])
protected $cdnAssetPath;
samandeggs marked this conversation as resolved.
Show resolved Hide resolved

public function __construct(S3Client $client, $bucket, $prefix = '', $cdnPrefix = '', $cdnAssetPath = '', array $options = [])
samandeggs marked this conversation as resolved.
Show resolved Hide resolved
{
$this->cdnPrefix = $cdnPrefix;
$this->cdnAssetPath = $cdnAssetPath ? $cdnAssetPath : ASSETS_DIR;
parent::__construct($client, $bucket, $prefix, $options);
}

Expand All @@ -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);
}


Expand Down