更新时间:2023-11-09 GMT+08:00

下载归档存储对象

如果要下载归档存储对象,需要先将归档存储对象恢复。恢复归档存储对象的恢复选项可支持二类,见下表:

选项

说明

OBS iOS SDK对应值

快速恢复

恢复耗时1~5分钟。

OBSRestoreTierExpedited

标准恢复

恢复耗时3~5小时。默认值。

OBSRestoreTierStandard

重复恢复归档存储数据时在延长恢复有效期的同时,也将会对恢复时产生的恢复费用进行重复收取。产生的标准存储类别的对象副本有效期将会延长,并且收取延长时间段产生的标准存储副本费用。

您可以通过OBSRestoreObjectRequest恢复归档存储对象。以下代码展示了如何下载归档存储对象:

static OBSClient *client;
NSString *endPoint = @"your-endpoint";
// 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;本示例以ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量AccessKeyID和SecretAccessKey。
// 您可以登录访问管理控制台获取访问密钥AK/SK,获取方式请参见https://support.huaweicloud.com/intl/zh-cn/usermanual-ca/ca_01_0003.html
char* ak_env = getenv("AccessKeyID");
char* sk_env = getenv("SecretAccessKey");
NSString *AK = [NSString stringWithUTF8String:ak_env];
NSString *SK = [NSString stringWithUTF8String:sk_env];
    
// 初始化身份验证
OBSStaticCredentialProvider *credentialProvider = [[OBSStaticCredentialProvider alloc] initWithAccessKey:AK secretKey:SK];
    
//初始化服务配置
OBSServiceConfiguration *conf = [[OBSServiceConfiguration alloc] initWithURLString:endPoint credentialProvider:credentialProvider];
    
// 初始化client
client = [[OBSClient alloc] initWithConfiguration:conf];
    
// 恢复对象
OBSRestoreObjectRequest *request = [[OBSRestoreObjectRequest alloc]initWithBucketName:@"bucketname" objectKey:@"objectname" storeDays:[NSNumber numberWithInt:30]]; //1 to 30
request.restoreTier = OBSRestoreTierExpedited;
    
OBSBFTask *task = [ self.client restoreObject:request completionHandler:^(OBSRestoreObjectResponse *response, NSError *error){
    NSLog(@"%@",response);
}];
    
// 等待对象恢复
sleep(6*60);
    
// 下载对象
NSString * outfilePath = [NSTemporaryDirectory() stringByAppendingString:@"filename"];
OBSGetObjectToFileRequest *request1 = [[OBSGetObjectToFileRequest alloc]initWithBucketName:@"bucketname" objectKey:@"objectname" downloadFilePath:outfilePath];
request1.downloadProgressBlock = ^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
    NSLog(@"%0.1f%%",(float)floor(totalBytesWritten*10000/totalBytesExpectedToWrite)/100);
};
    
[client getObject:request1 completionHandler:^(OBSGetObjectResponse *response, NSError *error){
    NSLog(@"%@",response.etag);
}];