Get object metadata (C++ SDK)
By default, calling the HeadObject operation on an object in a versioning-enabled bucket retrieves the metadata of the current version.
If the current version of an object is a delete marker, a 404 Not Found error is returned. To retrieve the metadata of a specific version, specify its versionId in the request.
Notes
-
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS 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 instance.
-
To query object metadata, you must have the
oss:GetObjectpermission. For more information, see Grant a custom policy.
Sample code
The following code provides an example of how to retrieve object metadata:
#include <alibabacloud/oss/OssClient.h>
#include <alibabacloud/oss/model/ObjectMetaData.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the OSS account information. */
/* Set yourEndpoint to the endpoint of the region where the bucket is located. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name, for example, examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt. */
std::string ObjectName = "exampledir/exampleobject.txt";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before running this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Get partial metadata of the object. */
GetObjectMetaRequest request(BucketName, ObjectName);
request.setVersionId("yourObjectVersionId");
auto outcome = client.GetObjectMeta(request);
if (!outcome.isSuccess()) {
/* Handle the exception. */
std::cout << "GetObjectMeta fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
else {
auto metadata = outcome.result();
std::cout << " get metadata success, VersionId:" << metadata.VersionId() << std::endl;
}
/* Get all metadata of the object. */
HeadObjectRequest request1(BucketName, ObjectName);
request1.setVersionId("yourObjectVersionId");
outcome = client.HeadObject(request1);
if (!outcome.isSuccess()) {
/* Handle the exception. */
std::cout << "HeadObject fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
else {
auto headMeta = outcome.result();
std::cout << " get headMeta success, VersionId:" << headMeta.VersionId() << std::endl;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
References
For more information about the API operations used to retrieve object metadata, see HeadObject and GetObjectMeta.