All Products
Search
Document Center

Object Storage Service:Convert the storage class of an object

Last Updated:Jan 18, 2024

Object Storage Service (OSS) provides the following storage classes to cover various data storage scenarios from hot data to cold data: Standard, Infrequent Access (IA), Archive, Cold Archive, and Deep Cold Archive. This topic describes how to convert the storage class of an object.

Examples

  • Convert the storage class of an object from Standard or IA to Archive

    The following sample code provides an example on how to convert the storage class of an object named exampleobject.txt from Standard or IA to Archive. In this example, the object is stored in the root directory of a bucket named examplebucket.

    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;
    }];
  • Convert the storage class of an object from Archive or Cold Archive to IA

    The following sample code provides an example on how to convert the storage class of an object named exampleobject.txt from Archive or Cold Archive to IA. In this example, the object is stored in the root directory of a bucket named examplebucket.

    NSString *bucketName = @"examplebucket";
    NSString *objectKey = @"exampleobject.txt";
    
    // The following sample code provides an example on how to 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];

References

For more information about the API operation that you can call to convert the storage class of an object, see CopyObject.