文件元信息(Object Meta)包括HTTP header和自定义元信息。

说明 有关文件元信息的更多详情,请参见开发指南中的文件元信息

设置文件元信息

以下代码用于设置文件元信息:
#include <alibabacloud/oss/OssClient.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);


    /* 设置HTTP header */
    auto meta = ObjectMetaData();
    meta.setContentType("text/plain");
    meta.setCacheControl("max-ag e=3");
    /* 设置自定义文件元信息 */
    meta.UserMetaData()["meta"] = "meta-value";

    std::shared_ptr<std::iostream> content = std::make_shared<std::stringstream>();
    *content << "Thank you for using Aliyun Object Storage Service!";
    client.PutObject(BucketName, ObjectName, content, meta);

    if (!outcome.isSuccess()) {
        /* 异常处理 */
        std::cout << "PutObject fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        ShutdownSdk();
        return -1;
    }

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

获取文件元信息

您可以通过以下两种方法获取文件元信息:

方法 描述 优势
GetObjectMeta 获取文件的ETag、Size(文件大小)、 LastModified(最后修改时间)。 更轻量、更快
HeadObject 获取文件的全部元信息。

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

#include <alibabacloud/oss/OssClient.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);

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

    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, ETag:" << metadata.ETag() << "; LastModified:" 
        << metadata.LastModified() << "; Size:" << metadata.ContentLength() << std::endl;
    }

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

    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 <<"headMeta success, ContentType:" 
        << headMeta.ContentType() << "; ContentLength:" << headMeta.ContentLength() 
        << "; CacheControl:" << headMeta.CacheControl() << std::endl;
    }

    /* 释放网络等资源 */
    ShutdownSdk();
    return 0;
}
说明 HTTP header详情请参见RFC2616