Tous les produits
Search
Centre de documentation

Object Storage Service:Interroger l'utilisation du stockage d'un bucket à l'aide du SDK OSS pour PHP 2.0

Dernière mise à jour :Sep 24, 2026

Cette rubrique explique comment interroger l'utilisation du stockage d'un bucket Object Storage Service (OSS) spécifique, ainsi que le nombre et l'utilisation du stockage des objets appartenant à différentes classes de stockage au sein du bucket.

Notes d'utilisation

  • L'exemple de code de cette rubrique utilise l'ID de région cn-hangzhou de la région Chine (Hangzhou). Par défaut, un endpoint public permet d'accéder aux ressources d'un bucket. Pour accéder aux ressources du bucket depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un endpoint interne. Pour plus d'informations sur les régions et les endpoints OSS, consultez la section Régions et endpoints.

Exemples

L'exemple de code suivant montre comment interroger l'utilisation du stockage d'un bucket :

<?php

require_once __DIR__ . '/../vendor/autoload.php'; // Automaticically load objects and dependency libraries.

use AlibabaCloud\Oss\V2 as Oss;

// Specify command line parameters.
$optsdesc = [
    "region" => ['help' => The region in which the bucket is located.', 'required' => True], // (Required) Specify the region in which the bucket is located. Example: oss-cn-hangzhou. 
    "endpoint" => ['help' => The domain names that other services can use to access OSS.', 'required' => False], // (Optional) Specify the endpoint that can be used by other services to access OSS. 
    "bucket" => ['help' => The name of the bucket, 'required' => True], // (Required) Specify the name of the bucket. 
];
$longopts = \array_map(function ($key) {
    return "$key:"; // Add a colon (:) to the end of each parameter to indicate that a value is required.
}, array_keys($optsdesc));

// Parse command line parameters.
$options = getopt("", $longopts); 

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

// Obtain the values of the command line parameters.
$region = $options["region"]; // The region in which the bucket is located. 
$bucket = $options["bucket"]; // The name of the bucket. 

// Use environment variables to load the credential information (AccessKey ID and AccessKey secret).
$credentialsProvider=new Oss\Credentials\EnvironmentVariableCredentialsProvider(); // Obtain the credential information from the environment variables. 

// Use the default configurations of the SDK.
$cfg=Oss\Config::loadDefault(); // Load the default configurations of the SDK. 
$cfg->setCredentialsProvider($credentialsProvider); // Specify the credential provider. 
$cfg->setRegion($region); // Specify the region. 
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]); // Specify the endpoint if an endpoint is provided. 
}

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

// Create a request to obtain the statistics of the bucket.
$request=new Oss\Models\GetBucketStatRequest($bucket); // Create a request to obtain the statistics of the bucket. 

// Use the getBucketStat method to obtain the statistics of the bucket.
$result = $client->getBucketStat($request); // Use the getBucketStat method to obtain the statistics of the bucket. 

// Print the returned result.
printf(
    'status code:' . $result-> statusCode. PHP_EOL . // The HTTP response status code. 
    'request id:' . $result-> requestId. PHP_EOL . // The unique identifier of the request. 
    'result:' . var_export($result, true) // The returned result, which contains the statistics of the bucket. 
);

Références

  • Pour consulter l'exemple de code complet permettant d'interroger les informations d'un bucket, rendez-vous sur GitHub.

  • Pour plus d'informations sur l'opération API permettant d'interroger les informations d'un bucket, consultez la section GetBucketStat.