Delete the tags of an object using OSS SDK for PHP 2.0
deleteObjectTagging removes all tags from an object. To remove tags from a specific version of a versioned object, specify its version ID. Without a version ID, OSS removes the tags of the current version.
Prerequisites
Before you begin, ensure that you have:
The
oss:DeleteObjectTaggingpermission. For more information, see Attach a custom policy to a RAM userThe version ID of the object, if you want to delete tags from a specific version. To get version IDs, see List objects
Usage notes
The sample code uses the China (Hangzhou) region (
cn-hangzhou) with a public endpoint by default. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For a full list of regions and endpoints, see Regions and endpoints.Tags are stored as key-value pairs on an object. For background on object tagging, see Object tagging. For the API reference, see DeleteObjectTagging.
Delete object tags
The following example deletes all tags from a specific version of an object. Remove the versionId parameter to target the current version instead.
<?php
// Include the autoload file to load dependencies.
require_once __DIR__ . '/../../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define command-line parameters.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The domain name used to access OSS.', 'required' => False],
"bucket" => ['help' => 'The name of the bucket.', 'required' => True],
"key" => ['help' => 'The name of the object.', 'required' => True],
];
// Build the long options list for getopt (each option requires a value).
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
$options = getopt("", $longopts);
// Validate required parameters.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help" . PHP_EOL;
exit(1);
}
}
$region = $options["region"];
$bucket = $options["bucket"];
$key = $options["key"];
// Load credentials from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Configure the client.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
$client = new Oss\Client($cfg);
// Build the delete request. Replace yourVersionId with the actual version ID.
$request = new Oss\Models\DeleteObjectTaggingRequest(
bucket: $bucket,
key: $key,
versionId: "yourVersionId",
);
// Delete all tags from the object.
$result = $client->deleteObjectTagging($request);
// Verify the result.
printf(
'status code: %s' . PHP_EOL .
'request ID: %s' . PHP_EOL,
$result->statusCode,
$result->requestId
);Replace the following placeholder before running the code:
| Placeholder | Description |
|---|---|
yourVersionId | The version ID of the object whose tags you want to delete |
Verify the result
A successful request returns HTTP status code 204. The response also includes a requestId that you can use to trace or debug the request.
What's next
Object tagging — Learn how object tags work and how to set them
DeleteObjectTagging API reference — Full API specification