public function S3fsService::getAmazonS3Client in S3 File System 4.0.x
Same name and namespace in other branches
- 8.3 src/S3fsService.php \Drupal\s3fs\S3fsService::getAmazonS3Client()
Sets up the S3Client object. For performance reasons, only one S3Client object will ever be created within a single request.
Parameters
array $config: Array of configuration settings from which to configure the client.
Return value
\Aws\S3\S3Client The fully-configured S3Client object.
Throws
\Drupal\s3fs\S3fsException The S3fs Exception.
Overrides S3fsServiceInterface::getAmazonS3Client
2 calls to S3fsService::getAmazonS3Client()
- S3fsService::refreshCache in src/
S3fsService.php - Refreshes the metadata cache.
- S3fsService::validate in src/
S3fsService.php - Validate the S3fs config.
File
- src/
S3fsService.php, line 254
Class
- S3fsService
- Defines a S3fsService service.
Namespace
Drupal\s3fsCode
public function getAmazonS3Client(array $config) {
$s3 =& drupal_static(__METHOD__ . '_S3Client');
$static_config =& drupal_static(__METHOD__ . '_static_config');
// If the client hasn't been set up yet, or the config given to this call is
// different from the previous call, (re)build the client.
if (!isset($s3) || $static_config != $config) {
$client_config = [];
$access_key = Settings::get('s3fs.access_key', '');
$secret_key = Settings::get('s3fs.secret_key', '');
$noKeyInSettings = empty($access_key) || empty($secret_key);
if ($noKeyInSettings && $this->moduleHandler
->moduleExists('key')) {
if (!$access_key && !empty($config['keymodule']['access_key_name'])) {
$key = \Drupal::service('key.repository')
->getKey($config['keymodule']['access_key_name']);
$key_value = $key
->getKeyValue();
if (!empty($key_value)) {
$access_key = $key_value;
}
}
if (!$secret_key && !empty($config['keymodule']['secret_key_name'])) {
$key = \Drupal::service('key.repository')
->getKey($config['keymodule']['secret_key_name']);
$key_value = $key
->getKeyValue();
if (!empty($key_value)) {
$secret_key = $key_value;
}
}
}
if (!empty($access_key) && !empty($secret_key)) {
$client_config['credentials'] = [
'key' => $access_key,
'secret' => $secret_key,
];
}
else {
// Use the defaultProvider to get all paths in home, env, etc.
$provider = CredentialProvider::defaultProvider();
// Use a custom ini file before defaultProvider.
if (!empty($config['credentials_file'])) {
$iniProvider = CredentialProvider::ini(NULL, $config['credentials_file']);
$provider = CredentialProvider::chain($iniProvider, $provider);
}
// Cache the results in a memoize function to avoid loading and parsing
// the ini file on every API operation.
$provider = CredentialProvider::memoize($provider);
// Allow SDK to cache results of calls to metadata servers.
$doctrineInstalled = class_exists('\\Doctrine\\Common\\Cache\\FilesystemCache');
if (!empty($config['use_credentials_cache']) && !empty($config['credentials_cache_dir']) && $doctrineInstalled) {
$cache = new DoctrineCacheAdapter(new FilesystemCache($config['credentials_cache_dir'] . '/s3fscache', '.doctrine.cache', 017));
$provider = CredentialProvider::cache($provider, $cache);
}
$client_config['credentials'] = $provider;
}
if (!empty($config['region'])) {
$client_config['region'] = $config['region'];
// Signature v4 is only required in the Beijing and Frankfurt regions.
// Also, setting it will throw an exception if a region hasn't been set.
$client_config['signature'] = 'v4';
}
if (!empty($config['use_customhost']) && !empty($config['hostname'])) {
if (preg_match('#http(s)?://#i', $config['hostname']) === 1) {
$client_config['endpoint'] = $config['hostname'];
}
else {
// Fallback for before we required hostname to include protocol.
$client_config['endpoint'] = ($config['use_https'] ? 'https://' : 'http://') . $config['hostname'];
}
}
// Use path-style endpoint, if selected.
if (!empty($config['use_path_style_endpoint'])) {
$client_config['use_path_style_endpoint'] = TRUE;
}
$client_config['version'] = S3fsStream::API_VERSION;
// Disable SSL/TLS verification.
if (!empty($config['disable_cert_verify'])) {
$client_config['http']['verify'] = FALSE;
}
// Set use_aws_shared_config_files = false to avoid reading ~/.aws/config.
// If open_basedir restriction is in effect an exception may be thrown
// without this enabled.
if (!empty($config['disable_shared_config_files'])) {
$client_config['use_aws_shared_config_files'] = FALSE;
}
// Create the Aws\S3\S3Client object.
$s3 = new S3Client($client_config);
$static_config = $config;
}
return $s3;
}