默认情况下,在受版本控制的存储空间(Bucket)中调用HeadObject接口仅获取文件(Object)当前版本的meta信息。

说明 如果Object的当前版本为删除标记,则返回404 Not Found。请求参数中指定versionId则返回指定版本Object的meta信息。

以下代码用于获取文件元信息:

#include <alibabacloud/oss/OssClient.h>
#include <alibabacloud/oss/model/ObjectMetaData.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
    std::string ObjectName = "yourObjectName";

    /* 初始化网络等资源 */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

    /* 获取文件的部分元信息 */
    GetObjectMetaRequest request(BucketName, ObjectName);
    request.setVersionId("yourObjectVersionId");
    auto outcome = client.GetObjectMeta(request);

    if (!outcome.isSuccess()) {
            /* 异常处理 */
            std::cout << "GetObjectMeta fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
    }
    else { 
            auto metadata = outcome.result();
            std::cout << " get metadata success, VersionId:" << metadata.VersionId() << std::endl;
    }

    /* 获取文件的全部元信息 */
    HeadObjectRequest request1(BucketName, ObjectName);
    request1.setVersionId("yourObjectVersionId");
    outcome = client.HeadObject(request1);

    if (!outcome.isSuccess()) {
            /* 异常处理 */
            std::cout << "HeadObject fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
ShutdownSdk();
            return -1;
    }
    else { 
            auto headMeta = outcome.result();
            std::cout << " get headMeta success, VersionId:" << headMeta.VersionId() << std::endl;
    }

    /* 释放网络等资源 */
    ShutdownSdk();
    return 0;
}

获取文件元信息的详情请参见HeadObjectGetObjectMeta