Object Storage Service (OSS) provides multiple storage classes, such as Standard, Infrequent Access (IA), Archive, Cold Archive, and Deep Cold Archive, to meet various storage requirements for both hot and cold data. In OSS, the content of an object cannot be modified after the object is created. Therefore, you cannot change the storage class of an object by modifying the original object. Instead, you must create a new object. This topic describes how to convert the storage class of an object by copying the source object using the CopyObject method in Python SDK V2.
Precautions
The sample code in this topic uses the China (Hangzhou) region, which has the region ID
cn-hangzhou, as an example. By default, a public endpoint is used. If you access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about the regions and endpoints that OSS supports, see Regions and endpoints.To convert the storage class of an object, you must have the
oss:GetObject,oss:PutObject, andoss:RestoreObjectpermissions. For more information, see Grant custom permissions to a RAM user.
Sample code
Use simple copy (CopyObject) to convert the storage class of an object
You can use the simple copy method CopyObject to convert the storage class of an object from Standard to Archive.
import argparse
import alibabacloud_oss_v2 as oss
# Create a command-line argument parser.
parser = argparse.ArgumentParser(description="copy object sample")
# Add the --region command-line argument, which specifies the region where the bucket is located. This argument is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Add the --bucket command-line argument, which specifies the name of the destination bucket. This argument is required.
parser.add_argument('--bucket', help='The name of the destination bucket.', required=True)
# Add the --endpoint command-line argument, which specifies the domain name that other services can use to access OSS. This argument is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Add the --key command-line argument, which specifies the name of the destination object. This argument is required.
parser.add_argument('--key', help='The name of the destination object.', required=True)
# Add the --source_key command-line argument, which specifies the name of the source object. This argument is required.
parser.add_argument('--source_key', help='The name of the source object.', required=True)
# Add the --source_bucket command-line argument, which specifies the name of the source bucket. This argument is required.
parser.add_argument('--source_bucket', help='The name of the source bucket.', required=True)
def main():
# Parse command-line arguments.
args = parser.parse_args()
# Load credentials from environment variables for identity verification.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Load the default configurations of the SDK and set the credentials provider.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
# Set the region in the configuration.
cfg.region = args.region
# If the endpoint parameter is provided, set the endpoint in the configuration.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Create an OSS client based on the configurations.
client = oss.Client(cfg)
# Execute a request to copy the object.
result = client.copy_object(oss.CopyObjectRequest(
bucket=args.bucket, # Specify the name of the destination bucket.
key=args.key, # Specify the key of the destination object.
source_key=args.source_key, # Specify the key of the source object.
source_bucket=args.source_bucket, # Specify the name of the source bucket.
storage_class= "Archive", # Convert the storage class to Archive Storage.
))
# Print the result of the copy operation.
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},'
)
# Call the main function when the script is directly run.
if __name__ == "__main__":
main() # The entry point of the script. The main function is called when the file is directly run.Using Copier to convert file types
You can use the Copier.Copy method in the copy manager of Python SDK V2 to convert the storage class of an object. This method combines simple copy and multipart copy operations. It automatically selects the appropriate operation to convert the storage class of the object based on the copy request parameters.
The following code provides an example of how to use the Copier.Copy method to convert the storage class of an object from Standard to Archive.
import argparse
import alibabacloud_oss_v2 as oss
# Create a command-line argument parser.
parser = argparse.ArgumentParser(description="copier sample")
# Add the --region command-line argument (required) to specify the region where the bucket is located.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Add the --bucket command-line argument (required) to specify the name of the destination bucket.
parser.add_argument('--bucket', help='The name of the destination bucket.', required=True)
# Add the --endpoint command-line argument (optional) to specify the endpoint of OSS.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Add the --key command-line argument (required) to specify the name of the destination object.
parser.add_argument('--key', help='The name of the destination object.', required=True)
# Add the --source_key command-line argument (required) to specify the name of the source object.
parser.add_argument('--source_key', help='The name of the source object.', required=True)
# Add the --source_bucket command-line argument (required) to specify the name of the source bucket.
parser.add_argument('--source_bucket', help='The name of the source bucket.', required=True)
def main():
# Parse command-line arguments.
args = parser.parse_args()
# Load credentials from environment variables.
# Use EnvironmentVariableCredentialsProvider to read the AccessKey ID and AccessKey secret from environment variables.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the default configurations of the SDK.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider # Set the credentials provider.
cfg.region = args.region # Set the region where the bucket is located.
if args.endpoint is not None:
cfg.endpoint = args.endpoint # If an endpoint is provided, set a custom endpoint.
# Create an OSS client.
client = oss.Client(cfg)
# Create a Copier instance and perform the copy operation.
copier = client.copier()
# Perform the copy operation.
result = copier.copy(
oss.CopyObjectRequest(
bucket=args.bucket, # The name of the destination bucket.
key=args.key, # The name of the destination object.
source_bucket=args.source_bucket, # The name of the source bucket.
source_key=args.source_key, # The name of the source object.
storage_class="Archive", # Convert the storage class to Archive Storage.
)
)
# Print the copy result.
# Use vars(result) to convert the result object to a dictionary and print the dictionary.
print(vars(result))
if __name__ == "__main__":
main()