Obtain object metadata (OSS SDK for PHP V2)

Updated at:

Retrieve the metadata of a specific object version in a versioning-enabled bucket using headObject (all metadata) or getObjectMeta (partial metadata).

Prerequisites

Before you begin, ensure that you have:

Usage notes

  • The sample code uses the public endpoint of the China (Hangzhou) region (cn-hangzhou). To access OSS from other Alibaba Cloud services in the same region, use the internal endpoint. For a full list of regions and endpoints, see OSS regions and endpoints.

Get all metadata with HeadObject

headObject retrieves all metadata of a specific object version.

Request parameters

ParameterTypeRequiredDescription
bucketstringYesThe bucket name
keystringYesThe object key
versionIdstringNoThe version ID of the object
<?php

require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

$optsdesc = [
    "region"   => ['help' => 'The region in which the bucket is located.', 'required' => True],
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
    "bucket"   => ['help' => 'The name of the bucket', 'required' => True],
    "key"      => ['help' => 'The name of the object', 'required' => True],
];

$longopts = \array_map(function ($key) {
    return "$key:";
}, array_keys($optsdesc));

$options = getopt("", $longopts);

foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        $help = $value['help'];
        echo "Error: the following arguments are required: --$key, $help" . PHP_EOL;
        exit(1);
    }
}

$region = $options["region"];
$bucket = $options["bucket"];
$key    = $options["key"];

// Load credentials from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]);
}

$client = new Oss\Client($cfg);

// Specify the version ID to retrieve metadata for a specific version.
$request = new Oss\Models\HeadObjectRequest(
    bucket:    $bucket,
    key:       $key,
    versionId: "yourVersionId", // Replace with the actual version ID.
);

$result = $client->headObject($request);

printf(
    'status code: ' . $result->statusCode . PHP_EOL .
    'request ID: '  . $result->requestId  . PHP_EOL .
    'result: '      . var_export($result, true) . PHP_EOL
);

Response fields

headObject returns all standard object metadata. The following table describes the key fields:

FieldTypeDescription
statusCodeintHTTP status code. 200 indicates success
requestIdstringRequest ID for debugging and tracing
ContentLengthintSize of the object in bytes
ContentTypestringMIME type of the object
ETagstringEntity tag (ETag) of the object, used for integrity verification
LastModifiedstringTime when the object was last modified
VersionIdstringVersion ID of the object
HashCRC64stringCRC-64 checksum of the object

Get partial metadata with GetObjectMeta

getObjectMeta can only obtain partial object metadata.

Note: getObjectMeta returns only six fields: ContentLength, ETag, LastModified, LastAccessTime, VersionId, and HashCRC64.

Request parameters

ParameterTypeRequiredDescription
bucketstringYesThe bucket name
keystringYesThe object key
versionIdstringNoThe version ID of the object
<?php

require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

$optsdesc = [
    "region"   => ['help' => 'The region in which the bucket is located.', 'required' => True],
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
    "bucket"   => ['help' => 'The name of the bucket', 'required' => True],
    "key"      => ['help' => 'The name of the object', 'required' => True],
];

$longopts = \array_map(function ($key) {
    return "$key:";
}, array_keys($optsdesc));

$options = getopt("", $longopts);

foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        $help = $value['help'];
        echo "Error: the following arguments are required: --$key, $help" . PHP_EOL;
        exit(1);
    }
}

$region = $options["region"];
$bucket = $options["bucket"];
$key    = $options["key"];

// Load credentials from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]);
}

$client = new Oss\Client($cfg);

// Specify the version ID to retrieve metadata for a specific version.
$request = new Oss\Models\GetObjectMetaRequest(
    bucket:    $bucket,
    key:       $key,
    versionId: "yourVersionId", // Replace with the actual version ID.
);

$result = $client->getObjectMeta($request);

printf(
    'status code: ' . $result->statusCode . PHP_EOL .
    'request ID: '  . $result->requestId  . PHP_EOL .
    'result: '      . var_export($result, true) . PHP_EOL
);

Response fields

FieldTypeDescription
statusCodeintHTTP status code. 200 indicates success
requestIdstringRequest ID for debugging and tracing
ContentLengthintSize of the object in bytes
ETagstringEntity tag (ETag) of the object
LastModifiedstringTime when the object was last modified
LastAccessTimestringTime when the object was last accessed
VersionIdstringVersion ID of the object
HashCRC64stringCRC-64 checksum of the object