OSS provides two operations for retrieving object metadata, each returning a different scope of information:
`GetObjectMeta` — returns a lightweight subset of metadata (ETag, object size, and last-modified time). Use this when you only need to check whether an object exists or get its basic properties.
`HeadObject` — returns the complete set of object metadata, including content type, content encoding, cache control, and custom headers. Use this when you need the full object properties.
In a versioning-enabled bucket, both operations return the metadata of the current version by default. If the current version is a delete marker, a 404 Not Found error is returned. To retrieve metadata for a specific version, set the versionId parameter in the request.
Prerequisites
Before you begin, ensure that you have:
The
oss:GetObjectpermission on the target object. For more information, see Attach a custom policy to a RAM userThe OSS C++ SDK installed and an
OssClientinstance initialized. For more information, see Create an OSSClient instance
Usage notes
The examples in this topic use the public endpoint for the China (Hangzhou) region. To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. For endpoint details, see Regions and endpoints.
The examples use
EnvironmentVariableCredentialsProviderto load credentials from theOSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables. Set these variables before running the code.
Get object metadata
The following example calls both GetObjectMeta and HeadObject and prints the returned VersionId.
#include <alibabacloud/oss/OssClient.h>
#include <alibabacloud/oss/model/ObjectMetaData.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Set the endpoint of the region where the bucket is located.
Example: https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou). */
std::string Endpoint = "yourEndpoint";
/* Set the region ID. Example: cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name. */
std::string BucketName = "examplebucket";
/* Specify the full object path, excluding the bucket name.
Example: exampledir/exampleobject.txt. */
std::string ObjectName = "exampledir/exampleobject.txt";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Load credentials from environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* GetObjectMeta: retrieve partial metadata (ETag, object size, and last-modified time). */
GetObjectMetaRequest metaRequest(BucketName, ObjectName);
metaRequest.setVersionId("yourObjectVersionId");
auto metaOutcome = client.GetObjectMeta(metaRequest);
if (!metaOutcome.isSuccess()) {
std::cout << "GetObjectMeta failed"
<< ", code: " << metaOutcome.error().Code()
<< ", message: " << metaOutcome.error().Message()
<< ", requestId: " << metaOutcome.error().RequestId()
<< std::endl;
ShutdownSdk();
return -1;
}
auto meta = metaOutcome.result();
std::cout << "GetObjectMeta succeeded, VersionId: " << meta.VersionId() << std::endl;
/* HeadObject: retrieve all metadata, including content type, content encoding,
cache control, and custom headers. */
HeadObjectRequest headRequest(BucketName, ObjectName);
headRequest.setVersionId("yourObjectVersionId");
auto headOutcome = client.HeadObject(headRequest);
if (!headOutcome.isSuccess()) {
std::cout << "HeadObject failed"
<< ", code: " << headOutcome.error().Code()
<< ", message: " << headOutcome.error().Message()
<< ", requestId: " << headOutcome.error().RequestId()
<< std::endl;
ShutdownSdk();
return -1;
}
auto headMeta = headOutcome.result();
std::cout << "HeadObject succeeded, VersionId: " << headMeta.VersionId() << std::endl;
/* Release network resources. */
ShutdownSdk();
return 0;
}Replace the placeholders with your actual values:
| Placeholder | Description | Example |
|---|---|---|
yourEndpoint | Endpoint of the region where the bucket is located | https://oss-cn-hangzhou.aliyuncs.com |
yourRegion | Region ID | cn-hangzhou |
yourObjectVersionId | Version ID of the object (omit to retrieve the current version) | (found in the object's version list) |
Versioning behavior
When working with versioned objects, be aware of the following behavior:
| Scenario | Result |
|---|---|
| Current version is a delete marker | Returns 404 Not Found |
Specified versionId is a valid object version | Returns metadata for that version |
versionId is not specified | Returns metadata for the current version |