You are here

function _s3fs_get_amazons3_client in S3 File System 7.2

Same name and namespace in other branches
  1. 7.3 s3fs.module \_s3fs_get_amazons3_client()
  2. 7 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

$config Array: 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 724
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('awssdk2_access_key');
    if (isset($config['awssdk2_access_key'])) {
      $access_key = $config['awssdk2_access_key'];
    }
    $secret_key = _s3fs_get_setting('awssdk2_secret_key');
    if (isset($config['awssdk2_secret_key'])) {
      $secret_key = $config['awssdk2_secret_key'];
    }
    $use_instance_profile = _s3fs_get_setting('awssdk2_use_instance_profile');
    if (isset($config['awssdk2_use_instance_profile'])) {
      $use_instance_profile = $config['awssdk2_use_instance_profile'];
    }
    $default_cache_config = _s3fs_get_setting('awssdk2_default_cache_config');
    if (isset($config['awssdk2_default_cache_config'])) {
      $default_cache_config = $config['awssdk2_default_cache_config'];
    }
    $library = _s3fs_load_awssdk2_library();
    if (!$library['loaded']) {
      throw new S3fsException(t('Unable to load the AWS SDK. Please ensure that the awssdk2 library is installed correctly.'));
    }
    elseif (!class_exists('Aws\\S3\\S3Client')) {
      throw new S3fsException(t('Cannot load Aws\\S3\\S3Client class. Please ensure that the awssdk2 library is installed correctly.'));
    }
    elseif (!$use_instance_profile && (!$secret_key || !$access_key)) {

      # s3fs isn't configured to use instance profile credentials, but also doesn't have the secret/access keys defined.
      throw new S3fsException(t("Your AWS credentials have not been properly configured.\n        Please set them on the S3 File System !settings_page or\n        set \$conf['awssdk2_access_key'] and \$conf['awssdk2_secret_key'] in settings.php.", array(
        '!settings_page' => l(t('settings page'), 'admin/config/media/s3fs/settings'),
      )));
    }
    elseif ($use_instance_profile && (empty($default_cache_config) || !(is_writable($default_cache_config) && is_readable($default_cache_config)))) {

      # s3fs is configured to use instance profile credentials, but the default_cache_config folder is either unset or

      # isn't read/writable by the web server.
      throw new s3fsException(t("Your AWS credentials have not been properly configured.\n        You are attempting to use instance profile credentials but you have not set a valid default cache location.\n        Please set it on the !settings_page or set \$conf['awssdk2_default_cache_config'] in settings.php.\n        Note that this cache location must be an existing folder that can be read and written by your webserver.\n        It should also be writable by whichever user you run drush commands under.", array(
        '!settings_page' => l(t('settings page'), 'admin/config/media/s3fs/settings'),
      )));
    }
    if ($use_instance_profile) {

      // Create a cache adapter that stores cached credentials on the filesystem
      // in the default cache config folder.
      $cache_adapter = new DoctrineCacheAdapter(new FilesystemCache($default_cache_config));
      $client_config = array(
        'credentials.cache' => $cache_adapter,
      );
    }
    else {
      $client_config = 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 = Aws\S3\S3Client::factory($client_config);
  }
  $static_config = $config;
  return $s3;
}