Skip to content

Commit

Permalink
env var setup complete
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Nov 22, 2024
1 parent fd34263 commit 08b67c3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ php flarum cache:clear
The S3 (or compatible) bucket can be configured either by environment variables or via the extension settings. If the environment variables are set, they will override the settings entered in the admin panel, if set.

#### Environment variables
- `AWS_ACCESS_KEY_ID` - your access key ID
- `AWS_SECRET_ACCESS_KEY` - your secret
- `AWS_DEFAULT_REGION` - the region
- `AWS_BUCKET` - the bucket name
- `AWS_URL` - the public facing base URL of the bucket
- `AWS_ENDPOINT` - the ARN
- `AWS_ACL` - The ACL, if any, that should be applied to the uploaded object (default: private). For possible values, see [AWS Docs](https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl)
- `FOF_S3_AWS_ACCESS_KEY_ID` - your access key ID *
- `FOF_S3_AWS_SECRET_ACCESS_KEY` - your secret *
- `FOF_S3_AWS_DEFAULT_REGION` - the region *
- `FOF_S3_AWS_BUCKET` - the bucket name *
- `FOF_S3_AWS_URL` - the public facing base URL of the bucket
- `FOF_S3_AWS_ENDPOINT` - the ARN
- `FOF_S3_AWS_ACL` - The ACL, if any, that should be applied to the uploaded object. For possible values, see [AWS Docs](https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl)
- `AWS_PATH_STYLE_ENDPOINT` - boolean value

`*` denotes the minimum requirements for using S3 on AWS. S3-compatible services will require more.

If you plan to setup the S3 configuration using the environment variables, please ensure these are set _before_ enabling the extension

#### Transferring assets from the existing filesystem to the S3 bucket
Expand Down
4 changes: 2 additions & 2 deletions js/src/admin/components/S3SettingsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default class S3SettingsPage extends ExtensionPage {
'awsS3Key',
this.buildSettingComponent({
setting: `${this.settingPrefix}awsS3Key`,
type: 'string',
type: 'password',
label: app.translator.trans('fof-s3-assets.admin.settings.s3key.label'),
help: app.translator.trans('fof-s3-assets.admin.settings.s3key.help'),
})
Expand All @@ -105,7 +105,7 @@ export default class S3SettingsPage extends ExtensionPage {
'awsS3Secret',
this.buildSettingComponent({
setting: `${this.settingPrefix}awsS3Secret`,
type: 'string',
type: 'password',
label: app.translator.trans('fof-s3-assets.admin.settings.s3secret.label'),
help: app.translator.trans('fof-s3-assets.admin.settings.s3secret.help'),
})
Expand Down
6 changes: 4 additions & 2 deletions src/Content/AdminPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Flarum\Foundation\Config;
use Flarum\Frontend\Document;
use Flarum\Settings\SettingsRepositoryInterface;
use FoF\S3Assets\Driver\Config as S3Config;
use FoF\S3Assets\Repository\S3Repository;
use Illuminate\Support\Arr;

Expand All @@ -22,13 +23,14 @@ class AdminPayload
public function __construct(
protected Config $config,
protected SettingsRepositoryInterface $settings,
protected S3Repository $s3
protected S3Repository $s3,
protected S3Config $s3Config
) {
}

public function __invoke(Document $document)
{
$document->payload['s3SetByEnv'] = Arr::get($this->config->offsetGet('filesystems'), 'disks.s3.set_by_environment');
$document->payload['s3SetByEnv'] = $this->s3Config->shouldUseEnv();
$document->payload['FoFS3Regions'] = $this->s3->getAwsRegions();
$document->payload['FoFS3ShareWithFoFUpload'] = $this->settings->get('fof-s3-assets.share_s3_config_with_fof_upload');
}
Expand Down
26 changes: 13 additions & 13 deletions src/Driver/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public function __construct(
) {
}

protected function shouldUseEnv(): bool
public function shouldUseEnv(): bool
{
return env('AWS_ACCESS_KEY_ID') && env('AWS_SECRET_ACCESS_KEY') && env('AWS_BUCKET');
return env('FOF_S3_AWS_ACCESS_KEY_ID') && env('FOF_S3_AWS_SECRET_ACCESS_KEY') && env('FOF_S3_AWS_BUCKET') && env('FOF_S3_AWS_REGION');
}

public function config(): array
Expand All @@ -44,7 +44,7 @@ public function config(): array
try {
$this->validator->assertValid($config);
} catch (IlluminateValidationException $e) {
$this->logger->error('[fof-s3-assets] Invalid S3 disk configuration', ['errors' => $e->errors()]);
$this->logger->error('[FOF_S3-assets] Invalid S3 disk configuration', ['errors' => $e->errors()]);

return [];
}
Expand Down Expand Up @@ -87,16 +87,16 @@ protected function buildConfigArray(string $key, string $secret, string $region,

protected function buildConfigFromEnv(): array
{
$bucket = env('AWS_BUCKET');
$region = env('AWS_DEFAULT_REGION');
$cdnUrl = env('AWS_URL', $this->createAwsUrlFromBucketAndRegion($bucket, $region));
$endpoint = env('AWS_ENDPOINT');
$pathStyle = (bool) env('AWS_PATH_STYLE_ENDPOINT', false);
$acl = env('AWS_ACL');
$bucket = env('FOF_S3_AWS_BUCKET');
$region = env('FOF_S3_AWS_REGION');
$cdnUrl = env('FOF_S3_AWS_URL', $this->createAwsUrlFromBucketAndRegion($bucket, $region));
$endpoint = env('FOF_S3_AWS_ENDPOINT');
$pathStyle = (bool) env('FOF_S3_AWS_PATH_STYLE_ENDPOINT', false);
$acl = env('FOF_S3_AWS_ACL');

return $this->buildConfigArray(
key: env('AWS_ACCESS_KEY_ID'),
secret: env('AWS_SECRET_ACCESS_KEY'),
key: env('FOF_S3_AWS_ACCESS_KEY_ID'),
secret: env('FOF_S3_AWS_SECRET_ACCESS_KEY'),
region: $region,
bucket: $bucket,
cdnUrl: $cdnUrl,
Expand Down Expand Up @@ -148,12 +148,12 @@ protected function createAwsUrlFromBucketAndRegion(?string $bucket, ?string $reg

protected function getSettingsPrefix(): string
{
$shareWithFoFUpload = (bool) $this->settings->get('fof-s3-assets.share_s3_config_with_fof_upload');
$shareWithFoFUpload = (bool) $this->settings->get('FOF_S3-assets.share_s3_config_with_fof_upload');

if ($shareWithFoFUpload) {
return 'fof-upload';
}

return 'fof-s3-assets';
return 'FOF_S3-assets';
}
}

0 comments on commit 08b67c3

Please sign in to comment.