Bucket Tagging

Last updated: 2020-09-09 11:34:33

    Overview

    This document provides an overview of APIs and SDK code samples related to bucket tagging.

    API Operation Description
    PUT Bucket tagging Setting bucket tags Sets tags for an existing bucket
    GET Bucket tagging Querying bucket tags Queries the existing tags of a specified bucket
    DELETE Bucket tagging Deleting bucket tags Deletes specified bucket tags

    SDK API Reference

    For the parameters and method descriptions of all the APIs in the SDK, please see SDK API Reference.

    Setting Bucket Tags

    API description

    This API is used to set tags for an existing bucket.

    Sample code

    Objective-C

    QCloudPutBucketTaggingRequest *putReq = [QCloudPutBucketTaggingRequest new];
    
    // Bucket name in the format: `BucketName-APPID`
    putReq.bucket = @"examplebucket-1250000000";
    
    // A set of tags
    QCloudBucketTagging *taggings = [QCloudBucketTagging new];
    
    QCloudBucketTag *tag1 = [QCloudBucketTag new];
    
    // Key of the tag, which can be up to 128 bytes of letters, digits, spaces, plus signs, minus signs, underscores, equal signs, dots,
    // colons, and slashes
    tag1.key = @"age";
    
    // Value of the tag, which can be up to 256 bytes of letters, digits, spaces, plus signs, minus signs, underscores, equal signs, dots,
    // colons, and slashes
    tag1.value = @"20";
    QCloudBucketTag *tag2 = [QCloudBucketTag new];
    tag2.key = @"name";
    tag2.value = @"karis";
    
    // A set of tags. Up to 10 tags are supported
    QCloudBucketTagSet *tagSet = [QCloudBucketTagSet new];
    tagSet.tag = @[tag1,tag2];
    taggings.tagSet = tagSet;
    
    // A set of tags
    putReq.taggings = taggings;
    
    [putReq setFinishBlock:^(id outputObject, NSError *error) {
        // `outputObject` contains all the HTTP response headers
        NSDictionary* info = (NSDictionary *) outputObject;
    }];
    [[QCloudCOSXMLService defaultCOSXML] PutBucketTagging:putReq];

    Note:

    For the complete sample, go to GitHub.

    Swift

    let req = QCloudPutBucketTaggingRequest.init();
    
    // Bucket name in the format: `BucketName-APPID`
    req.bucket = "examplebucket-1250000000";
    let taggings = QCloudBucketTagging.init();
    
    // A set of tags
    let tagSet = QCloudBucketTagSet.init();
    taggings.tagSet = tagSet;
    let tag1 = QCloudBucketTag.init();
    
    // Key of the tag, which can be up to 128 bytes of letters, digits, spaces, plus signs, minus signs, underscores, equal signs, dots,
    // colons, and slashes
    tag1.key = "age";
    
    // Value of the tag, which can be up to 256 bytes of letters, digits, spaces, plus signs, minus signs, underscores, equal signs, dots,
    // colons, and slashes
    tag1.value = "20";
    
    let tag2 = QCloudBucketTag.init();
    tag2.key = "name";
    tag2.value = "karis";
    
    // A set of tags. Up to 10 tags are supported
    tagSet.tag = [tag1,tag2];
    
    // A set of tags
    req.taggings = taggings;
    req.finishBlock = {(result,error) in
        if let result = result {
            // “result” contains response headers
        } else {
            print(error!);
        }
    }
    QCloudCOSXMLService.defaultCOSXML().putBucketTagging(req);

    Note:

    For the complete sample, go to GitHub.

    Querying Bucket Tags

    API description

    This API is used to query the existing tags of a specified bucket.

    Sample code

    Objective-C

    QCloudGetBucketTaggingRequest *getReq = [QCloudGetBucketTaggingRequest new];
    
    // Bucket name in the format: `BucketName-APPID`
    getReq.bucket = @"examplebucket-1250000000";
    
    [getReq setFinishBlock:^(QCloudBucketTagging * result, NSError * error) {
        // A set of tags
        QCloudBucketTagSet * tagSet = result.tagSet;
    }];
    [[QCloudCOSXMLService defaultCOSXML] GetBucketTagging:getReq];

    Note:

    For the complete sample, go to GitHub.

    Swift

    let req = QCloudGetBucketTaggingRequest.init();
    
    // Bucket name in the format: `BucketName-APPID`
    req.bucket = "examplebucket-1250000000";
    req.setFinish { (result, error) in
        if let result = result {
            // A set of tags
            let tagSet = result.tagSet
        } else {
            print(error!);
        }
    };
    QCloudCOSXMLService.defaultCOSXML().getBucketTagging(req);

    Note:

    For the complete sample, go to GitHub.

    Deleting Bucket Tags

    API description

    This API is used to delete the existing tags of a specified bucket.

    Sample code

    Objective-C

    QCloudDeleteBucketTaggingRequest *delReq = [QCloudDeleteBucketTaggingRequest new];
    
    // Bucket name in the format: `BucketName-APPID`
    delReq.bucket =  @"examplebucket-1250000000";
    
    [delReq setFinishBlock:^(id outputObject, NSError *error) {
        // `outputObject` contains all the HTTP response headers
        NSDictionary* info = (NSDictionary *) outputObject;
    }];
    [[QCloudCOSXMLService defaultCOSXML] DeleteBucketTagging:delReq];

    Note:

    For the complete sample, go to GitHub.

    Swift

    let req = QCloudDeleteBucketTaggingRequest.init();
    
    // Bucket name in the format: `BucketName-APPID`
    req.bucket = "examplebucket-1250000000";
    req.finishBlock =  { (result, error) in
        if let result = result {
            // “result” contains response headers
        } else {
            print(error!);
        }
    };
    QCloudCOSXMLService.defaultCOSXML().deleteBucketTagging(req);

    Note:

    For the complete sample, go to GitHub.

    Was this page helpful?

    Was this page helpful?

    • Not at all
    • Not very helpful
    • Somewhat helpful
    • Very helpful
    • Extremely helpful
    Send Feedback
    Help