All Products
Search
Document Center

Object Storage Service:Convert the storage class of an object (PHP SDK V2)

Last Updated:Mar 20, 2026

OSS supports five storage classes for different access patterns: Standard, Infrequent Access (IA), Archive, Cold Archive, and Deep Cold Archive. Because OSS objects are immutable after creation, converting a storage class means copying the object with the target class set — the original object is replaced by the copy.

OSS SDK for PHP 2.0 provides two ways to do this: Copier.Copy and CopyObject. Both set the storageClass parameter on a CopyObjectRequest.

Choose a method

MethodWhen to use
Copier.Copy (recommended)Default choice. Automatically switches between simple copy and multipart copy based on request parameters.
CopyObjectSimple scenarios where you want direct control over the copy operation.

Prerequisites

Before you begin, make sure you have:

  • An OSS bucket with the object to convert

  • The following permissions: oss:GetObject, oss:PutObject, and oss:RestoreObject

For help granting permissions, see Grant custom permissions to a RAM user.

Storage class values

When setting storageClass in your request, use these values:

Storage classValue
StandardStandard
Infrequent AccessIA
ArchiveArchive

Convert a storage class

The examples below convert an object from Standard to Archive. The sample code uses the China (Hangzhou) region (cn-hangzhou) with a public endpoint. To access OSS from other Alibaba Cloud products in the same region, use an internal endpoint. For endpoint details, see Regions and endpoints.

Use Copier.Copy (recommended)

Copier.Copy combines simple copy and multipart copy and selects the appropriate operation automatically.

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

// Define command-line parameters.
$optsdesc = [
    "region"   => ['help' => 'The region in which the bucket is located.', 'required' => True],
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
    "bucket"   => ['help' => 'The name of the bucket.', 'required' => True],
    "key"      => ['help' => 'The name of the destination object.', 'required' => True],
    "src-key"  => ['help' => 'The name of the source object.', 'required' => True],
];

$longopts = \array_map(function ($key) { return "$key:"; }, array_keys($optsdesc));
$options = getopt("", $longopts);

foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        echo "Error: the following arguments are required: --$key, " . $value['help'] . PHP_EOL;
        exit(1);
    }
}

$region = $options["region"];
$bucket = $options["bucket"];
$key    = $options["key"];
$srcKey = $options["src-key"];

// Load credentials from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]);
}

$client = new Oss\Client($cfg);

// Create a Copier instance and submit the copy request with the target storage class.
$copier = $client->newCopier();

$copyRequest = new Oss\Models\CopyObjectRequest(
    bucket: $bucket,
    key: $key,
    sourceBucket: $bucket,
    sourceKey: $srcKey,
    storageClass: "Archive"  // Set the target storage class.
);

$result = $copier->copy(request: $copyRequest);

printf(
    'status code: ' . $result->statusCode . PHP_EOL .  // 200 indicates success.
    'request id: '  . $result->requestId  . PHP_EOL    // Use this ID to debug or trace the request.
);

Use CopyObject

CopyObject performs a direct copy operation. For large objects, use Copier.Copy instead, as it automatically handles multipart copy.

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

// Define command-line parameters.
$optsdesc = [
    "region"     => ['help' => 'The region in which the bucket is located.', 'required' => True],
    "endpoint"   => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
    "bucket"     => ['help' => 'The name of the destination bucket.', 'required' => True],
    "key"        => ['help' => 'The name of the destination object.', 'required' => True],
    "src-bucket" => ['help' => 'The name of the source bucket.', 'required' => False],
    "src-key"    => ['help' => 'The name of the source object.', 'required' => True],
];

$longopts = \array_map(function ($key) { return "$key:"; }, array_keys($optsdesc));
$options = getopt("", $longopts);

foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        echo "Error: the following arguments are required: --$key, " . $value['help'] . PHP_EOL;
        exit(1);
    }
}

$region = $options["region"];
$bucket = $options["bucket"];
$key    = $options["key"];
$srcKey = $options["src-key"];

// Load credentials from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]);
}

$client = new Oss\Client($cfg);

// Build the copy request with the target storage class.
$request = new Oss\Models\CopyObjectRequest(
    bucket: $bucket,
    key: $key,
    sourceKey: $srcKey,
    sourceBucket: $bucket,
    storageClass: 'Archive'  // Set the target storage class.
);

if (!empty($options["src-bucket"])) {
    $request->sourceBucket = $options["src-bucket"];
}
$request->sourceKey = $srcKey;

$result = $client->copyObject($request);

printf(
    'status code: ' . $result->statusCode . PHP_EOL .  // 200 indicates success.
    'request id: '  . $result->requestId  . PHP_EOL    // Use this ID to debug or trace the request.
);