Get object tags with versioning (Java SDK V2)

Updated at:

This topic describes how to obtain tags for a specific version of an object in a versioned bucket by using OSS Java SDK V2.

Prerequisites

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

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

Permissions

By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default. The Alibaba Cloud account owner or an administrator must grant the required permissions.

API

Actions

Description

GetObjectTagging

oss:GetObjectTagging

Obtains tags for a specific object version.

Sample code

Get tags for a specific object version

The following code obtains tags for a specific version of an object. If versionId is not specified, OSS obtains the tags of the current version:

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.*;

public class GetObjectTaggingVersionSample {
    public static void main(String[] args) throws Exception {
        String region = "cn-hangzhou";
        String bucket = "examplebucket";
        String key = "exampleobject";
        String versionId = "CAEQMxiBgIDU0YWNkBYiIGRhY2U5ZjYxYzU5NDk5MGFhZjM5Zjg5ZTk5ZDU1";

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

        try (OSSClient client = clientBuilder.build()) {
            GetObjectTaggingResult result = client.getObjectTagging(
                    GetObjectTaggingRequest.newBuilder()
                            .bucket(bucket)
                            .key(key)
                            .versionId(versionId)
                            .build());

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

References