全部產品
Search
文件中心

Object Storage Service: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];

相關文檔