You are here

function S3fsStreamWrapper::_get_metadata_from_s3 in S3 File System 7

Same name and namespace in other branches
  1. 7.3 S3fsStreamWrapper.inc \S3fsStreamWrapper::_get_metadata_from_s3()
  2. 7.2 S3fsStreamWrapper.inc \S3fsStreamWrapper::_get_metadata_from_s3()

Returns the converted metadata for an object in S3.

Parameters

string $uri: The URI for the object in S3.

Return value

array An array of DB-compatible file metadata.

Throws

\Exception Any exception raised by the listObjects() S3 command will percolate out of this function.

2 calls to S3fsStreamWrapper::_get_metadata_from_s3()
S3fsStreamWrapper::stream_flush in ./S3fsStreamWrapper.inc
Support for fflush(). Flush current cached stream data to a file in S3.
S3fsStreamWrapper::_s3fs_get_object in ./S3fsStreamWrapper.inc
Try to fetch an object from the metadata cache.

File

./S3fsStreamWrapper.inc, line 1408
Drupal stream wrapper implementation for S3 File System.

Class

S3fsStreamWrapper
The stream wrapper class.

Code

function _get_metadata_from_s3($uri) {
  $this
    ->_debug("_get_metadata_from_s3({$uri}) called.", TRUE);
  $params = $this
    ->_get_params($uri);

  // TODO: Get rid of the uid column in the database. It's not used for
  // anything, and populating it requires us to use listObjects when
  // headObject alone would be sufficient.
  // In addition, the node column appears to also be entirely unused.
  // It may be worth removing that, too.
  // I wish we could call headObject(), rather than listObjects(), but
  // headObject() doesn't return the object's owner ID.
  $list_objects_result = $this->s3
    ->listObjects(array(
    'Bucket' => $params['Bucket'],
    'Prefix' => $params['Key'],
    'MaxKeys' => 1,
  ));

  // $list_objects_result['Contents'][0] is the s3 metadata. If it's unset,
  // there is no file in S3 matching this URI.
  if (empty($list_objects_result['Contents'][0])) {
    return FALSE;
  }
  $result = $list_objects_result['Contents'][0];

  // Neither headObject() nor listObjects() returns everything we need, so
  // to get the version ID of the file, we need to call both.
  $head_object_result = $this->s3
    ->headObject(array(
    'Bucket' => $params['Bucket'],
    'Key' => $params['Key'],
  ));
  if (!empty($head_object_result['VersionId'])) {
    $result['VersionId'] = $head_object_result['VersionId'];
  }
  return _s3fs_convert_metadata($uri, $result);
}