Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Paketleri listeleme (OSS SDK for Java 1.0)

Son Güncelleme:Oct 01, 2026

Paket, Object Storage Service (OSS) içinde depolanan nesneler için bir kapsayıcıdır. OSS'deki tüm nesneler paketlerde bulunur. Paketler alfabetik sıraya göre listelenir. Mevcut Alibaba Cloud hesabınıza ait olan ve belirli koşulları karşılayan tüm bölgelerdeki paketleri listeleyebilirsiniz.

Kullanım notları

  • Bu konuda China (Hangzhou) bölgesinin genel uç noktası kullanılmaktadır. Aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'ye erişmek için dahili uç noktayı kullanın. Desteklenen bölgeler ve uç noktalar hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.

  • Bu konuda erişim kimlik bilgileri ortam değişkenlerinden alınır. Daha fazla bilgi için bkz. Erişim kimlik bilgilerini yapılandırma.

  • Bu konu, bir OSS uç noktası kullanarak bir OSSClient örneği oluşturmayı göstermektedir. Özel alan adı kullanma veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için bkz. İstemci yapılandırması.

  • Paketleri listelemek için oss:ListBuckets iznine sahip olmanız gerekir. Daha fazla bilgi için bkz. Özel ilke atama.

  • Seyrek Erişim (IA) ve Arşiv paketlerini listelemek için OSS SDK for Java 2.6.0 veya sonraki bir sürümü kullanın.

  • Aşağıdaki örnek kodu yalnızca mevcut Alibaba Cloud hesabındaki tüm bölgelerde bulunan paketleri listelemek için çalıştırabilirsiniz. Belirli bir bölgedeki paketleri listelemek için aşağıdaki örnek kodu çalıştıramazsınız. Liste sonucu, belirtilen bölge uç noktasından bağımsızdır.

Bir Alibaba Cloud hesabındaki tüm paketleri listeleme

Aşağıdaki örnek kod, mevcut Alibaba Cloud hesabındaki tüm bölgelerde bulunan paketlerin nasıl listeleneceğini gösterir:

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 {
        // For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
        String region = "cn-hangzhou";
        
        // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

        // Create an OSS Client instance. 
        // Call the shutdown method to release associated resources when the OSS Client is no longer in use.
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);        
        OSS ossClient = OSSClientBuilder.create()
        .endpoint(endpoint)
        .credentialsProvider(credentialsProvider)
        .clientConfiguration(clientBuilderConfiguration)
        .region(region)               
        .build();

        try {
            // List all buckets in all regions within the current Alibaba Cloud account. 
            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();
            }
        }
    }
}

Bir kaynak grubundaki paketleri listeleme

Aşağıdaki örnek kod, mevcut Alibaba Cloud hesabındaki bir kaynak grubunda bulunan paketlerin nasıl listeleneceğini gösterir.

Not

Bir Alibaba Cloud hesabı, bir varsayılan kaynak grubu ve birden çok özel kaynak grubu içerir. Bir paket oluştururken bir kaynak grubu kimliği belirtmezseniz paket varsayılan kaynak grubuna ait olur.

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 {
        // For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the ID of the resource group. If you do not specify a resource group ID, the buckets in the default resource group are listed. 
        String rsId = "rg-aek27tc****";
        // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
        String region = "cn-hangzhou";
        
        // Create an OSS Client instance. 
        // Call the shutdown method to release associated resources when the OSS Client is no longer in use.
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);        
        OSS ossClient = OSSClientBuilder.create()
        .endpoint(endpoint)
        .credentialsProvider(credentialsProvider)
        .clientConfiguration(clientBuilderConfiguration)
        .region(region)               
        .build();

        try {
            // List the buckets. 
            ListBucketsRequest listBucketsRequest = new ListBucketsRequest();
            // List the buckets in the specified resource group within the current Alibaba Cloud account. 
            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();
            }
        }
    }
}               

Adlarında belirtilen önek bulunan paketleri listeleme

Aşağıdaki örnek kod, mevcut Alibaba Cloud hesabındaki tüm bölgelerde adlarında example öneki bulunan paketlerin nasıl listeleneceğini gösterir:

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 {
        // For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
        String region = "cn-hangzhou";
        
        // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

        // Create an OSS Client instance. 
        // Call the shutdown method to release associated resources when the OSS Client is no longer in use.
        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();
            // List the buckets whose names contain the specified prefix in all regions within the current Alibaba Cloud account.            
            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();
            }
        }
    }
}

İşaretçi ile belirtilen paketten sonra alfabetik olarak gelen paketleri listeleme

Aşağıdaki örnek kod, mevcut Alibaba Cloud hesabındaki tüm bölgelerde examplebucket adlı paketten sonra alfabetik olarak gelen paketlerin nasıl listeleneceğini gösterir:

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 {
        // For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the name of the bucket. Example: examplebucket. 
        String bucketName = "examplebucket";
        // Specify that up to 200 buckets are listed on each page. 
        int maxKeys = 200;
        // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
        String region = "cn-hangzhou";
        
        // Create an OSS Client instance. 
        // Call the shutdown method to release associated resources when the OSS Client is no longer in use.
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);        
        OSS ossClient = OSSClientBuilder.create()
        .endpoint(endpoint)
        .credentialsProvider(credentialsProvider)
        .clientConfiguration(clientBuilderConfiguration)
        .region(region)               
        .build();

        try {
            // List the buckets whose names are alphabetically after the bucket specified by marker in all regions within the current Alibaba Cloud account. 
            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();
            }
        }
    }
}

Belirli sayıda paketi listeleme

Aşağıdaki örnek kod, mevcut Alibaba Cloud hesabındaki tüm bölgelerde bulunan paketleri listelemeyi ve listelenebilecek maksimum paket sayısını 500 olarak ayarlamayı gösterir:

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 {
        // For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
        String region = "cn-hangzhou";
                
        // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

        // Create an OSS Client instance. 
        // Call the shutdown method to release associated resources when the OSS Client is no longer in use.
        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();           
            // List the buckets in all regions within the current Alibaba Cloud account and set the maximum number of buckets that can be listed to 500. Maximum value: 1000. Default value: 100. 
            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();
            }
        }
    }
}

İlgili bilgiler

  • Paketleri listelemek için kullanılan tam örnek kod için GitHub adresini ziyaret edin.

  • Paketleri listelemek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. ListBuckets (GetService).