文档

C++转换文件存储类型

更新时间:

OSS提供标准、低频访问、归档、冷归档和深度冷归档多种存储类型,全面覆盖从热到冷的各种数据存储场景。本文主要介绍如何转换文件(Object)的存储类型。

注意事项

  • 本文以华东1(杭州)外网Endpoint为例。如果您希望通过与OSS同地域的其他阿里云产品访问OSS,请使用内网Endpoint。关于OSS支持的Region与Endpoint的对应关系,请参见访问域名和数据中心

  • 本文以OSS域名新建OSSClient为例。如果您希望通过自定义域名、STS等方式新建OSSClient,请参见新建OssClient

  • 要转换文件存储类型,您必须具有oss:GetObjectoss:PutObjectoss:RestoreObject权限。具体操作,请参见为RAM用户授权自定义的权限策略

示例代码

将Object的存储类型从标准或低频访问转换为归档类型

#include <iostream>
#include <alibabacloud/oss/OssClient.h>

using namespace AlibabaCloud::OSS;

int main(void)
{  
            
    /* 填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。*/
    std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    /* 填写Bucket名称,例如examplebucket。*/
    std::string BucketName = "examplebucket";
    /* 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。*/
    std::string ObjectName = "exampledir/exampleobject.txt";
  
    /* 初始化网络等资源。*/
    InitializeSdk();
    ClientConfiguration conf;
    /* 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    
    /* 设置修改后的文件存储类型,例如将修改后的文件存储类型设置为归档类型。*/
    ObjectMetaData objectMeta;
    objectMeta.addHeader("x-oss-storage-class", "Archive");
    
    std::string SourceBucketName = BucketName;
    std::string SourceObjectName = ObjectName;
    
    CopyObjectRequest request(SourceBucketName, ObjectName, objectMeta);
    request.setCopySource(SourceBucketName, SourceObjectName);
    
    /* 修改为上述指定的文件存储类型。*/
    auto outcome = client.CopyObject(request);
    if (!outcome.isSuccess()) {
        /* 异常处理。*/
        std::cout << "CopyObject fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
    }
    
    /* 释放网络等资源。*/
    ShutdownSdk();
    return 0;
}

将Object的存储类型从归档转换为低频访问或者标准类型

#include <iostream>
#include <algorithm>
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
    
    /* 填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。*/
    std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    /* 填写Bucket名称,例如examplebucket。*/
    std::string BucketName = "examplebucket";
    /* 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。*/
    std::string ObjectName = "exampledir/exampleobject.txt";
	
    /* 初始化网络等资源。*/
    InitializeSdk();
    ClientConfiguration conf;
    /* 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
  
    /* 检查目标文件是否为归档类型。如果是,则需要先解冻才能更改存储类型。*/
    auto restoreOutcome = client.RestoreObject(BucketName, ObjectName);
    if (!restoreOutcome.isSuccess()) {
        /* 异常处理。*/
        std::cout << "RestoreObject fail" <<
        ",code:" << restoreOutcome.error().Code() <<
        ",message:" << restoreOutcome.error().Message() <<
        ",requestId:" << restoreOutcome.error().RequestId() << std::endl;
        return -1;
    }
    std::string onGoingRestore("ongoing-request=\"false\"");
    int maxWaitTimeInSeconds = 600;
    while (maxWaitTimeInSeconds > 0)
    {
        auto meta = client.HeadObject(BucketName, ObjectName);
        std::string restoreStatus = meta.result().HttpMetaData()["x-oss-restore"];
        std::transform(restoreStatus.begin(), restoreStatus.end(), restoreStatus.begin(), ::tolower);
        if (!restoreStatus.empty() && 
        restoreStatus.compare(0, onGoingRestore.size(), onGoingRestore)==0) {
            std::cout << " success, restore status:" << restoreStatus << std::endl;
            /* 成功解冻归档文件。*/
            break;
        }
        std::cout << " info, WaitTime:" << maxWaitTimeInSeconds
        << "; restore status:" << restoreStatus << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(10));
        maxWaitTimeInSeconds--;     
    }
    std::string SourceBucketName = BucketName;
    std::string SourceObjectName = ObjectName;
    ObjectMetaData objectMeta;
    /* 设置修改后的文件存储类型,例如将修改后的文件存储类型设置为低频访问。*/
    objectMeta.addHeader("x-oss-storage-class", "IA");
    CopyObjectRequest request(SourceBucketName, SourceObjectName, objectMeta);
    request.setCopySource(SourceBucketName, SourceObjectName);
    auto outcome = client.CopyObject(request);
    if (!outcome.isSuccess()) {
        std::cout << "CopyObject fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
    }
    ShutdownSdk();
    return 0;
}

相关文档

  • 关于转换文件存储类型的完整示例代码,请参见GitHub示例

  • 关于转换文件存储类型的API接口说明,请参见CopyObject

  • 本页导读 (1)