Object Storage Service (OSS) supports the Standard, Infrequent Access (IA), Archive, and Cold Archive storage classes. You can call the CopyObject operation to overwrite the source object. This way, the storage class of the destination object is specified as the storage class of the object.

Sample code

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

    The following 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 the 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 object named exampleobject.txt 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 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 the bucket named examplebucket.

    NSString *bucketName = @"examplebucket";
    NSString *objectKey = @"exampleobject.txt";
    
    // Check whether the storage class of the object is Archive or Cold Archive. If the storage class of the object is Archive or Cold Archive, you must restore the object before you can change the storage class of the object. 
    __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 object named exampleobject.txt 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.