You are here

function _s3fs_get_amazons3_client in S3 File System 7.3

Same name and namespace in other branches
  1. 7 s3fs.module \_s3fs_get_amazons3_client()
  2. 7.2 s3fs.module \_s3fs_get_amazons3_client()

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.

4 calls to _s3fs_get_amazons3_client()
s3fs.test in tests/s3fs.test
S3fsStreamWrapper::__construct in ./S3fsStreamWrapper.inc
Stream wrapper constructor.
_s3fs_refresh_cache in ./s3fs.module
Refreshes the metadata cache.
_s3fs_validate_config in ./s3fs.module
Checks all the configuration options to ensure that they're valid.

File

./s3fs.module, line 699
Hook implementations and other primary functionality for S3 File System.

Code

function _s3fs_get_amazons3_client($config) {
  static $s3;
  static $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) {

    // For the SDK credentials, get the saved settings from _s3fs_get_setting(). But since $config might be populated
    // with to-be-validated settings, its contents (if set) override the saved settings.
    $access_key = _s3fs_get_setting('awssdk_access_key');
    if (isset($config['awssdk_access_key'])) {
      $access_key = $config['awssdk_access_key'];
    }
    $secret_key = _s3fs_get_setting('awssdk_secret_key');
    if (isset($config['awssdk_secret_key'])) {
      $secret_key = $config['awssdk_secret_key'];
    }
    $use_instance_profile = _s3fs_get_setting('use_instance_profile');
    if (isset($config['use_instance_profile'])) {
      $use_instance_profile = $config['use_instance_profile'];
    }
    $credentials_file = _s3fs_get_setting('credentials_file');
    if (isset($config['credentials_file'])) {
      $credentials_file = $config['credentials_file'];
    }

    // Check if AWS SDK is loaded and accessible.
    $sdk_loaded = FALSE;
    if (module_exists('composer_manager')) {
      $sdk_loaded = class_exists('Aws\\Sdk');
    }
    elseif (module_exists('libraries')) {
      $library = _s3fs_load_awssdk_library();
      $sdk_loaded = $library['loaded'];
    }
    if (!$sdk_loaded) {
      throw new S3fsException(t('Unable to load the AWS SDK. Please ensure that the AWS SDK library is installed correctly.'));
    }
    elseif (!class_exists('Aws\\S3\\S3Client')) {
      throw new S3fsException(t('Cannot load Aws\\S3\\S3Client class. Please ensure that the AWS SDK library is installed correctly.'));
    }

    // Create the Aws\S3\S3Client object.
    $client_config = array();

    // If we have configured credentials locally use them, otherwise let
    // the SDK find them per API docs.
    // @see https://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/configuration.html
    if ($use_instance_profile) {

      // If defined path use that otherwise SDK will check home directory.
      if ($credentials_file) {
        $provider = CredentialProvider::ini(NULL, $credentials_file);
      }
      else {

        // Assume an instance profile provider if no path.
        $provider = CredentialProvider::instanceProfile();
      }

      // Cache the results in a memoize function to avoid loading and parsing
      // the ini file on every API operation.
      // @see https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials_provider.html
      $provider = CredentialProvider::memoize($provider);
      $client_config['credentials'] = $provider;
    }
    elseif (!empty($access_key) && !empty($secret_key)) {
      $client_config['credentials'] = array(
        'key' => $access_key,
        'secret' => $secret_key,
      );
    }
    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'])) {
      $client_config['base_url'] = $config['hostname'];
    }

    // Create the Aws\S3\S3Client object with the specified configuration.
    // S3 Service only supports 2006-03-01 API version currently. V3 requires
    // an explicit version declaration, and use of 'latest' is discouraged.
    $client_config['version'] = '2006-03-01';
    $s3 = new S3Client($client_config);
  }
  $static_config = $config;
  return $s3;
}