You are here

protected function AmazonS3StreamWrapper::_amazons3_get_object in AmazonS3 7

Try to fetch an object from the metadata cache.

Otherwise fetch it's info from S3 and populate the cache.

Parameters

string $uri: A string containing the uri of the resource to check.

bool $cache: A boolean representing whether to check the cache for file information.

Return value

array An array if the $uri exists, otherwise FALSE.

3 calls to AmazonS3StreamWrapper::_amazons3_get_object()
AmazonS3StreamWrapper::getExternalUrl in ./AmazonS3StreamWrapper.inc
Returns a web accessible URL for the resource.
AmazonS3StreamWrapper::stream_open in ./AmazonS3StreamWrapper.inc
Support for fopen(), file_get_contents(), file_put_contents() etc.
AmazonS3StreamWrapper::_stat in ./AmazonS3StreamWrapper.inc
Get file status.

File

./AmazonS3StreamWrapper.inc, line 1232
Drupal stream wrapper implementation for Amazon S3

Class

AmazonS3StreamWrapper
@file Drupal stream wrapper implementation for Amazon S3

Code

protected function _amazons3_get_object($uri, $cache = TRUE) {
  if ($uri === 's3://' || $uri === 's3:') {
    $metadata = $this
      ->_amazons3_format_response('/', array(), TRUE);
    return $metadata;
  }
  else {
    $uri = rtrim($uri, '/');
  }
  if ($cache) {
    $metadata = $this
      ->_amazons3_get_cache($uri);
    if ($metadata) {
      return $metadata;
    }
  }
  $is_dir = $this
    ->_amazons3_is_dir($uri);
  $metadata = NULL;
  if ($is_dir) {
    $metadata = $this
      ->_amazons3_format_response($uri, array(), TRUE);
  }
  else {
    try {
      $response = $this
        ->getS3()
        ->get_object_metadata($this->bucket, $this
        ->getLocalPath($uri));
      if ($response) {
        $metadata = $this
          ->_amazons3_format_response($uri, $response);
      }
    } catch (Exception $e) {
      watchdog('amazons3', 'Unable to get metadata for @uri', array(
        '@uri' => $uri,
      ), WATCHDOG_NOTICE);
    }
  }
  if (is_array($metadata)) {

    // Avoid any potential integrity constraint violations caused by another
    // process inserting the entry during the db_merge.
    try {

      // Save to the cache.
      db_merge('amazons3_file')
        ->key(array(
        'uri' => $metadata['uri'],
      ))
        ->fields($metadata)
        ->execute();
    } catch (Exception $e) {

      // If it fails we don't care, the entry is either already written or will
      // happen again on the next request.
    }
    return $metadata;
  }
  return FALSE;
}