OSS offers multiple storage classes, such as Standard, Infrequent Access (IA), Archive, Cold Archive, and Deep Cold Archive. These storage classes accommodate all data storage scenarios from hot to cold. This topic describes how to use the CopyObject operation to change the storage class of an object.
Notes
For more information about storage classes, see Storage classes. For more information about how to change storage classes, see Change storage classes.
In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported 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 change the storage class of an object, you must have the
oss:GetObjectandoss:PutObjectpermissions. To change the storage class of an Archive or Cold Archive object to Standard or Infrequent Access, you must also have theoss:RestoreObjectpermission. For more information, see Grant custom permissions to a RAM user.
Change the storage class from Standard or Infrequent Access to Archive
The following code shows how to change the storage class of an object from Standard or Infrequent Access 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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
$provider = new EnvironmentVariableCredentialsProvider();
// 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. Do not include the bucket name. Example: destfolder/exampleobject.txt.
$object = "<yourObjectName>";
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);
try {
// Specify the storage class for the object after the change. For 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 and object naming rules, see Buckets and Objects.
Change the storage class from Archive to Infrequent Access or Standard
The following code shows how to change the storage class of an object from Archive to Infrequent Access or 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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
$provider = new EnvironmentVariableCredentialsProvider();
// 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. Do not include the bucket name. Example: destfolder/exampleobject.txt.
$object = "<yourObjectName>";
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
);
$ossClient = new OssClient($config);
try {
// Restore the archived object.
$ossClient->restoreObject($bucket, $object);
// 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 for the object after the change. For example, set the storage class to Infrequent Access (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");