Todos os produtos
Search
Central de documentação

Object Storage Service:Acesso em tempo real a objetos Archive usando o OSS SDK for PHP 2.0

Última atualização: Sep 22, 2026

Este tópico descreve como usar o SDK do Object Storage Service (OSS) para PHP V2 para ativar ou desativar o recurso de acesso em tempo real a objetos Archive em um bucket.

Observações de uso

  • O código de exemplo neste tópico usa a região China (Hangzhou) (cn-hangzhou) como referência. Por padrão, o SDK utiliza um endpoint público. Ao acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região, use um endpoint interno. Para mais informações sobre os mapeamentos entre regiões e endpoints do OSS, consulte Regions and endpoints.

Código de exemplo

Ativar o acesso em tempo real a objetos Archive para um bucket

Use o código a seguir para ativar o acesso em tempo real a objetos Archive em um bucket.

<?php

// Import the autoloader file to load dependencies.
require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

// Define the descriptions for command-line arguments.
$optsdesc = [
    "region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region is required. This is the region where the bucket is located.
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint is optional. This is the domain name that other services can use to access OSS.
    "bucket" => ['help' => 'The name of the bucket', 'required' => True], // The bucket name is required.
];

// Generate a list of long options to parse command-line arguments.
$longopts = \array_map(function ($key) {
    return "$key:"; // A colon (:) is added to each parameter to indicate that a value is required.
}, array_keys($optsdesc));

// Parse the command-line arguments.
$options = getopt("", $longopts);

// Check whether required arguments are missing.
foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        $help = $value['help'];
        echo "Error: the following arguments are required: --$key, $help"; // Prompt the user that a required argument is missing.
        exit(1);
    }
}

// Obtain the values of the command-line arguments.
$region = $options["region"]; // The region where the bucket is located.
$bucket = $options["bucket"]; // The name of the bucket.

// Load credentials, including the AccessKey ID and AccessKey secret, from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();

// Set the credentials provider.
$cfg->setCredentialsProvider($credentialsProvider);

// Set the region.
$cfg->setRegion($region);

// If an endpoint is provided, set the endpoint.
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]);
}

// Create an OSS client instance.
$client = new Oss\Client($cfg);

// Create a request object to enable the real-time access of Archive objects feature for the bucket.
$request = new Oss\Models\PutBucketArchiveDirectReadRequest(
    bucket: $bucket, // The name of the bucket.
    archiveDirectReadConfiguration: new Oss\Models\ArchiveDirectReadConfiguration(
        enabled: true // Enable the real-time access of Archive objects feature.
    )
);

// Call the putBucketArchiveDirectRead method to enable the real-time access of Archive objects feature for the bucket.
$result = $client->putBucketArchiveDirectRead($request);

// Print the result.
printf(
    'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code of the response.
    'request id:' . $result->requestId               // The unique ID of the request.
);

Consultar se o acesso em tempo real a objetos Archive está ativado para um bucket

Use o código a seguir para consultar se o acesso em tempo real a objetos Archive está ativado em um bucket.

<?php

// Import the autoloader file to load dependencies.
require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

// Define the descriptions for command-line arguments.
$optsdesc = [
    "region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region is required. This is the region where the bucket is located.
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint is optional. This is the domain name that other services can use to access OSS.
    "bucket" => ['help' => 'The name of the bucket', 'required' => True], // The bucket name is required.
];

// Generate a list of long options to parse command-line arguments.
$longopts = \array_map(function ($key) {
    return "$key:"; // A colon (:) is added to each parameter to indicate that a value is required.
}, array_keys($optsdesc));

// Parse the command-line arguments.
$options = getopt("", $longopts);

// Check whether required arguments are missing.
foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        $help = $value['help'];
        echo "Error: the following arguments are required: --$key, $help"; // Prompt the user that a required argument is missing.
        exit(1);
    }
}

// Obtain the values of the command-line arguments.
$region = $options["region"]; // The region where the bucket is located.
$bucket = $options["bucket"]; // The name of the bucket.

// Load credentials, including the AccessKey ID and AccessKey secret, from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();

// Set the credentials provider.
$cfg->setCredentialsProvider($credentialsProvider);

// Set the region.
$cfg->setRegion($region);

// If an endpoint is provided, set the endpoint.
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]);
}

// Create an OSS client instance.
$client = new Oss\Client($cfg);

// Create a request object to obtain the configurations of the real-time access of Archive objects feature for the bucket.
$request = new Oss\Models\GetBucketArchiveDirectReadRequest(
    bucket: $bucket // The name of the bucket.
);

// Call the getBucketArchiveDirectRead method to obtain the configurations of the real-time access of Archive objects feature for the bucket.
$result = $client->getBucketArchiveDirectRead($request);

// Print the result.
printf(
    'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code of the response.
    'request id:' . $result->requestId . PHP_EOL .   // The unique ID of the request.
    'archive direct read config:' . var_export($result->archiveDirectReadConfiguration->enabled, true) . PHP_EOL // The status that indicates whether the real-time access of Archive objects feature is enabled.
);

Referências

  • Para o código de exemplo completo para ativar o acesso em tempo real a objetos Archive em um bucket, consulte GitHub Sample.

  • Para mais informações sobre a operação de API para ativar o acesso em tempo real a objetos Archive em um bucket, consulte PutBucketArchiveDirectRead.

  • Para mais informações sobre a operação de API para consultar se o acesso em tempo real a objetos Archive está ativado em um bucket, consulte GetBucketArchiveDirectRead.