Cette rubrique explique comment utiliser le SDK OSS pour PHP 2.0 afin de supprimer les tags d'un objet.
Notes
L'exemple de code de cette rubrique utilise l'ID de région
cn-hangzhoupour la région Chine (Hangzhou). Par défaut, un endpoint public permet d'accéder aux ressources d'un bucket. Si vous accédez aux ressources depuis un autre service Alibaba Cloud situé dans la même région que le bucket, utilisez un endpoint interne. Pour plus d'informations sur les régions et les endpoints pris en charge par OSS, consultez la page Régions et endpoints.Pour supprimer les tags d'un objet, vous devez disposer de l'autorisation
oss:DeleteObjectTagging. Pour plus d'informations, consultez la section Attacher une stratégie personnalisée à un utilisateur RAM.
Une paire clé-valeur identifie les objets. Pour plus d'informations, consultez la page Tagging d'objet.
Pour plus d'informations sur la suppression des tags, consultez la documentation relative à l'opération DeleteObjectTagging.
Exemple de code
Voici l'exemple de code permettant de supprimer les tags d'un objet spécifique dans un bucket :
<?php
// Introduce autoload files to load dependent libraries.
require_once __DIR__ . '/../../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Specify descriptions for command line arguments.
$optsdesc = [
"region" => ['help' => The region in which the bucket is located.', 'required' => True], // (Required) Specify the region in which the bucket is located.
"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 for accessing OSS.
"bucket" => ['help' => The name of the bucket, 'required' => True], // (Required) Specify the name of the bucket.
"key" => ['help' => The name of the object, 'required' => True], // (Required) Specify the name of the object.
];
// Convert the descriptions to a list of long options required by getopt.
// Add a colon (:) to the end of each parameter to indicate that a value is required.
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
// Parse the command line arguments.
$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']; // Obtain help information for the parameters.
echo "Error: the following arguments are required: --$key, $help" . PHP_EOL;
exit(1); // Exit the program if the required parameters are not configured.
}
}
// Obtain values from the parsed parameters.
$region = $options["region"]; // The region in which the bucket is located.
$bucket = $options["bucket"]; // The name of the bucket.
$key = $options["key"]; // The name of the object.
// Obtain access credentials from environment variables.
// Use EnvironmentVariableCredentialsProvider to retrieve the AccessKey ID and AccessKey secret from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configuration of the SDK.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider); // Specify the credential provider.
$cfg->setRegion($region); // Specify the region in which the bucket is located.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]); // Specify the endpoint if one is provided.
}
// Create an OSSClient instance.
$client = new Oss\Client($cfg);
// Create a PutObjectTaggingRequest object to delete the tags configured for the object.
$request = new Oss\Models\DeleteObjectTaggingRequest(
bucket: $bucket,
key: $key
);
// Delete the tags of the object.
$result = $client->deleteObjectTagging($request);
// Display the result of deletion.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code. For example, HTTP status code 204 indicates that the tags are deleted.
'request id:' . $result-> requestId. PHP_EOL // The request ID, which is used to debug or trace a request.
);