Extend the Illuminate\Filesystem\FilesystemManager
and overwrite s3 driver creation strategy.
use Aws\S3\S3Client;
use Aws\Sts\StsClient;
use Illuminate\Contracts\Filesystem\Cloud;
use Illuminate\Filesystem\FilesystemManager as BaseFilesystemManager;
use League\Flysystem\AwsS3v3\AwsS3Adapter as S3Adapter;
class FilesystemManager extends BaseFilesystemManager
{
/**
* Create an instance of the Amazon S3 driver.
*
* @param array $config
* @return Cloud
*/
public function createS3Driver(array $config): Cloud
{
$s3Config = $this->formatS3Config($config);
$stsClient = new StsClient($s3Config);
$credentials = $stsClient->assumeRole([
'RoleArn' => 'YOUR_AWS_ROLE_ARN',
'RoleSessionName' => 'YOUR_AWS_ROLE_SESSION_NAME',
]);
$s3Config['key'] = $credentials['Credentials']['AccessKeyId'];
$s3Config['secret'] = $credentials['Credentials']['SecretAccessKey'];
$s3Config['token'] = $credentials['Credentials']['SessionToken'];
$root = $s3Config['root'] ?? null;
$options = $config['options'] ?? [];
$streamReads = $config['stream_reads'] ?? false;
return $this->adapt($this->createFlysystem(
new S3Adapter(new S3Client($s3Config), $s3Config['bucket'], $root, $options, $streamReads),
$config,
));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Register Service Provider
/**
* Register any application services.
*
* @return void
*/
public function register(): void
{
$this->app->singleton('filesystem', static function ($app) {
return new FilesystemManager($app);
});
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
Refs:
- https://docs.aws.amazon.com/code-samples/latest/catalog/php-sts-AssumeRole.php.html
Comment