OSS supports five storage classes—Standard, Infrequent Access (IA), Archive, Cold Archive, and Deep Cold Archive—covering data access scenarios from hot data to cold data. Because OSS objects are immutable after creation, converting a storage class requires creating a new object via a copy operation with the target class specified.
This topic shows how to convert an object's storage class using OSS SDK for Python 2.0.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket with objects to convert
The following RAM permissions granted to your identity:
oss:GetObjectoss:PutObjectoss:RestoreObject
For instructions on granting permissions, see Grant custom permissions to a RAM user.
Usage notes
The sample code uses the region ID
cn-hangzhou(China (Hangzhou)). By default, a public endpoint is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For a full list of regions and endpoints, see Regions and endpoints.
Choose a method
OSS SDK for Python 2.0 provides two methods for storage class conversion:
| Method | Recommended use case |
|---|---|
CopyObject | Small objects; does not handle multipart copy automatically |
Copier.Copy | Large objects; automatically selects between simple copy and multipart copy based on the request parameters |
Use Copier.Copy when you are unsure of the object size or want automatic multipart handling.
Convert the storage class using CopyObject
CopyObject converts an object's storage class by copying the source object to a destination with storage_class set to the target class.
The following example converts an object from Standard to Archive.
import argparse
import alibabacloud_oss_v2 as oss
parser = argparse.ArgumentParser(description="copy object sample")
# Required arguments
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the destination bucket.', required=True)
parser.add_argument('--key', help='The name of the destination object.', required=True)
parser.add_argument('--source_key', help='The name of the source object.', required=True)
parser.add_argument('--source_bucket', help='The name of the source bucket.', required=True)
# Optional arguments
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
def main():
args = parser.parse_args()
# Load credentials from environment variables.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Load the default SDK configuration and set the credentials provider.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Create an OSS client.
client = oss.Client(cfg)
# Copy the object with the target storage class.
result = client.copy_object(oss.CopyObjectRequest(
bucket=args.bucket,
key=args.key,
source_key=args.source_key,
source_bucket=args.source_bucket,
storage_class="Archive",
))
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' version id: {result.version_id},'
f' hash crc64: {result.hash_crc64},'
f' source version id: {result.source_version_id},'
f' server side encryption: {result.server_side_encryption},'
f' server side data encryption: {result.server_side_data_encryption},'
f' last modified: {result.last_modified},'
f' etag: {result.etag},'
)
if __name__ == "__main__":
main()Convert the storage class using Copier
Copier.Copy wraps both simple copy and multipart copy and automatically selects the appropriate method based on the request parameters. Use this method for large objects to avoid manual multipart handling.
The following example converts an object from Standard to Archive.
import argparse
import alibabacloud_oss_v2 as oss
parser = argparse.ArgumentParser(description="copier sample")
# Required arguments
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the destination bucket.', required=True)
parser.add_argument('--key', help='The name of the destination object.', required=True)
parser.add_argument('--source_key', help='The name of the source object.', required=True)
parser.add_argument('--source_bucket', help='The name of the source bucket.', required=True)
# Optional arguments
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
def main():
args = parser.parse_args()
# Load credentials from environment variables.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Load the default SDK configuration and set the credentials provider.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Create an OSS client.
client = oss.Client(cfg)
# Create a Copier instance.
copier = client.copier()
# Copy the object with the target storage class.
# Copier automatically selects simple copy or multipart copy based on the request parameters.
result = copier.copy(
oss.CopyObjectRequest(
bucket=args.bucket,
key=args.key,
source_bucket=args.source_bucket,
source_key=args.source_key,
storage_class="Archive",
)
)
print(vars(result))
if __name__ == "__main__":
main()