All Products
Search
Document Center

Object Storage Service:Convert the storage class of an object

Last Updated:Nov 05, 2024

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.

Sample code

The following section provides examples on how to convert the storage class of an object.

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

    const OSS = require('ali-oss');
    
    const client = new OSS({
      // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
      region: 'yourregion',
      // Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
      accessKeyId: process.env.OSS_ACCESS_KEY_ID,
      accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
      authorizationV4: true,
      // Specify the name of the bucket. 
      bucket: 'yourbucketname'
    })
    const options = {
        headers:{'x-oss-storage-class':'Archive'}
    }
    client.copy('Objectname','Objectname',options).then((res) => {
        console.log(res);
    }).catch(err => {
        console.log(err)
    })

References

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