You are here

public function ValidateService::validate in S3 File System 8.2

Validate the S3fs config.

Parameters

$config: Array of configuration settings from which to configure the client.

$returnError: Boolean, False by default.

Return value

Boolean/array

File

src/ValidateService.php, line 58

Class

ValidateService
Defines a ValidateService service.

Namespace

Drupal\s3fs

Code

public function validate(array $config, $returnError = FALSE) {
  if (!empty($config['use_customhost']) && empty($config['hostname'])) {
    if ($returnError) {
      $name = 'hostname';
      $msg = $this
        ->t('You must specify a Hostname to use the Custom Host feature.');
      return [
        $name,
        $msg,
      ];
    }
    return FALSE;
  }
  if (!empty($config['use_cname']) && empty($config['domain'])) {
    if ($returnError) {
      $name = 'domain';
      $msg = $this
        ->t('You must specify a CDN Domain Name to use the CNAME feature.');
      return [
        $name,
        $msg,
      ];
    }
    return FALSE;
  }
  try {
    $s3 = $this
      ->getAmazonS3Client($config);
  } catch (S3Exception $e) {
    if ($returnError) {
      $name = 'form';
      $msg = $e
        ->getMessage();
      return [
        $name,
        $msg,
      ];
    }
    return FALSE;
  }

  // Test the connection to S3, and the bucket name.
  try {

    // listObjects() will trigger descriptive exceptions if the credentials,
    // bucket name, or region are invalid/mismatched.
    $s3
      ->listObjects([
      'Bucket' => $config['bucket'],
      'MaxKeys' => 1,
    ]);
  } catch (S3Exception $e) {
    if ($returnError) {
      $name = 'form';
      $msg = $this
        ->t('An unexpected error occurred. @message', [
        '@message' => $e
          ->getMessage(),
      ]);
      return [
        $name,
        $msg,
      ];
    }
    return FALSE;
  }
  return TRUE;
}