C++转换文件存储类型

OSS 提供多种存储类型,包括标准、低频访问、归档、冷归档和深度冷归档,以满足从热数据到冷数据的各种存储需求。在对象存储中,一旦对象被创建,其内容是不可修改的。这意味着,如果您想更改对象的存储类型,就无法直接修改原对象,而必须创建一个新的对象。因此,转换对象的存储类型需要使用 Bucket.CopyObject 方法,这一操作会通过复制原对象来实现存储类型的转换。

注意事项

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

  • 本文以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)
{  
            
    /* yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。*/
    std::string Endpoint = "yourEndpoint";
    /* yourRegion填写Bucket所在地域对应的Region。以华东1(杭州)为例,Region填写为cn-hangzhou。*/
    std::string Region = "yourRegion";
    /* 填写Bucket名称,例如examplebucket。*/
    std::string BucketName = "examplebucket";
    /* 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。*/
    std::string ObjectName = "exampledir/exampleobject.txt";
  
    /* 初始化网络等资源。*/
    InitializeSdk();
    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);
    
    /* 设置修改后的文件存储类型,例如将修改后的文件存储类型设置为归档类型。*/
    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)
{
    
    /* yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。*/
    std::string Endpoint = "yourEndpoint";
    /* yourRegion填写Bucket所在地域对应的Region。以华东1(杭州)为例,Region填写为cn-hangzhou。*/
    std::string Region = "yourRegion";
    /* 填写Bucket名称,例如examplebucket。*/
    std::string BucketName = "examplebucket";
    /* 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。*/
    std::string ObjectName = "exampledir/exampleobject.txt";
	
    /* 初始化网络等资源。*/
    InitializeSdk();
    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);
  
    /* 检查目标文件是否为归档类型。如果是,则需要先解冻才能更改存储类型。*/
    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