全部产品
Search
文档中心

对象存储 OSS:iOS转换文件存储类型

更新时间:Aug 28, 2024

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

注意事项

  • 使用本文示例前您需要先通过自定义域名、STS等方式新建OSSClient,具体请参见初始化

示例代码

  • 标准或低频访问类型转换为归档类型

    以下代码用于将examplebucket根目录下名为exampleobject.txt的Object的存储类型从标准或低频访问类型转换为归档类型:

    OSSCopyObjectRequest * copy = [OSSCopyObjectRequest new];
    copy.sourceBucketName = @"examplebucket";
    copy.sourceObjectKey = @"exampleobject.txt";
    copy.bucketName = @"examplebucket";
    copy.objectKey = @"exampleobject.txt";
    // 将exampleobject.txt的存储类型指定为归档类型Archive。
    copy.objectMeta = @{@"x-oss-storage-class" : @"Archive"};
    
    OSSTask * task = [client copyObject:copy];
    [task continueWithBlock:^id(OSSTask *task) {
        if (!task.error) {
            NSLog(@"copy object success!");
        } else {
            NSLog(@"copy object failed, error: %@" , task.error);
        }
        return nil;
    }];
    // 实现同步阻塞等待任务完成。
    // [task waitUntilFinished];  
  • 归档、冷归档类型或者深度冷归档转换为低频访问类型

    以下代码用于将examplebucket根目录下名为exampleobject.txt的Object的存储类型从归档、冷归档类型或者深度冷归档类型转换为低频访问类型:

    NSString *bucketName = @"examplebucket";
    NSString *objectKey = @"exampleobject.txt";
    
    // 以下以检查文件是否为归档类型为例。如果文件为归档类型,则需要先解冻文件才能更改其存储类型。
    // 如需检查文件是否为冷归档或者深度冷归档,需要将isArchived替换为isColdArchived或者isDeepColdArchived。
    __block bool isArchived = false;
    OSSHeadObjectRequest *headRq = [OSSHeadObjectRequest new];
    headRq.bucketName = bucketName;
    headRq.objectKey = objectKey;
    [[[client headObject:headRq] continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) {
        if (!task.error) {
            OSSHeadObjectResult *headRs = task.result;
            isArchived = [headRs.httpResponseHeaderFields[@"x-oss-storage-class"] isEqualToString:@"Archive"];
        } else {
            NSLog(@"head object fail! error: %@", task.error);
        }
        return nil;
    }] waitUntilFinished];
    
    if (isArchived) {
        // 解冻文件。
        OSSRestoreObjectRequest *restoreRequest = [OSSRestoreObjectRequest new];
        restoreRequest.bucketName = bucketName;
        restoreRequest.objectKey = objectKey;
        [[[client restoreObject:restoreRequest] continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) {
            if (!task.error) {
                NSLog(@"restore object success!");
            } else {
                NSLog(@"restore object fail! error: %@", task.error);
            }
            return nil;
        }] waitUntilFinished];
    
        // 等待解冻完成。
        __block bool isRestored = false;
        do {
            sleep(1);
            headRq = [OSSHeadObjectRequest new];
            headRq.bucketName = bucketName;
            headRq.objectKey = objectKey;
            [[[client headObject:headRq] continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) {
                if (!task.error) {
                    OSSHeadObjectResult *headRs = task.result;
                    isRestored = (headRs.httpResponseHeaderFields[@"x-oss-restore"] != nil &&
                                  ![headRs.httpResponseHeaderFields[@"x-oss-restore"] isEqualToString:@"ongoing-request=\"true\""]);
                } else {
                    NSLog(@"head object fail! error: %@", task.error);
                }
                return nil;
            }] waitUntilFinished];
        } while (!isRestored);
    }
    
    // 将exampleobject.txt的存储类型指定为低频访问类型IA。
    OSSCopyObjectRequest *copy = [OSSCopyObjectRequest new];
    copy.bucketName = bucketName;
    copy.objectKey = objectKey;
    copy.sourceBucketName = bucketName;
    copy.sourceObjectKey = objectKey;
    copy.objectMeta = @{@"x-oss-storage-class": @"IA"};
    
    [[[client copyObject:copy] continueWithBlock:^id(OSSTask *task) {
        if (!task.error) {
            NSLog(@"copy object success!");
        } else {
            NSLog(@"copy object failed, error: %@" , task.error);
        }
        return nil;
    }] waitUntilFinished];

相关文档