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

Updated at:

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:

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:

PlaceholderDescription
yourVersionIdThe 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