You are here

function _s3fs_get_amazons3_client in S3 File System 7

Same name and namespace in other branches
  1. 7.3 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.

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 547
Sets up the S3fsStreamWrapper class to be used as a Drupal 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 s3fs_awssdk2_* settings, pull them normally, then override them
    // if their equivalent awssdk2_* setting is set in settings.php.
    $access_key = !empty($config['awssdk2_access_key']) ? $config['awssdk2_access_key'] : FALSE;
    $access_key = variable_get('awssdk2_access_key', $access_key);
    $secret_key = !empty($config['awssdk2_secret_key']) ? $config['awssdk2_secret_key'] : FALSE;
    $secret_key = variable_get('awssdk2_secret_key', $secret_key);
    $default_cache_config = !empty($config['awssdk2_default_cache_config']) ? $config['awssdk2_default_cache_config'] : FALSE;
    $default_cache_config = variable_get('awssdk2_default_cache_config', $default_cache_config);
    $use_instance_profile = !empty($config['use_instance_profile']);
    $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)) {
      throw new S3fsException(t("Your AWS credentials have not been properly configured. Please set them on the S3 File System !settings_page or\n        set \$conf['awssdk2_access_key'] and \$conf['awssdk2_secret_key'] in your site's settings.php file.", array(
        '!settings_page' => l(t('settings page'), 'admin/config/media/s3fs/settings'),
      )));
    }
    elseif ($use_instance_profile && empty($default_cache_config)) {
      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 default cache location.\n        Please set it on the !settings_page or set \$conf['awssdk2_default_cache_config'] in your site's settings.php file.", array(
        '!settings_page' => l(t('settings page'), 'admin/config/media/s3fs/settings'),
      )));
    }

    // Create the Aws\S3\S3Client object.
    if ($use_instance_profile) {
      $client_config = array(
        'default_cache_config' => $default_cache_config,
      );
    }
    else {
      $client_config = array(
        'key' => $access_key,
        'secret' => $secret_key,
      );
    }
    if (!empty($config['region'])) {
      $client_config['region'] = $config['region'];
    }
    if (!empty($config['use_customhost']) && !empty($config['hostname'])) {
      $client_config['base_url'] = $config['hostname'];
    }
    $s3 = Aws\S3\S3Client::factory($client_config);
  }
  $static_config = $config;
  return $s3;
}