默认情况下,在受版本控制的存储空间(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账号信息。*/
    /* 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。*/
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    /* yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。*/
    std::string Endpoint = "yourEndpoint";
    /* 填写Bucket名称,例如examplebucket。*/
    std::string BucketName = "examplebucket";
    /* 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。*/
    std::string ObjectName = "exampledir/exampleobject.txt";

    /* 初始化网络等资源。*/
    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;
}

相关文档

关于获取文件元信息的API接口说明,请参见HeadObjectGetObjectMeta