Tous les produits
Search
Centre de documentation

Object Storage Service:Marquage de bucket à l'aide du SDK OSS pour PHP 2.0

Dernière mise à jour :Aug 18, 2026

Configurez, interrogez et supprimez les tags d'un bucket avec le SDK OSS pour PHP 2.0.

Remarque

L'exemple de code de cette rubrique utilise le point de terminaison public de la région Chine (Hangzhou). Pour accéder à OSS depuis d'autres services Alibaba Cloud de la même région, utilisez un point de terminaison interne. Pour en savoir plus sur les régions et les points de terminaison, consultez la section Régions et points de terminaison.

Exemple de code

Configurer les tags d'un bucket

L'exemple de code suivant configure les tags d'un bucket :

<?php

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

use AlibabaCloud\Oss\V2 as Oss;

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

// Generate a list of long options for parsing command line parameters.
$longopts = \array_map(function ($key) {
    return "$key:"; // The colon (:) following each parameter indicates that the parameter is required.
}, array_keys($optsdesc));

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

// Check whether the required parameters have been 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); 
    }
}

// Retrieve the values of the command line parameters.
$region = $options["region"]; // Region in which the bucket is located.
$bucket = $options["bucket"]; // Name of the bucket.

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

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

// Specify the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);

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

// Specify the endpoint if one is provided.
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]);
}

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

// Create tags containing multiple key-value pairs for the bucket.
$tagging = new Oss\Models\Tagging(
    tagSet: new Oss\Models\TagSet(
        tags: [new Oss\Models\Tag(key: 'key1', value: 'value1'), new Oss\Models\Tag(key: 'key2', value: 'value2')]
    )
);

// Create a request object for configuring tags and include the tagging information. 
$request = new Oss\Models\PutBucketTagsRequest(bucket: $bucket, tagging: $tagging);

// Configure tags for the bucket using the putBucketTags method.
$result = $client->putBucketTags($request);

// Output the result.
printf(
    'status code:' . $result->statusCode . PHP_EOL . // HTTP status code.
    'request id:' . $result->requestId // Unique ID of the request.
);

Interroger les tags d'un bucket

L'exemple de code suivant interroge les tags d'un bucket :

<?php

require_once __DIR__ . '/../vendor/autoload.php'; // Automatically 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], // The region parameter is required. Example: oss-cn-hangzhou.
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint parameter is optional.
    "bucket" => ['help' => 'The name of the bucket', 'required' => True], // The name of the bucket is required.
];
$longopts = \array_map(function ($key) {
    return "$key:"; // The colon (:) following each parameter indicates that the parameter 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); 
    }
}

// Retrieve the values of the command line parameters.
$region = $options["region"]; // Region in which the bucket is located.
$bucket = $options["bucket"]; // Name of the bucket.

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

// Use the default configuration 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 one is provided.
}

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

// Create a request object for querying the tags configured for the bucket.
$request = new Oss\Models\GetBucketTagsRequest(bucket: $bucket); 

// Query the tags using the getBucketTags method.
$result = $client->getBucketTags($request); 

// Output the result.
printf(
    'status code:' . $result->statusCode . PHP_EOL . // HTTP status code.
    'request id:' . $result->requestId . PHP_EOL . // Unique ID of the request.
    'tags:' . var_export($result->tagging, true) // Returns an array that consists of tags.
);

Supprimer tous les tags configurés pour 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], // The region parameter is required. Example: oss-cn-hangzhou.
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint parameter is optional.
    "bucket" => ['help' => 'The name of the bucket', 'required' => True], // The name of the bucket is required.
];
$longopts = \array_map(function ($key) {
    return "$key:"; // The colon (:) following each parameter indicates that the parameter 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); 
    }
}

// Retrieve the values of the command line parameters.
$region = $options["region"]; // Region in which the bucket is located.
$bucket = $options["bucket"]; // Name of the bucket.

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

// Use the default configuration 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 one is provided.
}

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

// Create a request object for deleting tags.
$request = new Oss\Models\DeleteBucketTagsRequest(bucket: $bucket); 

// Delete the tags configured for the bucket using the deleteBucketTags method.
$result = $client->deleteBucketTags($request); 

// Output the result.
printf(
    'status code:' . $result->statusCode . PHP_EOL . // HTTP status code.
    'request id:' . $result->requestId // Unique ID of the request.
);

Références

  • Pour configurer les tags d'un bucket via l'API, consultez la section PutBucketTags.

  • Pour interroger les tags d'un bucket via l'API, consultez la section GetBucketTags.

  • Pour supprimer les tags d'un bucket via l'API, consultez la section DeleteBucketTags.