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 call the CopyObject operation to convert the storage class of an object.

Note For more information about storage classes, see Overview and Convert storage classes in the Developer Guide.

Covert a Standard or an IA object to an Archive object

The following code provides an example on how to convert a Standard or an IA object to an Archive object:

<?php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}

use OSS\OssClient;
use OSS\Core\OssException;

// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint. 
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name. 
$bucket= "<yourBucketName>";
// Specify the full path of the object. The full path of the object does not contain bucket names. Example: destfolder/exampleobject.txt. 
$object = "<yourObjectName>";

$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);

try {

    // Specify the storage class that you want to convert to. In this example, set the storage class to Archive. 
    $copyOptions = array(
        OssClient::OSS_HEADERS => array(            
            'x-oss-storage-class' => 'Archive',
            'x-oss-metadata-directive' => 'REPLACE',
        ),
    );
    
    $ossClient->copyObject($bucket, $object, $bucket, $object, $copyOptions);

} catch (OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}

print(__FUNCTION__ . ": OK" . "\n");

For more information about the naming conventions for buckets, see bucket. For more information about the naming conventions for objects, see .

Covert an Archive object to an IA or a Standard object

The following code provides an example on how to convert an Archive object to an IA or a Standard object:

<?php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}

use OSS\OssClient;
use OSS\Core\OssException;

// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint. 
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name. 
$bucket= "<yourBucketName>";
// Specify the full path of the object. The full path of the object does not contain bucket names. Example: destfolder/exampleobject.txt. 
$object = "<yourObjectName>";

$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);

try {

    // Restore the Archive object. 
    $ossClient->restoreObject($bucket, $object);
    
    // Wait until the object is restored. 
    $waitTime = 100;
    while ($waitTime > 0) {
        
        $meta = $ossClient->getObjectMeta($bucket, $object);
        
        if (array_key_exists("x-oss-restore", $meta) && 
            strcmp($meta["x-oss-restore"], "ongoing-request=\"true\"") == 0){
            print("In restore status, ". $meta["x-oss-restore"]. "\n");
            sleep(5);
        } else {
            break;
        }

        $waitTime -= 5;     
    }

    // Specify the storage class to which you want to convert. In this example, you can set the storage class to IA or Standard. 
    $copyOptions = array(
        OssClient::OSS_HEADERS => array(             
            'x-oss-storage-class' => 'IA',
            'x-oss-metadata-directive' => 'REPLACE',
        ),
    );
    $ossClient->copyObject($bucket, $object, $bucket, $object, $copyOptions);

} catch (OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}

print(__FUNCTION__ . ": OK" . "\n");