All Products
Search
Document Center

Object Storage Service:Manage versioning (OSS SDK for PHP V2)

Last Updated:Mar 20, 2026

Versioning applies to all objects in a bucket. When enabled, OSS preserves every version of an object, so you can recover from accidental overwrites or deletions by restoring any previous version.

Prerequisites

Before you begin, ensure that you have:

  • An OSS bucket

  • The oss:PutBucketVersioning permission to enable or suspend versioning

  • The oss:GetBucketVersioning permission to retrieve the versioning status

For RAM user permission setup, see Grant custom permissions to a RAM user.

Usage notes

  • The sample code uses the China (Hangzhou) region (cn-hangzhou) and the public endpoint. To access OSS from other Alibaba Cloud services in the same region, use the internal endpoint. For supported regions and endpoints, see OSS regions and endpoints.

  • Credentials are loaded from environment variables (Access Key ID and Access Key Secret) via EnvironmentVariableCredentialsProvider. Do not hardcode credentials in source code.

Enable versioning

The following code sets the versioning status of a bucket to Enabled.

<?php

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

use AlibabaCloud\Oss\V2 as Oss;

$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],
];

$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])) {
        $help = $value['help'];
        echo "Error: the following arguments are required: --$key, $help" . PHP_EOL;
        exit(1);
    }
}

$region = $options["region"];
$bucket = $options["bucket"];

// Load credentials from the Access Key ID and Access Key Secret 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);

$request = new Oss\Models\PutBucketVersioningRequest(
    bucket: $bucket,
    versioningConfiguration: new Oss\Models\VersioningConfiguration(
        status: 'Enabled'
    )
);

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

printf(
    'status code: ' . $result->statusCode . PHP_EOL .
    'request ID: ' . $result->requestId . PHP_EOL
);

Get versioning status

The following code retrieves the versioning status of a bucket.

The versioningConfiguration->status field returns one of three values:

StatusMeaning
EnabledVersioning is active; all objects receive unique version IDs.
SuspendedVersioning is suspended; new objects receive a null version ID.
*(empty string)*Versioning has never been enabled on this bucket.
<?php

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

use AlibabaCloud\Oss\V2 as Oss;

$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],
];

$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])) {
        $help = $value['help'];
        echo "Error: the following arguments are required: --$key, $help" . PHP_EOL;
        exit(1);
    }
}

$region = $options["region"];
$bucket = $options["bucket"];

// Load credentials from the Access Key ID and Access Key Secret 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);

$request = new Oss\Models\GetBucketVersioningRequest(
    bucket: $bucket
);

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

printf(
    'status code: ' . $result->statusCode . PHP_EOL .
    'request ID: ' . $result->requestId . PHP_EOL .
    'versioning status: ' . $result->versioningConfiguration->status . PHP_EOL
);