All Products
Search
Document Center

Object Storage Service:Real-time access of Archive objects (OSS SDK for PHP V2)

Last Updated:Mar 20, 2026

Archive objects in Object Storage Service (OSS) are not directly readable — accessing them requires a restore operation first. Enabling Archive direct read removes this restriction, letting you read Archive objects immediately via standard GET requests without restoring them first.

This topic shows how to use OSS SDK for PHP V2 to enable Archive direct read and check its status for a bucket.

Usage notes

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

Prerequisites

Before you begin, ensure that you have:

  • An OSS bucket with Archive objects

  • Your AccessKey ID and AccessKey secret stored as environment variables

Enable Archive direct read for a bucket

Call putBucketArchiveDirectRead with enabled: true to enable Archive direct read on a 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";
        exit(1);
    }
}

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

// 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);

// Enable Archive direct read for the bucket.
$request = new Oss\Models\PutBucketArchiveDirectReadRequest(
    bucket: $bucket,
    archiveDirectReadConfiguration: new Oss\Models\ArchiveDirectReadConfiguration(
        enabled: true
    )
);

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

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

Run the script with the following arguments:

ArgumentRequiredDescription
--regionYesRegion where the bucket is located, for example cn-hangzhou
--bucketYesName of the bucket
--endpointNoCustom endpoint for accessing OSS

Check Archive direct read status for a bucket

Call getBucketArchiveDirectRead to check whether Archive direct read is enabled on a 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";
        exit(1);
    }
}

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

// 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);

// Check Archive direct read status for the bucket.
$request = new Oss\Models\GetBucketArchiveDirectReadRequest(
    bucket: $bucket
);

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

printf(
    'status code:' . $result->statusCode . PHP_EOL .
    'request id:' . $result->requestId . PHP_EOL .
    'archive direct read config:' . var_export($result->archiveDirectReadConfiguration->enabled, true) . PHP_EOL
);

The archiveDirectReadConfiguration->enabled field in the response returns a boolean indicating whether Archive direct read is currently enabled.

References