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

Object Storage Service:バケットのリスト (Java SDK V1)

最終更新日:Nov 27, 2025

バケットは、オブジェクトを格納するために使用されるコンテナーです。すべてのオブジェクトはバケットに格納する必要があります。バケットはアルファベット順にリストされます。ご利用の Alibaba Cloud アカウントに属するすべてのリージョンのバケットをリストし、特定の条件に基づいてフィルターできます。

注意事項

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

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

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

  • バケットをリストするには、oss:ListBuckets 権限が必要です。詳細については、「RAM ユーザーへのカスタムアクセスポリシーのアタッチ」をご参照ください。

  • 低頻度アクセス (IA) またはアーカイブバケットがある場合は、Java SDK 2.6.0 以降を使用してください。

  • 以下のコードは、現在の Alibaba Cloud アカウント配下のすべてのリージョンにあるバケットをリストします。このコードを使用して特定のリージョン内のバケットをリストすることはできません。指定されたエンドポイントに関連付けられているリージョンは、結果に影響しません。

すべてのバケットのリスト

以下のコードは、現在の Alibaba Cloud アカウント配下のすべてのリージョンにあるすべてのバケットをリストする方法の例です。

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.Bucket;

import java.util.List;

public class Demo {

    public static void main(String[] args) throws Exception {
        // この例では、エンドポイントは https://oss-cn-hangzhou.aliyuncs.com に設定されています。これは、中国 (杭州) リージョンのエンドポイントです。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // バケットが配置されているリージョンを指定します。この例では、中国 (杭州) リージョンが使用されます。Region を cn-hangzhou に設定します。
        String region = "cn-hangzhou";
        
        // 環境変数からアクセス認証情報を取得します。このサンプルコードを実行する前に、環境変数 OSS_ACCESS_KEY_ID および OSS_ACCESS_KEY_SECRET が設定されていることを確認してください。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

        // 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 {
            // 現在の Alibaba Cloud アカウント配下のすべてのリージョンにあるすべてのバケットをリストします。
            List<Bucket> buckets = ossClient.listBuckets();
            for (Bucket bucket : buckets) {
                System.out.println(" - " + bucket.getName());
            }
        } 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();
            }
        }
    }
}

リソースグループ内のバケットのリスト

以下のコードは、現在の Alibaba Cloud アカウント配下の指定されたリソースグループ内のバケットをリストする方法の例です。

説明

Alibaba Cloud アカウントには、1 つのデフォルトリソースグループと複数のカスタムリソースグループがあります。バケットを作成する際にリソースグループ ID を指定しない場合、バケットはデフォルトリソースグループに属します。

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        // この例では、エンドポイントは https://oss-cn-hangzhou.aliyuncs.com に設定されています。これは、中国 (杭州) リージョンのエンドポイントです。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 環境変数からアクセス認証情報を取得します。このサンプルコードを実行する前に、環境変数 OSS_ACCESS_KEY_ID および OSS_ACCESS_KEY_SECRET が設定されていることを確認してください。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // リソースグループの ID を指定します。リソースグループ ID を指定しない場合、デフォルトリソースグループ内のバケットがリストされます。
        String rsId = "rg-aek27tc****";
        // バケットが配置されているリージョンを指定します。この例では、中国 (杭州) リージョンが使用されます。Region を 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 {
            // バケットをリストします。
            ListBucketsRequest listBucketsRequest = new ListBucketsRequest();
            // 現在の Alibaba Cloud アカウント配下の指定されたリソースグループ内のバケットをリストします。
            listBucketsRequest.setResourceGroupId(rsId);
            BucketList bucketList = ossClient.listBuckets(listBucketsRequest);
            for (Bucket bucket : bucketList.getBucketList()) {
                System.out.println(" - " + bucket.getName());
            }
        } 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();
            }
        }
    }
}               

指定されたプレフィックスを持つバケットのリスト

以下のコードは、現在の Alibaba Cloud アカウント配下のすべてのリージョンで、プレフィックスが "example" のバケットをリストする方法の例です。

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.Bucket;
import com.aliyun.oss.model.BucketList;
import com.aliyun.oss.model.ListBucketsRequest;

public class Demo {

