Skip to content

Commit 627d157

Browse files
Move S3Client to the container. (#72)
Moves `S3Client` to a bound service in the container and adds a new `AwsClientConfiguration` that can be overridden to make user-land customisation easier.
1 parent be21c4e commit 627d157

File tree

5 files changed

+66
-29
lines changed

5 files changed

+66
-29
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Hammerstone\Sidecar\Clients\Configurations;
4+
5+
use Hammerstone\Sidecar\Contracts\AwsClientConfiguration as AwsClientConfigurationContract;
6+
7+
class AwsClientConfiguration implements AwsClientConfigurationContract
8+
{
9+
public function getConfiguration()
10+
{
11+
$config = [
12+
'version' => 'latest',
13+
'region' => config('sidecar.aws_region'),
14+
];
15+
16+
$credentials = array_filter([
17+
'key' => config('sidecar.aws_key'),
18+
'secret' => config('sidecar.aws_secret'),
19+
]);
20+
21+
if ($credentials) {
22+
$config['credentials'] = $credentials;
23+
}
24+
25+
return $config;
26+
}
27+
}

src/Clients/S3Client.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Hammerstone\Sidecar\Clients;
4+
5+
use Aws\S3\S3Client as BaseClient;
6+
7+
class S3Client extends BaseClient
8+
{
9+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Hammerstone\Sidecar\Contracts;
4+
5+
interface AwsClientConfiguration
6+
{
7+
/**
8+
* Retrieve an array of configuration options for Lambda,
9+
* Cloudwatch and S3 AWS services.
10+
*
11+
* @return array
12+
*/
13+
public function getConfiguration();
14+
}

src/Package.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55

66
namespace Hammerstone\Sidecar;
77

8-
use Aws\S3\S3Client;
8+
use Hammerstone\Sidecar\Clients\S3Client;
99
use Hammerstone\Sidecar\Exceptions\SidecarException;
1010
use Illuminate\Support\Arr;
1111
use Illuminate\Support\Carbon;
1212
use Illuminate\Support\Collection;
1313
use Illuminate\Support\Str;
1414
use Illuminate\Support\Traits\Macroable;
15+
use ZipStream\Exception;
1516
use ZipStream\Option\Archive;
1617
use ZipStream\Option\File as FileOptions;
1718
use ZipStream\ZipStream;
@@ -281,7 +282,7 @@ public function deploymentConfiguration()
281282
{
282283
try {
283284
$key = $this->upload();
284-
} catch (\ZipStream\Exception $e) {
285+
} catch (Exception $e) {
285286
throw new SidecarException('Sidecar could not create ZIP: ' . $e->getMessage());
286287
}
287288

@@ -306,7 +307,7 @@ public function getFilename()
306307
/**
307308
* @return string
308309
*
309-
* @throws \ZipStream\Exception
310+
* @throws Exception
310311
*/
311312
public function upload()
312313
{
@@ -442,21 +443,12 @@ protected function bucket()
442443
}
443444

444445
/**
445-
* @return string
446+
* @return void
446447
*/
447448
protected function registerStreamWrapper()
448449
{
449450
// Register the s3:// stream wrapper.
450451
// https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/s3-stream-wrapper.html
451-
$client = new S3Client([
452-
'version' => 'latest',
453-
'region' => config('sidecar.aws_region'),
454-
'credentials' => [
455-
'key' => config('sidecar.aws_key'),
456-
'secret' => config('sidecar.aws_secret'),
457-
]
458-
]);
459-
460-
$client->registerStreamWrapper();
452+
app(S3Client::class)->registerStreamWrapper();
461453
}
462454
}

src/Providers/SidecarServiceProvider.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
namespace Hammerstone\Sidecar\Providers;
77

88
use Hammerstone\Sidecar\Clients\CloudWatchLogsClient;
9+
use Hammerstone\Sidecar\Clients\Configurations\AwsClientConfiguration;
910
use Hammerstone\Sidecar\Clients\LambdaClient;
11+
use Hammerstone\Sidecar\Clients\S3Client;
1012
use Hammerstone\Sidecar\Commands\Activate;
1113
use Hammerstone\Sidecar\Commands\Configure;
1214
use Hammerstone\Sidecar\Commands\Deploy;
1315
use Hammerstone\Sidecar\Commands\Install;
1416
use Hammerstone\Sidecar\Commands\Warm;
17+
use Hammerstone\Sidecar\Contracts\AwsClientConfiguration as AwsClientConfigurationContract;
1518
use Hammerstone\Sidecar\Manager;
1619
use Illuminate\Support\ServiceProvider;
1720

@@ -23,32 +26,24 @@ public function register()
2326

2427
$this->mergeConfigFrom(__DIR__ . '/../../config/sidecar.php', 'sidecar');
2528

29+
$this->app->bind(AwsClientConfigurationContract::class, AwsClientConfiguration::class);
30+
2631
$this->app->singleton(LambdaClient::class, function () {
2732
return new LambdaClient($this->getAwsClientConfiguration());
2833
});
2934

3035
$this->app->singleton(CloudWatchLogsClient::class, function () {
3136
return new CloudWatchLogsClient($this->getAwsClientConfiguration());
3237
});
38+
39+
$this->app->singleton(S3Client::class, function () {
40+
return new S3Client($this->getAwsClientConfiguration());
41+
});
3342
}
3443

3544
protected function getAwsClientConfiguration()
3645
{
37-
$config = [
38-
'version' => 'latest',
39-
'region' => config('sidecar.aws_region'),
40-
];
41-
42-
$credentials = array_filter([
43-
'key' => config('sidecar.aws_key'),
44-
'secret' => config('sidecar.aws_secret'),
45-
]);
46-
47-
if ($credentials) {
48-
$config['credentials'] = $credentials;
49-
}
50-
51-
return $config;
46+
return $this->app->make(AwsClientConfigurationContract::class)->getConfiguration();
5247
}
5348

5449
public function boot()

0 commit comments

Comments
 (0)