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

Sample code

The following section provides examples on how to convert the storage class of an object.
  • The following code provides an example on how to convert the storage class of an object from Standard or IA to Archive:
    import com.aliyun.oss.ClientException;
    import com.aliyun.oss.OSS;
    import com.aliyun.oss.OSSClientBuilder;
    import com.aliyun.oss.OSSException;
    import com.aliyun.oss.model.CopyObjectRequest;
    import com.aliyun.oss.model.CopyObjectResult;
    import com.aliyun.oss.model.ObjectMetadata;
    import com.aliyun.oss.model.StorageClass;
    
    public class Demo {
        public static void main(String[] args) throws Exception {
            // In this example, the endpoint of the China (Hangzhou) region is used. Specify the actual endpoint. 
            String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
            // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
            String accessKeyId = "yourAccessKeyId";
            String accessKeySecret = "yourAccessKeySecret";
            // In this example, a bucket and a Standard or an IA object have been created. 
            // Specify the name of the bucket. Example: examplebucket. 
            String bucketName = "examplebucket";
            // Specify the full path of the object. The full path of the object cannot contain the bucket name. Example: exampleobject.txt. 
            String objectName = "exampleobject.txt";
    
            // Create an OSSClient instance. 
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    
            try {
                // Create a CopyObjectRequest object. 
                CopyObjectRequest request = new CopyObjectRequest(bucketName, objectName, bucketName, objectName) ;
    
                // Create an ObjectMetadata object. 
                ObjectMetadata objectMetadata = new ObjectMetadata();
    
                // Encapsulate the header. In this example, the storage class is converted to Archive. 
                objectMetadata.setHeader("x-oss-storage-class", StorageClass.Archive);
                request.setNewObjectMetadata(objectMetadata);
    
                // Convert the storage class of the object. 
                CopyObjectResult result = ossClient.copyObject(request);
            } catch (OSSException oe) {
                System.out.println("Caught an OSSException, which means your request made it to OSS, "
                        + "but was rejected with an error response for some reason.");
                System.out.println("Error Message:" + oe.getErrorMessage());
                System.out.println("Error Code:" + oe.getErrorCode());
                System.out.println("Request ID:" + oe.getRequestId());
                System.out.println("Host ID:" + oe.getHostId());
            } catch (ClientException ce) {
                System.out.println("Caught an ClientException, which means the client encountered "
                        + "a serious internal problem while trying to communicate with OSS, "
                        + "such as not being able to access the network.");
                System.out.println("Error Message:" + ce.getMessage());
            } finally {
                if (ossClient != null) {
                    ossClient.shutdown();
                }
            }
        }
    }
  • The following code provides an example on how to convert the storage class of an object from Archive to IA:
    package com.aliyun.oss.demo;
    
    import com.aliyun.oss.ClientException;
    import com.aliyun.oss.OSS;
    import com.aliyun.oss.OSSClientBuilder;
    import com.aliyun.oss.OSSException;
    import com.aliyun.oss.model.CopyObjectRequest;
    import com.aliyun.oss.model.CopyObjectResult;
    import com.aliyun.oss.model.ObjectMetadata;
    import com.aliyun.oss.model.StorageClass;
    
    public class Demo {
        public static void main(String[] args) throws Exception {
            // In this example, the endpoint of the China (Hangzhou) region is used. Specify the actual endpoint. 
            String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
            // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
            String accessKeyId = "yourAccessKeyId";
            String accessKeySecret = "yourAccessKeySecret";
            // In this example, a bucket and an Archive object have been created. 
            // Specify the name of the bucket. Example: examplebucket. 
            String bucketName = "examplebucket";
            // Specify the full path of the object. The full path of the object cannot contain the bucket name. Example: exampleobject.txt. 
            String objectName = "exampleobject.txt";
    
            // Create an OSSClient instance. 
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    
            try {
                // Query the object metadata. 
                ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucketName, objectName);
    
                // Check whether the storage class of the object for which you want to convert the storage class is Archive. If the storage class of the object is Archive, you must restore the object before you can convert the storage class. 
                StorageClass storageClass = objectMetadata.getObjectStorageClass();
                System.out.println("storage type:" + storageClass);
                if (storageClass == StorageClass.Archive) {
                    // Restore the object. 
                    ossClient.restoreObject(bucketName, objectName);
    
                    // Wait until the object is restored. 
                    do {
                        Thread.sleep(1000);
                        objectMetadata = ossClient.getObjectMetadata(bucketName, objectName);
                    } while (!objectMetadata.isRestoreCompleted());
                }
    
                // Create a CopyObjectRequest object. 
                CopyObjectRequest request = new CopyObjectRequest(bucketName, objectName, bucketName, objectName) ;
    
                // Create an ObjectMetadata object. 
                objectMetadata = new ObjectMetadata();
    
                // Encapsulate the header. In this example, the storage class is converted to IA. 
                objectMetadata.setHeader("x-oss-storage-class", StorageClass.IA);
                request.setNewObjectMetadata(objectMetadata);
    
                // Convert the storage class of the object. 
                CopyObjectResult result = ossClient.copyObject(request);
    
            } catch (OSSException oe) {
                System.out.println("Caught an OSSException, which means your request made it to OSS, "
                        + "but was rejected with an error response for some reason.");
                System.out.println("Error Message:" + oe.getErrorMessage());
                System.out.println("Error Code:" + oe.getErrorCode());
                System.out.println("Request ID:" + oe.getRequestId());
                System.out.println("Host ID:" + oe.getHostId());
            } catch (ClientException ce) {
                System.out.println("Caught an ClientException, which means the client encountered "
                        + "a serious internal problem while trying to communicate with OSS, "
                        + "such as not being able to access the network.");
                System.out.println("Error Message:" + ce.getMessage());
            } finally {
                if (ossClient != null) {
                    ossClient.shutdown();
                }
            }
        }
    }

References

  • For more information about the complete sample code that is used to convert the storage class of an object, visit GitHub.
  • For more information about the API operation that you can call to convert the storage class of an object, see CopyObject.