Manage versioning (PHP SDK V1)
Versioning applies to all objects in a bucket. Versioning lets you restore an object to any of its previous versions if the object is accidentally overwritten or deleted.
A bucket has three versioning states: unversioned (default), enabled, and suspended. For more information, see Versioning.
After you enable versioning for a bucket, you cannot return the bucket to the unversioned state. However, you can suspend versioning.
Usage notes
-
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.
-
This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Create an OssClient.
-
To set the versioning state of a bucket, you must have the
oss:PutBucketVersioningpermission. To retrieve the versioning state of a bucket, you must have theoss:GetBucketVersioningpermission. For more information, see Attach a custom access policy to a RAM user.
Set the versioning state of a bucket
The following code shows how to set the versioning state of a bucket:
<?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 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";
$bucket= "<yourBucketName>";
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);
try {
// Set the versioning state of the bucket to Enabled or Suspended.
$ossClient->putBucketVersioning($bucket, "Enabled");
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
For more information, see PutBucketVersioning.
Get the versioning state of a bucket
The following code shows how to retrieve the versioning state of a bucket:
<?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 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";
$bucket= "<yourBucketName>";
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);
try {
// Get the versioning state of the bucket.
$status = $ossClient->getBucketVersioning($bucket);
print($status);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
For more information about how to retrieve the versioning state of a bucket, see GetBucketVersioning.
List the versions of all objects in a bucket
The following code shows how to list the versions of all objects, including delete markers, in a specified bucket:
<?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 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";
$bucket= "<yourBucketName>";
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);
try {
$maxKey = 100;
$nextKeyMarker = '';
$nextVersionIdMarker = '';
while (true) {
$options = array(
'delimiter' => '',
'key-marker' => $nextKeyMarker,
'max-keys' => $maxKey,
'version-id-marker' => $nextVersionIdMarker,
);
$result = $ossClient->listObjectVersions($bucket, $options);
$nextKeyMarker = $result->getNextKeyMarker();
$nextVersionIdMarker = $result->getNextVersionIdMarker();
$objectList = $result->getObjectVersionList();
$deleteMarkerList = $result->getDeleteMarkerList();
// Print the version information of objects.
if (!empty($objectList)) {
print("objectList:\n");
foreach ($objectList as $objectInfo) {
print($objectInfo->getKey() . ",");
print($objectInfo->getVersionId() . ",");
print($objectInfo->getLastModified() . ",");
print($objectInfo->getETag() . ",");
print($objectInfo->getSize() . ",");
print($objectInfo->getIsLatest() . "\n");
}
}
// Print the version information of delete markers.
if (!empty($deleteMarkerList)) {
print("deleteMarkerList: \n");
foreach ($deleteMarkerList as $deleteMarkerInfo) {
print($deleteMarkerInfo->getKey() . ",");
print($deleteMarkerInfo->getVersionId() . ",");
print($deleteMarkerInfo->getLastModified() . ",");
print($deleteMarkerInfo->getIsLatest() . "\n");
}
}
if ($result->getIsTruncated() !== "true") {
break;
}
}
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
For more information, see ListObjectVersions (GetBucketVersions).