All Products
Search
Document Center

Object Storage Service:Convert the storage classes of objects

Last Updated:Sep 11, 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 Initialization.

  • To convert the storage class of an object, you must have the oss:GetObject, oss:PutObject, and oss:RestoreObject permissions. For more information, see Common examples of RAM policies.

Examples

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

Important
  • If an object is overwritten or deleted within less than 60 days after its storage class is converted to Archive, you are charged for the remainder of the minimum storage duration (60 days) for Archive.

  • If an object is overwritten or deleted within less than 180 days after its storage class is converted to Cold Archive, you are charged for the remainder of the minimum storage duration (180 days) for Cold Archive.

For more information, see Storage fees.

The following code provides an example on how to convert the storage class of an object from Standard or Infrequent Access (IA) to Archive or Cold Archive:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import os
# 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. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())

# In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')
# Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
# Make sure that the storage class of the object is Standard or IA. 
object_name = 'exampledir/exampleobject.txt'

# Convert the storage class of the object to Archive by adding a header that specifies the Archive storage class in the request. 
headers = {'x-oss-storage-class': oss2.BUCKET_STORAGE_CLASS_ARCHIVE}
# Convert the storage class of the object to Cold Archive by adding a header that specifies the Cold Archive storage class in the request. 
# headers = {'x-oss-storage-class': oss2.BUCKET_STORAGE_CLASS_COLD_ARCHIVE}

# Change the storage class of the object. 
bucket.copy_object(bucket.bucket_name, object_name, object_name, headers)                    

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

Important

If an object is overwritten or deleted within less than 30 days after its storage class is converted to IA, you are charged for the remainder of the minimum storage duration (30 days) for IA. For more information, see Storage fees.

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

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import os
import time
# 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. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())

# In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')
# Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
# Make sure that the storage class of the object is Archive. 
object_name = 'exampledir/exampleobject.txt'

# Query the metadata of the object. 
meta = bucket.head_object(object_name)

# Restore the object. The time required to restore an object vary with the size of the object. 
if meta.resp.headers['x-oss-storage-class'] == oss2.BUCKET_STORAGE_CLASS_ARCHIVE:
    bucket.restore_object(object_name)
    while True:
        meta = bucket.head_object(object_name)
        if meta.resp.headers['x-oss-restore'] == 'ongoing-request="true"':
            time.sleep(5)
        else:
            break

# Convert the storage class of the object to Standard by adding a header that specifies the Standard storage class in the request. 
headers = {'x-oss-storage-class': oss2.BUCKET_STORAGE_CLASS_STANDARD}
# Convert the storage class of the object to IA by adding a header that specifies the IA storage class in the request. 
# headers = {'x-oss-storage-class': oss2.BUCKET_STORAGE_CLASS_IA}

# Change the storage class of the object. 
bucket.copy_object(bucket.bucket_name, object_name, object_name, headers)

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

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

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import os
import time
# 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. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())

# In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')
# Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
# Make sure that the storage class of the object is Cold Archive. 
object_name = 'exampledir/exampleobject.txt'

# Query the metadata of the object. 
meta = bucket.head_object(object_name)

# Restore the object. 
if meta.resp.headers['x-oss-storage-class'] == oss2.BUCKET_STORAGE_CLASS_COLD_ARCHIVE:
    # Specify the restoration priority. RESTORE_TIER_EXPEDITED indicates that the object is restored within 1 hour. 
    job_parameters = RestoreJobParameters(RESTORE_TIER_EXPEDITED)
    # Specify the duration (in days) within which the restored object remains in the restored state. 
    restore_config = oss2.models.RestoreConfiguration(days=5, job_parameters=job_parameters)
    # bucket.restore_object(object_name, input=restore_config)
    bucket.restore_object(object_name)
    while True:
        meta = bucket.head_object(object_name)
        if meta.resp.headers['x-oss-restore'] == 'ongoing-request="true"':
            time.sleep(5)
        else:
            break
# Convert the storage class of the object to Standard by adding a header that specifies the Standard storage class in the request. 
headers = {'x-oss-storage-class': oss2.BUCKET_STORAGE_CLASS_STANDARD}
# Convert the storage class of the object to IA by adding a header that specifies the IA storage class in the request. 
# headers = {'x-oss-storage-class': oss2.BUCKET_STORAGE_CLASS_IA}

# Change the storage class of the object. 
bucket.copy_object(bucket.bucket_name, object_name, object_name, headers)

References

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