You are here

function amazons3_form_bucket_validate in AmazonS3 7.2

Element validate callback to validate a bucket name.

Parameters

array &$element: The element to validate.

array &$form_state: The current state of the form.

array $form: The current form.

2 string references to 'amazons3_form_bucket_validate'
amazons3_admin in ./amazons3.admin.inc
Form API callback for the configuration form.
_amazons3_field_configuration in ./amazons3.module
Add S3 configuration to file field settings forms.

File

./amazons3.module, line 628
Hook implementations for the AmazonS3 module.

Code

function amazons3_form_bucket_validate(array &$element, array &$form_state, array $form) {
  $bucket = $element['#value'];
  if (empty($bucket)) {
    return;
  }
  if (!isset($form_state['values']['amazons3_hostname'])) {
    $hostname = variable_get('amazons3_hostname');
  }
  else {
    $hostname = $form_state['values']['amazons3_hostname'];
  }

  // Inject our credentials for testing.
  $config = array();
  if (isset($form_state['values']['amazons3_key']) && isset($form_state['values']['amazons3_secret'])) {
    $config['credentials'] = new Credentials($form_state['values']['amazons3_key'], $form_state['values']['amazons3_secret']);
  }
  if (isset($form_state['values']['amazons3_region'])) {
    $config['region'] = $form_state['values']['amazons3_region'];
  }
  elseif (isset($form_state['values']['field']['settings']['amazons3_region']) && $form_state['values']['field']['settings']['amazons3_region']) {
    $config['region'] = $form_state['values']['field']['settings']['amazons3_region'];
  }
  if (!empty($hostname)) {
    $config['endpoint'] = $hostname;
  }
  try {
    $s3 = S3Client::factory($config, $bucket);
    S3Client::validateBucketExists($bucket, $s3);
  } catch (S3ConnectValidationException $e) {
    form_error($element, t('The S3 access credentials are invalid or the bucket does not exist.'));
    watchdog_exception('amazons3', $e);
  } catch (Exception $e) {
    form_error($element, t('There was a problem connecting to S3. The following exception was thrown: @exception', array(
      '@exception' => $e
        ->getMessage(),
    )));
    watchdog_exception('amazons3', $e);
  }
}