Obtain object metadata (OSS SDK for PHP V2)
This topic describes how to obtain object metadata in a versioning-enabled bucket.
Considerations
-
The sample code in this topic uses the public endpoint of the China (Hangzhou) region as an example. The region ID is
cn-hangzhou. If you want to access OSS from other Alibaba Cloud services in the same region, use the internal endpoint. For more information about the regions and endpoints supported by OSS, see OSS regions and endpoints. -
To obtain object metadata, you must have the
oss:GetObjectpermission. For more information, see Grant custom permissions to a RAM user.
Sample code
Use the HeadObject method to get all metadata of an object
You can use the following code to use the HeadObject method to obtain all metadata of a specified object version.
<?php
// Import the autoloader file to ensure that dependency libraries are loaded correctly.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define the description of command-line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region in which the bucket is located. (Required)
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint. (Optional)
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // The bucket name. (Required)
"key" => ['help' => 'The name of the object', 'required' => True], // The object name. (Required)
];
// Convert the argument descriptions to the long option format required by getopt.
// A colon ":" after each argument indicates that the argument requires a value.
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
// Parse the command-line arguments.
$options = getopt("", $longopts);
// Verify that all required arguments are provided.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help']; // Get the help information for the argument.
echo "Error: the following arguments are required: --$key, $help" . PHP_EOL;
exit(1); // If a required argument is missing, exit the program.
}
}
// Extract values from the parsed arguments.
$region = $options["region"]; // The region in which the bucket is located.
$bucket = $options["bucket"]; // The bucket name.
$key = $options["key"]; // The object name.
// Load credentials from environment variables.
// Use EnvironmentVariableCredentialsProvider to read the Access Key ID and Access Key Secret from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider); // Set the credential provider.
$cfg->setRegion($region); // Set the region in which the bucket is located.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]); // If an endpoint is provided, set the endpoint.
}
// Create an OSS client instance.
$client = new Oss\Client($cfg);
// Create a HeadObjectRequest object to obtain the metadata of the object.
$request = new Oss\Models\HeadObjectRequest(
bucket: $bucket,
key: $key,
versionId:"yourVersionId", //Specify the actual version ID.
);
// Execute the operation to obtain the object metadata.
$result = $client->headObject($request);
// Print the result of obtaining the metadata.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code. For example, 200 indicates success.
'request id:' . $result->requestId . PHP_EOL . // The request ID, which is used for debugging or tracing the request.
'result:' . var_export($result, true) . PHP_EOL // The detailed metadata result.
);
Use the getObjectMeta method to get partial metadata of an object
The GetObjectMeta method can only obtain partial object metadata, including the following: content length (ContentLength), entity tag (ETag), the time when the returned object was last modified (LastModified), the time when the object was last accessed (LastAccessTime), the version ID of the object (VersionId), and the 64-bit CRC value of the object (HashCRC64).
You can use the following code to use the GetObjectMeta method to obtain partial metadata of a specified object version.
<?php
// Include the autoloader file to ensure that dependencies are loaded correctly.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define the descriptions for the command-line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region where the bucket is located. (Required)
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint. (Optional)
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // The bucket name. (Required)
"key" => ['help' => 'The name of the object', 'required' => True], // The object name. (Required)
];
// Convert the parameter descriptions to the long option format that is required by getopt.
// A colon (:) after each parameter indicates that the parameter requires a value.
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
// Parse the command-line arguments.
$options = getopt("", $longopts);
// Verify that required parameters are specified.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help']; // Obtain the help information for the parameter.
echo "Error: the following arguments are required: --$key, $help" . PHP_EOL;
exit(1); // If a required parameter is not specified, exit the program.
}
}
// Extract values from the parsed parameters.
$region = $options["region"]; // The region where the bucket is located.
$bucket = $options["bucket"]; // The bucket name.
$key = $options["key"]; // The object name.
// Load credentials from environment variables.
// Use EnvironmentVariableCredentialsProvider to read the Access Key ID and Access Key Secret from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default SDK configurations.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider); // Set the credential provider.
$cfg->setRegion($region); // Set the region where the bucket is located.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]); // If an endpoint is provided, set the endpoint.
}
// Create an OSS client instance.
$client = new Oss\Client($cfg);
// Create a GetObjectMetaRequest object to obtain the metadata of an object.
$request = new Oss\Models\GetObjectMetaRequest(
bucket: $bucket,
key: $key,
versionId:"yourVersionId", // Specify the actual version ID.
);
// Obtain the object metadata.
$result = $client->getObjectMeta($request);
// Print the metadata.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code. For example, 200 indicates a successful request.
'request id:' . $result->requestId . PHP_EOL . // The request ID, which is used to debug or trace the request.
'result:' . var_export($result, true) . PHP_EOL // The detailed metadata.
);