Skip to content

Commit b8f1e0c

Browse files
committed
Improve S3 client creation and move it to Deployer::s3_client.
The improvements allow for the use of IAM roles rather than specifying access keys.
1 parent 11f2aae commit b8f1e0c

1 file changed

Lines changed: 33 additions & 29 deletions

File tree

src/Deployer.php

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,8 @@ public function upload_files( string $processed_site_path ) : void {
2424
return;
2525
}
2626

27-
$client_options = [
28-
'profile' => Controller::getValue( 's3Profile' ),
29-
'version' => 'latest',
30-
'region' => Controller::getValue( 's3Region' ),
31-
];
32-
33-
/*
34-
If no credentials option, SDK attempts to load credentials from
35-
your environment in the following order:
36-
37-
- environment variables.
38-
- a credentials .ini file.
39-
- an IAM role.
40-
*/
41-
if (
42-
Controller::getValue( 's3AccessKeyID' ) &&
43-
Controller::getValue( 's3SecretAccessKey' )
44-
) {
45-
$client_options['credentials'] = [
46-
'key' => Controller::getValue( 's3AccessKeyID' ),
47-
'secret' => \WP2Static\CoreOptions::encrypt_decrypt(
48-
'decrypt',
49-
Controller::getValue( 's3SecretAccessKey' )
50-
),
51-
];
52-
unset( $client_options['profile'] );
53-
}
54-
5527
// instantiate S3 client
56-
$s3 = new \Aws\S3\S3Client( $client_options );
28+
$s3 = self::s3_client();
5729

5830
// iterate each file in ProcessedSite
5931
$iterator = new RecursiveIteratorIterator(
@@ -114,6 +86,38 @@ public function upload_files( string $processed_site_path ) : void {
11486
}
11587
}
11688

89+
public function s3_client() : \Aws\S3\S3Client {
90+
$client_options = [
91+
'version' => 'latest',
92+
'region' => Controller::getValue( 's3Region' ),
93+
];
94+
95+
/*
96+
If no credentials option, SDK attempts to load credentials from
97+
your environment in the following order:
98+
99+
- environment variables.
100+
- a credentials .ini file.
101+
- an IAM role.
102+
*/
103+
if (
104+
Controller::getValue( 's3AccessKeyID' ) &&
105+
Controller::getValue( 's3SecretAccessKey' )
106+
) {
107+
$client_options['credentials'] = [
108+
'key' => Controller::getValue( 's3AccessKeyID' ),
109+
'secret' => \WP2Static\CoreOptions::encrypt_decrypt(
110+
'decrypt',
111+
Controller::getValue( 's3SecretAccessKey' )
112+
),
113+
];
114+
} else if ( Controller::getValue( 's3Profile' ) ) {
115+
$client_options['profile'] = Controller::getValue( 's3Profile' );
116+
}
117+
118+
return new \Aws\S3\S3Client( $client_options );
119+
}
120+
117121
public function cloudfront_client() : \Aws\CloudFront\CloudFrontClient {
118122
/*
119123
If no credentials option, SDK attempts to load credentials from

0 commit comments

Comments
 (0)