All Products
Search
Document Center

Object Storage Service:List objects in CloudBox

Last Updated:Aug 08, 2025

By default, objects in an OSS on CloudBox bucket are listed in alphabetical order. You can list all objects in the bucket, objects with a specific prefix, or a specific number of objects.

Use Alibaba Cloud SDKs

You can list all objects in an OSS on CloudBox bucket using the Java SDK. The Java SDK version must be 3.15.0 or later.

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;
import java.util.List;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;

public class Demo {
    public static void main(String[] args) throws Exception {
        // Specify the data endpoint of the OSS on CloudBox bucket.
        String endpoint = "https://cb-f8z7yvzgwfkl9q0h****.cn-heyuan.oss-cloudbox.aliyuncs.com";
        // Obtain access credentials from environment variables. Before running this 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 OSS on CloudBox bucket. For example, examplebucket.
        String bucketName = "examplebucket";
        // Specify the region where the OSS on CloudBox bucket is located.
        String region = "cn-hangzhou";
        // Specify the ID of the CloudBox.
        String cloudBoxId = "cb-f8z7yvzgwfkl9q0h****";
        // Specify a prefix. For example, exampledir/object.
        String keyPrefix = "exampledir/object";

        // Create an OSSClient instance.
        // Call the shutdown method to release resources when you no longer need the OSSClient instance.
        ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
        conf.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(new DefaultCredentialProvider(credentialsProvider.getCredentials()))
                .clientConfiguration(conf)
                .region(region)
                .cloudBoxId(cloudBoxId)
                .build();

        try {
            // List objects. If you do not set keyPrefix, all objects in the OSS on CloudBox bucket are listed. If you set keyPrefix, objects whose names contain the specified prefix are listed.
            ObjectListing objectListing = ossClient.listObjects(bucketName, keyPrefix);
            List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
            for (OSSObjectSummary s : sums) {
                System.out.println("\t" + s.getKey());
            }
        } 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();
            }
        }
    }
}                  

Use the ossutil command line interface

For more information about how to list objects using ossutil, see list-objects (get-bucket).

Use REST APIs

If your application requires a high degree of customization, you can send REST API requests directly. This requires you to manually write code to calculate signatures.

You can call the GetBucket (ListObjects) or ListObjectsV2 (GetBucketV2) API operation to list objects in a bucket. Use the newer GetBucketV2 (ListObjectsV2) operation for application development. To ensure backward compatibility, OSS continues to support GetBucket (ListObjects).