All Products
Search
Document Center

Object Storage Service:Convert the storage classes of objects

Last Updated:Nov 24, 2023

Object Storage Service (OSS) provides the following storage classes to cover a variety of 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.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.

  • To convert the storage class of an object, you must have the oss:GetObject, oss:PutObject, and oss:RestoreObject permissions. For more information, see Attach a custom policy to a RAM user.

Examples

Important

If you convert the storage class of an IA, Archive, Cold Archive, or Deep Cold Archive object or delete the object before the minimum storage duration elapses, you are charged for the storage usage of the object that is stored for less than the minimum storage duration. For more information, see How am I charged for objects whose storage duration is less than the minimum storage duration?

Convert the storage class of an object from Standard or IA to Archive, Cold Archive, or Deep Cold Archive

The following sample code provides an example on how to convert the storage class of an object from Standard or IA to Archive, Cold Archive, or Deep Cold Archive:

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
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 your actual endpoint. 
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // In this example, a bucket and a Standard or IA object are created. 
        // Specify the name of the bucket. Example: examplebucket. 
        String bucketName = "examplebucket";
        // Specify the full path of the object. Do not include the bucket name in the full path. Example: exampleobject.txt. 
        String objectName = "exampleobject.txt";

        // Create an OSSClient instance. 
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);

        try {
            // Create a CopyObjectRequest object. 
            CopyObjectRequest request = new CopyObjectRequest(bucketName, objectName, bucketName, objectName) ;

            // Create an ObjectMetadata object. 
            ObjectMetadata objectMetadata = new ObjectMetadata();

            // Convert the storage class of the object to Archive. 
            objectMetadata.setHeader("x-oss-storage-class", StorageClass.Archive);
            // Convert the storage class of the object to Cold Archive. 
            // objectMetadata.setHeader("x-oss-storage-class", StorageClass.ColdArchive);
            // Convert the storage class of the object to Deep Cold Archive. 
            // objectMetadata.setHeader("x-oss-storage-class", StorageClass.DeepColdArchive);
            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();
            }
        }
    }
}

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

The following sample code provides an example on how to convert the storage class of an object from Archive to Standard or IA:

package com.aliyun.oss.demo;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
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 your actual endpoint. 
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // In this example, a bucket and an Archive object are created. 
        // Specify the name of the bucket. Example: examplebucket. 
        String bucketName = "examplebucket";
        // Specify the full path of the object. Do not include the bucket name in the full path. Example: exampleobject.txt. 
        String objectName = "exampleobject.txt";

        // Create an OSSClient instance. 
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);

        try {
            // Query object metadata. 
            ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucketName, objectName);

            // Check whether the object whose storage class you want to convert is an Archive object. If the object is an Archive object, 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();

            // Convert the storage class of the object to IA. 
            objectMetadata.setHeader("x-oss-storage-class", StorageClass.IA);
            // Convert the storage class of the object to Standard. 
            // objectMetadata.setHeader("x-oss-storage-class", StorageClass.Standard);
            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.