You are here

public function ValidateService::getAmazonS3Client in S3 File System 8.2

Sets up the S3Client object. For performance reasons, only one S3Client object will ever be created within a single request.

Parameters

$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

2 calls to ValidateService::getAmazonS3Client()
ValidateService::copyFileSystemToS3 in src/ValidateService.php
Copies all the local files from the specified file system into S3.
ValidateService::validate in src/ValidateService.php
Validate the S3fs config.

File

src/ValidateService.php, line 119

Class

ValidateService
Defines a ValidateService service.

Namespace

Drupal\s3fs

Code

public function getAmazonS3Client($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) {
    if (!class_exists('Aws\\S3\\S3Client')) {
      throw new S3fsException($this
        ->t('Cannot load Aws\\S3\\S3Client class. Please ensure that the aws sdk php library is installed correctly.'));
    }
    elseif (!$config['use_instance_profile'] && (!$config['secret_key'] || !$config['access_key'])) {
      throw new S3fsException($this
        ->t("Your AWS credentials have not been properly configured.\n          Please set them on the S3 File System admin/config/media/s3fs page or\n          set \$config['s3fs.settings']['access_key'] and \$config['s3fs.settings']['secret_key'] in settings.php."));
    }
    elseif ($config['use_instance_profile'] && empty($config['default_cache_config'])) {
      throw new S3fsException($this
        ->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 admin/config/media/s3fs page or set \$config['s3fs.settings']['cache_config'] in settings.php."));
    }

    // Create the Aws\S3\S3Client object.
    if ($config['use_instance_profile']) {
      $client_config = [
        'default_cache_config' => $config['default_cache_config'],
      ];
    }
    else {
      $client_config['credentials'] = [
        'key' => $config['access_key'],
        'secret' => $config['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'];
    }
    $client_config['version'] = 'latest';
    $s3 = S3Client::factory($client_config);
    $static_config = $config;
  }
  return $s3;
}