Obtain object metadata (OSS SDK for PHP V2)
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:
An OSS bucket with versioning enabled
The
oss:GetObjectpermission. For details, see Grant custom permissions to a RAM userThe version ID of the object you want to inspect
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
| Parameter | Type | Required | Description |
|---|---|---|---|
bucket | string | Yes | The bucket name |
key | string | Yes | The object key |
versionId | string | No | The 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:
| Field | Type | Description |
|---|---|---|
statusCode | int | HTTP status code. 200 indicates success |
requestId | string | Request ID for debugging and tracing |
ContentLength | int | Size of the object in bytes |
ContentType | string | MIME type of the object |
ETag | string | Entity tag (ETag) of the object, used for integrity verification |
LastModified | string | Time when the object was last modified |
VersionId | string | Version ID of the object |
HashCRC64 | string | CRC-64 checksum of the object |
Get partial metadata with GetObjectMeta
getObjectMeta can only obtain partial object metadata.
Note:getObjectMetareturns only six fields:ContentLength,ETag,LastModified,LastAccessTime,VersionId, andHashCRC64.
Request parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
bucket | string | Yes | The bucket name |
key | string | Yes | The object key |
versionId | string | No | The 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
| Field | Type | Description |
|---|---|---|
statusCode | int | HTTP status code. 200 indicates success |
requestId | string | Request ID for debugging and tracing |
ContentLength | int | Size of the object in bytes |
ETag | string | Entity tag (ETag) of the object |
LastModified | string | Time when the object was last modified |
LastAccessTime | string | Time when the object was last accessed |
VersionId | string | Version ID of the object |
HashCRC64 | string | CRC-64 checksum of the object |