You are here

function _s3fs_validate_config in S3 File System 7

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

Checks all the configuration options to ensure that they're valid.

Return value

bool TRUE if config is good to go, otherwise FALSE.

2 calls to _s3fs_validate_config()
s3fs_settings_validate in ./s3fs.admin.inc
Validates the values on the admin form.
_s3fs_refresh_cache in ./s3fs.module
Refreshes the metadata cache.

File

./s3fs.module, line 216
Sets up the S3fsStreamWrapper class to be used as a Drupal file system.

Code

function _s3fs_validate_config($config) {
  if (!empty($config['use_customhost']) && empty($config['hostname'])) {
    form_set_error('s3fs_hostname', 'You must specify a Hostname to use the Custom Host feature.');
    return FALSE;
  }
  if (!empty($config['use_cname']) && empty($config['domain'])) {
    form_set_error('s3fs_domain', 'You must specify a CDN Domain Name to use the CNAME feature.');
    return FALSE;
  }
  try {
    $s3 = _s3fs_get_amazons3_client($config);
  } catch (S3fsException $e) {
    form_set_error('s3fs_bucket', $e
      ->getMessage());
    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(array(
      'Bucket' => $config['bucket'],
    ));
  } catch (Aws\S3\Exception\InvalidAccessKeyIdException $e) {
    form_set_error('', t('The Access Key in your AWS credentials is invalid.'));
    return FALSE;
  } catch (Aws\S3\Exception\SignatureDoesNotMatchException $e) {
    form_set_error('', t('The Secret Key in your AWS credentials is invalid.'));
    return FALSE;
  } catch (Aws\S3\Exception\NoSuchBucketException $e) {
    form_set_error('s3fs_bucket', t('The specified bucket does not exist.'));
    return FALSE;
  } catch (Aws\S3\Exception\PermanentRedirectException $e) {
    form_set_error('s3fs_region', t('This bucket exists, but it is not in the specified region.'));
    return FALSE;
  } catch (Exception $e) {
    form_set_error('s3fs_bucket', t('An unexpected %exception occured, with the following error message:<br>%error', array(
      '%exception' => get_class($e),
      '%error' => $e
        ->getMessage(),
    )));
    return FALSE;
  }
  return TRUE;
}