オブジェクトにタグを設定した後、そのオブジェクトのタグ情報を取得できます。バケットでバージョン管理が有効になっている場合、OSS はデフォルトでオブジェクトの現在のバージョンのタグを取得します。特定のバージョンのタグを取得するには、オブジェクトのバージョン ID (versionId) を指定する必要があります。
注意事項
このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。同一リージョン内の他の Alibaba Cloud サービスから OSS にアクセスする場合は、内部エンドポイントを使用してください。サポートされているリージョンとエンドポイントの詳細については、「リージョンとエンドポイント」をご参照ください。
このトピックでは、環境変数からアクセス認証情報を取得します。アクセス認証情報の設定方法の詳細については、「アクセス認証情報の設定」をご参照ください。
このトピックでは、OSS エンドポイントを使用して OSSClient インスタンスを作成します。カスタムドメイン名またはセキュリティトークンサービス (STS) を使用して OSSClient インスタンスを作成する場合は、「一般的なシナリオの設定例」をご参照ください。
オブジェクトタグを取得するには、
oss:GetObjectTagging権限が必要です。詳細については、「RAM ユーザーへのカスタム権限の付与」をご参照ください。
オブジェクトタグ情報の取得
バケットでバージョン管理が無効になっている場合は、オブジェクトのタグ情報を取得できます。バケットでバージョン管理が有効になっている場合、OSS はデフォルトでオブジェクトの現在のバージョンのタグを取得します。
次のコードは、examplebucket バケットの exampledir フォルダにある exampleobject.txt ファイルのタグを取得する方法を示しています。
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;
import java.util.*;
public class Demo {
public static void main(String[] args) throws Throwable {
// 中国 (杭州) リージョンのエンドポイントを例として使用します。実際のエンドポイントに置き換えてください。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 環境変数からアクセス認証情報を取得します。このコードを実行する前に、OSS_ACCESS_KEY_ID および OSS_ACCESS_KEY_SECRET 環境変数が設定されていることを確認してください。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// バケット名を指定します。例:examplebucket。
String bucketName = "examplebucket";
// オブジェクトの完全なパスを指定します。例:exampledir/exampleobject.txt。完全なパスにバケット名を含めることはできません。
String objectName = "exampledir/exampleobject.txt";
// バケットが配置されているリージョンを指定します。たとえば、バケットが中国 (杭州) リージョンにある場合は、リージョンを cn-hangzhou に設定します。
String region = "cn-hangzhou";
// OSSClient インスタンスを作成します。
// OSSClient インスタンスが不要になったら、shutdown メソッドを呼び出してリソースを解放します。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// ファイルのタグを取得します。
TagSet tagSet = ossClient.getObjectTagging(bucketName, objectName);
Map<String, String> tags = tagSet.getAllTags();
// タグ情報を表示します。
System.out.println("object tagging: "+ tags.toString());
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}特定バージョンのオブジェクトのタグ情報の取得
バケットでバージョン管理が有効になっている場合、オブジェクトのバージョン ID (versionId) を指定することで、特定バージョンのオブジェクトのタグを取得できます。
次のコードは、examplebucket バケットの exampledir フォルダにある exampleobject.txt ファイルの特定バージョンのタグを取得する方法を示しています。
versionId の取得方法の詳細については、「ファイルのリスト (Java SDK V1)」をご参照ください。
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;
public class Demo {
public static void main(String[] args) throws Throwable {
// 中国 (杭州) リージョンのエンドポイントを例として使用します。実際のエンドポイントに置き換えてください。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 環境変数からアクセス認証情報を取得します。このコードを実行する前に、OSS_ACCESS_KEY_ID および OSS_ACCESS_KEY_SECRET 環境変数が設定されていることを確認してください。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// バケット名を指定します。例:examplebucket。
String bucketName = "examplebucket";
// オブジェクトの完全なパスを指定します。例:exampledir/exampleobject.txt。完全なパスにバケット名を含めることはできません。
String objectName = "exampledir/exampleobject.txt";
// オブジェクトのバージョン ID を指定します。例:CAEQMxiBgICAof2D0BYiIDJhMGE3N2M1YTI1NDQzOGY5NTkyNTI3MGYyMzJm****。
String versionId = "CAEQMxiBgICAof2D0BYiIDJhMGE3N2M1YTI1NDQzOGY5NTkyNTI3MGYyMzJm****";
// バケットが配置されているリージョンを指定します。たとえば、バケットが中国 (杭州) リージョンにある場合は、リージョンを cn-hangzhou に設定します。
String region = "cn-hangzhou";
// OSSClient インスタンスを作成します。
// OSSClient インスタンスが不要になったら、shutdown メソッドを呼び出してリソースを解放します。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
GenericRequest genericRequest = new GenericRequest(bucketName, objectName, versionId);
TagSet tagSet = ossClient.getObjectTagging(genericRequest);
// タグ情報を表示します。
System.out.println("object tagging: "+ tagSet.toString());
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}関連ドキュメント
オブジェクトタグを取得するための完全なサンプルコードについては、GitHub の例をご参照ください。
オブジェクトタグを取得するための API 操作の詳細については、「GetObjectTagging」をご参照ください。