Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Convert the storage class of an object (iOS SDK)

Son Güncelleme:Oct 01, 2026

Object Storage Service (OSS), sıcak verilerden soğuk verilere kadar çeşitli veri depolama senaryolarını kapsayan Standart, Seyrek Erişim (IA), Arşiv, Soğuk Arşiv ve Derin Soğuk Arşiv olmak üzere aşağıdaki depolama sınıflarını sunar. Bu konu, bir nesnenin depolama sınıfının nasıl dönüştürüleceğini açıklar.

Kullanım notları

  • Bu konudaki örnek kodu çalıştırmadan önce, özel etki alanı adı veya Security Token Service (STS) kullanma gibi yöntemlerle bir OSSClient örneği oluşturmanız gerekir. Daha fazla bilgi için bkz. Başlatma.

Örnekler

  • Bir nesnenin depolama sınıfını Standart veya IA'dan Arşiv'e dönüştürün

    Aşağıdaki örnek kod, examplebucket adlı bir paketin kök dizininde depolanan exampleobject.txt adlı bir nesnenin depolama sınıfının Standart veya IA'dan Arşiv'e nasıl dönüştürüleceğini gösterir.

    OSSCopyObjectRequest * copy = [OSSCopyObjectRequest new];
    copy.sourceBucketName = @"examplebucket";
    copy.sourceObjectKey = @"exampleobject.txt";
    copy.bucketName = @"examplebucket";
    copy.objectKey = @"exampleobject.txt";
    // Set the storage class of the exampleobject.txt object to 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;
    }];
    // Implement synchronous blocking to wait for the task to complete. 
    // [task waitUntilFinished];  
  • Bir nesnenin depolama sınıfını Arşiv, Soğuk Arşiv veya Derin Soğuk Arşiv'den IA'ya dönüştürün

    Aşağıdaki örnek kod, examplebucket adlı bir paketin kök dizininde depolanan exampleobject.txt adlı bir nesnenin depolama sınıfının Arşiv, Soğuk Arşiv veya Derin Soğuk Arşiv'den IA'ya nasıl dönüştürüleceğini gösterir.

    NSString *bucketName = @"examplebucket";
    NSString *objectKey = @"exampleobject.txt";
    
    // The following lines check whether the storage class of the object is Archive. If the storage class of the object is Archive, you must restore the object before you can convert the storage class of the object. 
    // To check whether the storage class of the object is Cold Archive or Deep Cold Archive, replace isArchived with isColdArchived or 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) {
        // Restore the object. 
        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];
    
        // Wait until the object is restored. 
        __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);
    }
    
    // Set the storage class of the exampleobject.txt object to 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];

İlgili bilgiler

  • Bir nesnenin depolama sınıfını dönüştürmek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. CopyObject.

  • Bir OSSClient örneğinin nasıl başlatılacağı hakkında daha fazla bilgi için bkz. Başlatma.