默认情况下,在受版本控制的存储空间(Bucket)中调用GetObject接口仅返回文件(Object)的当前版本。

对某个Bucket执行GetObject操作时,分以下三种情况:
  • 如果该Bucket中Object的当前版本是删除标记(Delete Marker),则返回404 Not Found。
  • 如果在查询参数中指定Object的versionId,则返回指定的Object版本。当versionId指定为“null”时,则返回versionId为“null”的Object版本。
  • 通过指定versionId的方式来获取删除标记时,则返回405 Method Not Allowed。

以下代码用于下载文件:

#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);

    /*获取文件到本地内存*/
    GetObjectRequest request(BucketName, ObjectName);
    request.setVersionId("yourObjectVersionId");
    auto outcome = client.GetObject(request);
    if (outcome.isSuccess()) {
        std::cout << "getObjectToBuffer" << " success, Content-Length:" << outcome.result().Metadata().ContentLength() << std::endl;
        /*读取下载的object内容*/
        std::string content;
        *(outcome.result().Content()) >> content;
        std::cout << "getObjectToBuffer" << "content:" << content << std::endl;
        /*查看本次下载object的版本Id*/
        std::cout << "versionid:" << outcome.result().VersionId() << std::endl;
    }
    else {
            /*异常处理*/
            std::cout << "getObjectToBuffer fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
    }

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

下载文件的详细信息请参见GetObject