    public static void main(String[] args) throws Exception {
        // この例では、エンドポイントは https://oss-cn-hangzhou.aliyuncs.com に設定されています。これは、中国 (杭州) リージョンのエンドポイントです。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // バケットが配置されているリージョンを指定します。この例では、中国 (杭州) リージョンが使用されます。Region を cn-hangzhou に設定します。
        String region = "cn-hangzhou";
        
        // 環境変数からアクセス認証情報を取得します。このサンプルコードを実行する前に、環境変数 OSS_ACCESS_KEY_ID および OSS_ACCESS_KEY_SECRET が設定されていることを確認してください。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

        // 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 {
            ListBucketsRequest listBucketsRequest = new ListBucketsRequest();
            // 現在の Alibaba Cloud アカウント配下のすべてのリージョンで、指定されたプレフィックスを持つバケットをリストします。
            listBucketsRequest.setPrefix("example");
            BucketList bucketList = ossClient.listBuckets(listBucketsRequest);
            for (Bucket bucket : bucketList.getBucketList()) {
                System.out.println(" - " + bucket.getName());
            }
        } 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();
            }
        }
    }
}

指定されたマーカー以降のバケットのリスト

以下のコードは、現在の Alibaba Cloud アカウント配下のすべてのリージョンで、名前がアルファベット順で "examplebucket" より後にあるバケットをリストする方法の例です。

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;
import java.util.List;

public class Demo {
    public static void main(String[] args) throws com.aliyuncs.exceptions.ClientException {
        // この例では、エンドポイントは https://oss-cn-hangzhou.aliyuncs.com に設定されています。これは、中国 (杭州) リージョンのエンドポイントです。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 環境変数からアクセス認証情報を取得します。このサンプルコードを実行する前に、環境変数 OSS_ACCESS_KEY_ID および OSS_ACCESS_KEY_SECRET が設定されていることを確認してください。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // バケット名を指定します。例:examplebucket
        String bucketName = "examplebucket";
        // 各ページにリストするバケットの数を 200 に設定します。
        int maxKeys = 200;
        // バケットが配置されているリージョンを指定します。この例では、中国 (杭州) リージョンが使用されます。Region を 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 {
            // 現在の Alibaba Cloud アカウント配下のすべてのリージョンで、名前がアルファベット順で指定されたマーカーより後にあるバケットをリストします。
            String nextMarker = bucketName;
            BucketList bucketListing;

            do {
                bucketListing = ossClient.listBuckets(new ListBucketsRequest().withMarker(nextMarker).withMaxKeys(maxKeys));

                List<Bucket> sums = bucketListing.getBucketList();
                for (Bucket s : sums) {
                    System.out.println("\t" + s.getName());
                }

                nextMarker = bucketListing.getNextMarker();

            } while (bucketListing.isTruncated());
        } 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();
            }
        }
    }
}

指定された数のバケットのリスト

以下のコードは、現在の Alibaba Cloud アカウント配下のすべてのリージョンにあるバケットをリストし、返されるバケットの数を最大 500 に制限する方法の例です。

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.Bucket;
import com.aliyun.oss.model.BucketList;
import com.aliyun.oss.model.ListBucketsRequest;

public class Demo {

    public static void main(String[] args) throws Exception {
        // この例では、エンドポイントは https://oss-cn-hangzhou.aliyuncs.com に設定されています。これは、中国 (杭州) リージョンのエンドポイントです。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // バケットが配置されているリージョンを指定します。この例では、中国 (杭州) リージョンが使用されます。Region を cn-hangzhou に設定します。
        String region = "cn-hangzhou";
                
        // 環境変数からアクセス認証情報を取得します。このサンプルコードを実行する前に、環境変数 OSS_ACCESS_KEY_ID および OSS_ACCESS_KEY_SECRET が設定されていることを確認してください。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

        // 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 {
            ListBucketsRequest listBucketsRequest = new ListBucketsRequest();           
            // 現在の Alibaba Cloud アカウント配下のすべてのリージョンにあるバケットをリストし、リストするバケットの最大数を 500 に制限します。デフォルト値は 100 です。最大値は 1000 です。
            listBucketsRequest.setMaxKeys(500);
            BucketList bucketList = ossClient.listBuckets(listBucketsRequest);
            for (Bucket bucket : bucketList.getBucketList()) {
                System.out.println(" - " + bucket.getName());
            }
        } 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 操作の詳細については、「ListBuckets (GetService)」をご参照ください。