Merge pull request #1070 from fenak/feat/allow-aws-sdk-to-use-default-credential-provider-chain

Feature: Allow AWS SDK to use default credential provider chain for S3Storage
This commit is contained in:
El RIDO 2023-02-28 04:48:55 +00:00 committed by GitHub
commit b23e781b5b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 24 deletions

View file

@ -230,6 +230,19 @@ dir = PATH "data"
;accesskey = "access key id" ;accesskey = "access key id"
;secretkey = "secret access key" ;secretkey = "secret access key"
;[model]
; example of S3 configuration for AWS using its SDK default credential provider chain
; if relying on environment variables, the AWS SDK will look for the following:
; - AWS_ACCESS_KEY_ID
; - AWS_SECRET_ACCESS_KEY
; - AWS_SESSION_TOKEN (if needed)
; for more details, see https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html#default-credential-chain
;class = S3Storage
;[model_options]
;region = "eu-central-1"
;version = "latest"
;bucket = "my-bucket"
[yourls] [yourls]
; When using YOURLS as a "urlshortener" config item: ; When using YOURLS as a "urlshortener" config item:
; - By default, "urlshortener" will point to the YOURLS API URL, with or without ; - By default, "urlshortener" will point to the YOURLS API URL, with or without

View file

@ -82,31 +82,33 @@ class S3Storage extends AbstractData
*/ */
public function __construct(array $options) public function __construct(array $options)
{ {
$this->_options['credentials'] = array(); if (is_array($options)) {
// AWS SDK will try to load credentials from environment if credentials are not passed via configuration
// ref: https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html#default-credential-chain
if (isset($options['accesskey']) && isset($options['secretkey'])) {
$this->_options['credentials'] = array();
if (is_array($options) && array_key_exists('region', $options)) { $this->_options['credentials']['key'] = $options['accesskey'];
$this->_options['region'] = $options['region']; $this->_options['credentials']['secret'] = $options['secretkey'];
} }
if (is_array($options) && array_key_exists('version', $options)) { if (array_key_exists('region', $options)) {
$this->_options['version'] = $options['version']; $this->_options['region'] = $options['region'];
} }
if (is_array($options) && array_key_exists('endpoint', $options)) { if (array_key_exists('version', $options)) {
$this->_options['endpoint'] = $options['endpoint']; $this->_options['version'] = $options['version'];
} }
if (is_array($options) && array_key_exists('accesskey', $options)) { if (array_key_exists('endpoint', $options)) {
$this->_options['credentials']['key'] = $options['accesskey']; $this->_options['endpoint'] = $options['endpoint'];
} }
if (is_array($options) && array_key_exists('secretkey', $options)) { if (array_key_exists('use_path_style_endpoint', $options)) {
$this->_options['credentials']['secret'] = $options['secretkey']; $this->_options['use_path_style_endpoint'] = filter_var($options['use_path_style_endpoint'], FILTER_VALIDATE_BOOLEAN);
} }
if (is_array($options) && array_key_exists('use_path_style_endpoint', $options)) { if (array_key_exists('bucket', $options)) {
$this->_options['use_path_style_endpoint'] = filter_var($options['use_path_style_endpoint'], FILTER_VALIDATE_BOOLEAN); $this->_bucket = $options['bucket'];
} }
if (is_array($options) && array_key_exists('bucket', $options)) { if (array_key_exists('prefix', $options)) {
$this->_bucket = $options['bucket']; $this->_prefix = $options['prefix'];
} }
if (is_array($options) && array_key_exists('prefix', $options)) {
$this->_prefix = $options['prefix'];
} }
$this->_client = new S3Client($this->_options); $this->_client = new S3Client($this->_options);