All Products
Search
Document Center

Object Storage Service:Convert the storage class of an object in PHP

Last Updated:Mar 08, 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. This topic describes how to call the CopyObject operation to convert the storage class of an object.

Usage notes

  • For more information about storage classes, see Overview. For more information about how to convert the storage class of an object, see Convert storage classes.

  • 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, 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 Create an OSSClient instance.

  • To convert the storage class of an object, you must have the oss:GetObject and oss:PutObject permissions. To convert the storage class of an object from Archive or Cold Archive to Standard or IA, you must have the oss:RestoreObject permission. For more information, see Attach a custom policy to a RAM user.

Convert the storage class of an object from Standard or IA to Archive

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

<?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\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;

// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
$provider = new EnvironmentVariableCredentialsProvider();
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the name of the bucket. 
$bucket= "<yourBucketName>";
// Specify the full path of the object. Do not include the bucket name in the full path. Example: destfolder/exampleobject.txt. 
$object = "<yourObjectName>";

$config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
    );
    $ossClient = new OssClient($config);

try {

    // Specify the storage class to which you want to convert the object. 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 bucket naming, see Bucket. For more information about object naming, see Object.

Convert the storage class of an object from Archive to or IA to Standard

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

<?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\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;

// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
$provider = new EnvironmentVariableCredentialsProvider();
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the name of the bucket. 
$bucket= "<yourBucketName>";
// Specify the full path of the object. Do not include the bucket name in the full path. Example: destfolder/exampleobject.txt. 
$object = "<yourObjectName>";

$config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
    );
    $ossClient = new OssClient($config);

try {

    // Restore the Archive object. 
    $ossClient->restoreObject($bucket, $object);
    
    // Specify the time to wait for the object to be 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 the Archive object. In this example, 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");