Obtain file metadata (PHP SDK V1)

Updated at:

By default, calling the HeadObject operation on a versioning-enabled bucket returns the metadata of only the current version of an object.

Precautions

  • In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and endpoints.

  • This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Create an OssClient.

  • To obtain file metadata, you must have the oss:GetObject permission. For more information, see Grant custom access policies to RAM users.

Example code

The following code shows how to obtain file metadata:

Note

If the current version of an object is a delete marker, a 404 Not Found error is returned. If you specify a version ID in the request, the metadata of the specified object version is returned.

<?php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}

use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;

// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
$provider = new EnvironmentVariableCredentialsProvider();
// Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "yourEndpoint";
// Set the bucket name. Example: examplebucket.
$bucket= "examplebucket";
// Set the full path of the object. The full path cannot contain the bucket name. Example: exampledir/exampleobject.txt.
$object = "exampledir/exampleobject.txt";
// Set the version ID of the object.
$versionId = "CAEQMxiBgICAof2D0BYiIDJhMGE3N2M1YTI1NDQzOGY5NTkyNTI3MGYyMzJm****";

$config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"=> "cn-hangzhou"
    );
    $ossClient = new OssClient($config);

try {
    // Obtain all metadata of the specified object version.
    $objectMeta = $ossClient->getObjectMeta($bucket, $object, array(OssClient::OSS_VERSION_ID => $versionId));
    print_r($objectMeta);

    // Obtain partial metadata of the specified object version.
    $objectMeta = $ossClient->getSimplifiedObjectMeta($bucket, $object, array(OssClient::OSS_VERSION_ID => $versionId));
    print_r($objectMeta);
  
} catch (OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}

print(__FUNCTION__ . ": OK" . "\n");

References

For more information about how to obtain file metadata, see GetObjectMeta.