Remove all tags from an OSS object using the delete_object_tagging method in the Python SDK V2.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket with at least one tagged object
The
oss:DeleteObjectTaggingpermission granted to your RAM user. For details, see Grant custom permissions to a RAM user(Optional) Versioning enabled on the bucket, if you need to delete tags from a specific object version
Usage notes
Object tags are key-value pairs attached to an object. For background on object tagging, see Object tagging.
The sample code uses the
cn-hangzhouregion and a public endpoint by default. If you access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For endpoint mappings by region, see OSS regions and endpoints.For the REST API reference, see DeleteObjectTagging.
Method definition
delete_object_tagging(request: DeleteObjectTaggingRequest, **kwargs) -> DeleteObjectTaggingResultParameters
| Parameter | Type | Required | Description |
|---|---|---|---|
bucket | str | Yes | The name of the bucket. |
key | str | Yes | The name of the object. |
version_id | str | No | The version ID of the object. If omitted, tags are deleted from the current version. |
For the complete parameter reference, see DeleteObjectTaggingRequest.
Return value
| Field | Type | Description |
|---|---|---|
status_code | int | The HTTP status code. A value of 204 indicates success. |
request_id | str | The unique ID of the request, used for troubleshooting. |
version_id | str | The version ID of the object. Returns null for unversioned objects. |
For the complete return value reference, see DeleteObjectTaggingResult.
For the complete method definition, see delete_object_tagging.
Delete tags from an object
The following examples use command-line arguments to pass the bucket name, object name, and region. Credentials are loaded from environment variables.
Delete tags from the current version
import argparse
import alibabacloud_oss_v2 as oss
parser = argparse.ArgumentParser(description="delete object tagging sample")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS.')
parser.add_argument('--key', help='The name of the object.', required=True)
def main():
args = parser.parse_args()
# Load credentials from environment variables.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Build the client configuration.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
if args.endpoint is not None:
cfg.endpoint = args.endpoint
client = oss.Client(cfg)
# Delete all tags from the object.
result = client.delete_object_tagging(oss.DeleteObjectTaggingRequest(
bucket=args.bucket,
key=args.key,
))
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' version id: {result.version_id},'
)
if __name__ == "__main__":
main()Delete tags from a specific version
If versioning is enabled on the bucket, pass the version ID to remove tags from a specific object version without affecting other versions.
import argparse
import alibabacloud_oss_v2 as oss
parser = argparse.ArgumentParser(description="delete object tagging for a specific version")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS.')
parser.add_argument('--key', help='The name of the object.', required=True)
parser.add_argument('--version-id', help='The version ID of the object.', required=True)
def main():
args = parser.parse_args()
# Load credentials from environment variables.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Build the client configuration.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
if args.endpoint is not None:
cfg.endpoint = args.endpoint
client = oss.Client(cfg)
# Delete tags from the specified object version.
result = client.delete_object_tagging(oss.DeleteObjectTaggingRequest(
bucket=args.bucket,
key=args.key,
version_id=args.version_id,
))
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' version id: {result.version_id},'
)
if __name__ == "__main__":
main()References
For the complete sample code, see delete_object_tagging.py on GitHub.