すべてのプロダクト
Search
ドキュメントセンター

Object Storage Service:オブジェクトタグの削除 (Java SDK V1)

最終更新日:Nov 27, 2025

不要になったオブジェクトタグは削除できます。 バケットでバージョン管理が有効になっている場合、Object Storage Service (OSS) はデフォルトで現在のオブジェクトバージョンのタグを削除します。 特定のバージョンのタグを削除するには、オブジェクトのバージョン ID を指定する必要があります。

注意事項

  • このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。 同じリージョン内の他の Alibaba Cloud サービスから OSS にアクセスするには、内部エンドポイントを使用します。 サポートされているリージョンとエンドポイントの詳細については、リージョンとエンドポイントをご参照ください。

  • このトピックでは、アクセス認証情報は環境変数から取得されます。 アクセス認証情報の設定方法の詳細については、アクセス認証情報の設定をご参照ください。

  • このトピックでは、OSS エンドポイントを使用して OSSClient インスタンスを作成します。 カスタムドメイン名または Security Token Service (STS) を使用して OSSClient インスタンスを作成する場合は、一般的なシナリオの設定例をご参照ください。

  • オブジェクトタグを削除するには、oss:DeleteObjectTagging 権限が必要です。 詳細については、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;

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 {
            // オブジェクトタグを削除します。
            ossClient.deleteObjectTagging(bucketName, objectName);
        } 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 を指定することで、特定のオブジェクトバージョンのタグを削除できます。

次のコードは、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.GenericRequest;

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);
            ossClient.deleteObjectTagging(genericRequest);
        } 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 操作の詳細については、DeleteObjectTagging をご参照ください。