Skip to content

Commit

Permalink
feat: containerise s3 client generation, support other client credent…
Browse files Browse the repository at this point in the history
…ials (#25)

* feat: containerise s3 client generation, support other client credentials

fixes #23

* fix: shift dependency registration from boot to register
  • Loading branch information
tractorcow authored Apr 3, 2022
1 parent f89dd6a commit 99db95f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
18 changes: 4 additions & 14 deletions src/Http/Controllers/UploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Ahmedkandel\NovaS3MultipartUpload\Http\Controllers;

use Ahmedkandel\NovaS3MultipartUpload\NovaS3MultipartUpload;
use Aws\S3\S3Client;
use Illuminate\Support\Str;
use Laravel\Nova\Http\Requests\NovaRequest;

Expand All @@ -19,7 +18,7 @@ class UploadController
/**
* S3 client instance.
*
* @var \Aws\S3\S3Client
* @var \Aws\S3\S3ClientInterface
*/
private $s3Client;

Expand All @@ -36,10 +35,10 @@ public function preflightHeader()
/**
* Retrieve resource tool and Authorize then create S3Client..
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return void
*/
private function init($request)
protected function init($request)
{
$resource = $request->findResourceOrFail();

Expand All @@ -53,16 +52,7 @@ private function init($request)

abort_unless($this->tool->canUpload, 403);

$this->s3Client = new S3Client([
'credentials' => [
'key' => config("filesystems.disks.{$this->tool->disk}.key"),
'secret' => config("filesystems.disks.{$this->tool->disk}.secret"),
],
'endpoint' => config("filesystems.disks.{$this->tool->disk}.endpoint"),
'use_path_style_endpoint' => config("filesystems.disks.{$this->tool->disk}.use_path_style_endpoint"),
'region' => config("filesystems.disks.{$this->tool->disk}.region"),
'version' => 'latest',
]);
$this->s3Client = app()->makeWith('novas3client', ['disk' => $this->tool->disk]);
}

/**
Expand Down
32 changes: 32 additions & 0 deletions src/ToolServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@

namespace Ahmedkandel\NovaS3MultipartUpload;

use Aws\S3\S3Client;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Laravel\Nova\Nova;

class ToolServiceProvider extends ServiceProvider
{
public function register()
{
parent::register();

// Construct s3 client for tool
$this->app->bind('novas3client', function ($app, $args) {
$disk = $args['disk'];
$config = $this->formatS3Config(config("filesystems.disks.{$disk}"));
return new S3Client($config);
});
}


/**
* Bootstrap any application services.
*
Expand All @@ -25,6 +40,23 @@ public function boot()
});
}

/**
* Format the given S3 configuration with the default options.
*
* @param array $config
* @return array
*/
protected function formatS3Config(array $config)
{
$config += ['version' => 'latest'];

if (!empty($config['key']) && !empty($config['secret'])) {
$config['credentials'] = Arr::only($config, ['key', 'secret', 'token']);
}

return $config;
}

/**
* Register the tool's routes.
*
Expand Down

0 comments on commit 99db95f

Please sign in to comment.