Delete the tags of an object using OSS SDK for PHP 2.0

Updated at:

If versioning is enabled for a bucket that stores the object whose tags you want to delete, Object Storage Service (OSS) deletes the tags of the current version of the object by default. To delete the tags of a specified version of the object, you must specify its version ID.

Notes

  • The sample code in this topic uses the region ID cn-hangzhou of the China (Hangzhou) region. By default, a public endpoint is used to access resources in a bucket. If you want to access resources in the bucket from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about supported regions and endpoints, see Regions and endpoints.

  • Deleting the tags of an object requires the oss:DeleteObjectTagging permission. For details, see Grant a custom policy.

Note

Sample code

Note
  • If versioning is enabled for the bucket that stores the object whose tags you want to delete, you can delete the tags of a specified version of the object by specifying the version ID. If you do not specify the version ID of the object, only the tags of its current version are deleted.

  • For how to obtain version IDs, see List objects using OSS SDK for PHP 1.0.

Below is the sample code for deleting the tags of a specific version of an object in a bucket:

<?php

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

use AlibabaCloud\Oss\V2 as Oss;

// Define and describe 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.
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // (Optional) Specify the endpoint 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 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']; // Obtain help information for the parameters.
        echo "Error: the following arguments are required: --$key, $help" . PHP_EOL;
        exit(1); // Exit the program if a required parameter is missing.
    }
}

// Assign the values parsed from the command-line parameters to the corresponding variables.
$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.

// Load 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,
            versionId:"yourVersionId",// Specify the version ID of the object.
);

// 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 deletion succeeded.
    'request id:' . $result->requestId . PHP_EOL     // The request ID, which is used to debug or trace a request.
);