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.
Usage notes
- In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint For more information about the regions and endpoints supported by OSS, 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 STS, see Create an OSSClient instance.
- To convert the storage class of an object, you must have the
oss:GetObject
andoss:PutObject
permissions. To convert the storage class of an Archive or a Cold Archive object to Standard or IA, you must also have theoss:RestoreObject
permission. For more information, see Attach a custom policy to a RAM user.
Convert 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;
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console.
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// 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. The full path of the object cannot contain the bucket name. Example: destfolder/exampleobject.txt.
$object = "<yourObjectName>";
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
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 the naming conventions for buckets, see Bucket. For more information about the naming conventions for objects, see .
Convert 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;
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console.
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// 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. The full path of the object cannot contain the bucket name. 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 the 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");