Bucket tags (Java SDK V2)

Updated at:

Manage bucket tags by calling the PutBucketTags, GetBucketTags, and DeleteBucketTags operations with OSS Java SDK V2.

Prerequisites

  • OSS Java SDK V2 is installed. If you use Maven, add the com.aliyun:alibabacloud-oss-v2 dependency to pom.xml.

  • Access credentials are configured by using environment variables. The samples in this topic use EnvironmentVariableCredentialsProvider to read credentials from the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.

Permissions

An Alibaba Cloud account has full permissions by default. RAM users and RAM roles under an Alibaba Cloud account have no permissions by default and must be granted the required permissions by the Alibaba Cloud account or an account administrator.

API

Action

Description

PutBucketTags

oss:PutBucketTagging

Set, query, or delete bucket tags.

Sample code

import com.aliyun.sdk.service.oss2.OSSClient;
import com.aliyun.sdk.service.oss2.OSSClientBuilder;
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.models.*;
import java.util.ArrayList;
import java.util.List;

public class BucketTagsSample {
    public static void main(String[] args) throws Exception {
        String region = "cn-hangzhou";
        String bucket = "examplebucket";

        CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
        OSSClientBuilder clientBuilder = OSSClient.newBuilder()
                .credentialsProvider(provider)
                .region(region);

        try (OSSClient client = clientBuilder.build()) {
            // Set bucket tags
            List<Tag> tagList = new ArrayList<>();
            tagList.add(Tag.newBuilder().key("key1").value("value1").build());
            tagList.add(Tag.newBuilder().key("key2").value("value2").build());

            Tagging tagging = Tagging.newBuilder()
                    .tagSet(TagSet.newBuilder().tags(tagList).build())
                    .build();

            PutBucketTagsResult putResult = client.putBucketTags(PutBucketTagsRequest.newBuilder()
                    .bucket(bucket).tagging(tagging).build());

            System.out.printf("PutBucketTags status code:%d%n", putResult.statusCode());

            // Query bucket tags
            GetBucketTagsResult getResult = client.getBucketTags(
                    GetBucketTagsRequest.newBuilder().bucket(bucket).build());

            for (Tag tag : getResult.tagging().tagSet().tags()) {
                System.out.printf("tag: %s = %s%n", tag.key(), tag.value());
            }

            // Delete bucket tags
            DeleteBucketTagsResult delResult = client.deleteBucketTags(
                    DeleteBucketTagsRequest.newBuilder().bucket(bucket).build());

            System.out.printf("DeleteBucketTags status code:%d%n", delResult.statusCode());

        } catch (Exception e) {
            System.out.printf("error:%n%s", e);
        }
    }
}

References