You are here

function amazons3_file_stream_wrapper_uri_normalize_alter in AmazonS3 7.2

Implements hook_file_stream_wrapper_uri_normalize_alter().

File

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

Code

function amazons3_file_stream_wrapper_uri_normalize_alter(&$uri, $scheme, $target) {
  if ($scheme == 's3') {
    try {

      // If this try passes, $uri is a fully-formed s3:// URI with a bucket.
      $url = S3Url::factory($uri);

      // Validate that the bucket exists. Sometimes we might be passed in URIs
      // without a bucket, like s3://image.jpg. If image.jpg is not a bucket, we
      // assume that image.jpg is supposed to be created in the default bucket.
      $bucket = $url
        ->getBucket();
      $s3 = S3Client::factory(array(), $bucket);
      S3Client::validateBucketExists($bucket, $s3, new \Drupal\amazons3\Cache());
    } catch (\InvalidArgumentException $e) {

      // Catch if S3Url::factory() can not parse $uri. That happens if we are
      // passed in a bare URI like s3://. Fall back to the default bucket.
      $uri = amazons3_uri_add_bucket($target);
    } catch (S3ConnectValidationException $e) {

      // Catch if a bucket does not exist or is invalid.
      $uri = amazons3_uri_add_bucket($target);
    }
  }
}