get_object_metadata ( $bucket, $filename, $opt )

Gets the collective metadata for the given Amazon S3 object.

The MD5 value for an object can be retrieved from the ETag HTTP header for any object that was uploaded with a normal PUT/POST. This value is incorrect for multipart uploads.

Access

public

Parameters

Parameter

Type

Required

Description

$bucket

string

Required

The name of the bucket to use.

$filename

string

Required

The file name for the Amazon S3 object.

$opt

array

Optional

An associative array of parameters that can have the following keys:

  • versionId - string - Optional - The version of the object to retrieve. Version IDs are returned in the x-amz-version-id header of any previous object-related request.
  • curlopts - array - Optional - A set of values to pass directly into curl_setopt(), where the key is a pre-defined CURLOPT_* constant.
  • returnCurlHandle - boolean - Optional - A private toggle specifying that the cURL handle be returned rather than actually completing the request. This toggle is useful for manually managed batch requests.

Returns

Type

Description

mixed

If the object exists, the method returns the collective metadata for the Amazon S3 object. If the object does not exist, the method returns boolean false.

Examples

Get the metadata for an object.

// Instantiate the class
$s3 = new AmazonS3();
$bucket = 'my-bucket' . strtolower($s3->key);

$response = $s3->get_object_metadata($bucket, 'üpløåd/î\'vé nøw béén üpløådéd.txt');

// Success?
var_dump($response['ContentType']);
var_dump($response['Headers']['content-language']);
var_dump($response['Headers']['x-amz-meta-ice-ice-baby']);
Result:
string(10) "text/plain"
string(5) "en-US"
string(18) "too cold, too cold"

Related Methods

Source

Method defined in services/s3.class.php | Toggle source view (63 lines) | View on GitHub

public function get_object_metadata($bucket, $filename, $opt = null)
{
    $batch = new CFBatchRequest();
    $this->batch($batch)->get_object_acl($bucket, $filename); // Get ACL info
    $this->batch($batch)->get_object_headers($bucket, $filename); // Get content-type
    $this->batch($batch)->list_objects($bucket, array( // Get other metadata
        'max-keys' => 1,
        'prefix' => $filename
    ));
    $response = $this->batch($batch)->send();

    // Fail if any requests were unsuccessful
    if (!$response->areOK())
    {
        return false;
    }

    $data = array(
        'ACL'          => array(),
        'ContentType'  => null,
        'ETag'         => null,
        'Headers'      => null,
        'Key'          => null,
        'LastModified' => null,
        'Owner'        => array(),
        'Size'         => null,
        'StorageClass' => null,
    );

    // Add the content type
    $data['ContentType'] = (string) $response[1]->header['content-type'];

    // Add the other metadata (including storage type)
    $contents = json_decode(json_encode($response[2]->body->query('descendant-or-self::Contents')->first()), true);
    $data = array_merge($data, (is_array($contents) ? $contents : array()));

    // Add ACL info
    $grants = $response[0]->body->query('descendant-or-self::Grant');
    $max = count($grants);

    // Add raw header info
    $data['Headers'] = $response[1]->header;
    foreach (array('_info', 'x-amz-id-2', 'x-amz-request-id', 'cneonction', 'server', 'content-length', 'content-type', 'etag') as $header)
    {
        unset($data['Headers'][$header]);
    }
    ksort($data['Headers']);

    if (count($grants) > 0)
    {
        foreach ($grants as $grant)
        {
            $dgrant = array(
                'id' => (string) $this->util->try_these(array('ID', 'URI'), $grant->Grantee),
                'permission' => (string) $grant->Permission
            );

            $data['ACL'][] = $dgrant;
        }
    }

    return $data;
}

Copyright © 2010–2013 Amazon Web Services, LLC


Feedback