All Products
Search
Document Center

Object Storage Service:Access tracking (PHP SDK V2)

Last Updated:Mar 20, 2026

Use Object Storage Service (OSS) SDK for PHP 2.0 to enable or query access tracking for a bucket.

Prerequisites

Before you begin, make sure that you have:

  • Installed OSS SDK for PHP 2.0 and its dependencies via Composer

  • An OSS bucket

  • Your AccessKey ID and AccessKey secret stored as environment variables

Usage notes

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

Examples

Enable access tracking

<?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(fn($key) => "$key:", array_keys($optsdesc));
$options  = getopt("", $longopts);

foreach ($optsdesc as $key => $value) {
    if ($value['required'] === true && empty($options[$key])) {
        echo "Error: the following arguments are required: --$key, " . $value['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);

// Set access tracking status to ENABLED.
$accessMonitorConfiguration = new Oss\Models\AccessMonitorConfiguration(
    status: Oss\Models\AccessMonitorStatusType::ENABLED
);

$request = new Oss\Models\PutBucketAccessMonitorRequest(
    bucket: $bucket,
    accessMonitorConfiguration: $accessMonitorConfiguration
);

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

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

Run the example:

php enable_access_tracking.php --region cn-hangzhou --bucket <your-bucket-name>

Query the access tracking status

<?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(fn($key) => "$key:", array_keys($optsdesc));
$options  = getopt("", $longopts);

foreach ($optsdesc as $key => $value) {
    if ($value['required'] === true && empty($options[$key])) {
        echo "Error: the following arguments are required: --$key, " . $value['help'];
        exit(1);
    }
}

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

$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\GetBucketAccessMonitorRequest(bucket: $bucket);

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

printf(
    'status code: %s' . PHP_EOL .
    'request ID: %s' . PHP_EOL .
    'access tracking status: %s' . PHP_EOL,
    $result->statusCode,
    $result->requestId,
    $result->accessMonitorConfiguration->status
);

Run the example:

php get_access_tracking.php --region cn-hangzhou --bucket <your-bucket-name>

The response fields are:

FieldDescription
$result->statusCodeHTTP status code returned by OSS
$result->requestIdUnique identifier of the request
$result->accessMonitorConfiguration->statusCurrent access tracking status of the bucket

References