Delete object tags using OSS SDK for Python 2.0
This topic describes how to use OSS SDK for Python 2.0 to delete the tags of an object.
Notes
-
The sample code in this topic uses the region ID
cn-hangzhouof 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 in which the bucket is located, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints. -
A key-value pair is used to identify objects. For more information about object tagging, see Tag objects in OSS Developer Guide.
-
For more information about how to delete the tags of an object, see DeleteObjectTagging.
-
To delete the tags of an object, you must have the
oss:DeleteObjectTaggingpermission. For more information, see Grant a custom policy.
Method
delete_object_tagging(request: DeleteObjectTaggingRequest, **kwargs) → DeleteObjectTaggingResult
Request parameters
|
Parameter |
Type |
Description |
|
request |
DeleteObjectTaggingRequest |
The request parameters. For more information, see DeleteObjectTaggingRequest |
Response parameters
|
Type |
Description |
|
DeleteObjectTaggingResult |
The return value. For more information, see DeleteObjectTaggingResult |
For the complete method definition, see delete_object_tagging.
Sample code
Below is the sample code for deleting the tags of a specific object in a bucket:
import argparse
import alibabacloud_oss_v2 as oss
# Create a command-line parameter parser and define the parameters.
parser = argparse.ArgumentParser(description="delete object tagging sample")
# Add command-line arguments.
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():
# Parse the command-line argument.
args = parser.parse_args()
# Obtain access credentials from environment variables for authentication.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the default configuration to create a configuration object.
cfg = oss.config.load_default()
# Set the credential provider.
cfg.credentials_provider = credentials_provider
# Set the region.
cfg.region = args.region
# If an endpoint is provided, configure the endpoint.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Create an OSS client instance using the preceding configurations.
client = oss.Client(cfg)
# Delete the tags of the specified object.
result = client.delete_object_tagging(oss.DeleteObjectTaggingRequest(
bucket=args.bucket,
key=args.key,
))
# Print the result.
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' version id: {result.version_id},'
)
# all the main function when the script is directly run.
if __name__ == "__main__":
main()
Reference
-
For the complete sample code for deleting object tags, see delete_object_tagging.py.