This topic describes how to query the storage capacity of a specified bucket, the number of objects of different storage classes in the bucket, and the storage usage of the objects.

Examples

The following code provides an example on how to query the storage capacity of a bucket named examplebucket, the number of objects of different storage classes in the bucket, and the storage usage of the objects:

Notice Only Object Storage Service (OSS) SDK for Java V3.14.1 and later support all attributes that are included in the following sample code.
import com.aliyun.oss.*;
import com.aliyun.oss.model.BucketStat;

public class Demo {
    public static void main(String[] args) throws Exception {
        // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
        String accessKeyId = "yourAccessKeyId";
        String accessKeySecret = "yourAccessKeySecret";
        // Specify the bucket name. Example: examplebucket. 
        String bucketName = "examplebucket";

        // Create an OSSClient instance. 
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            BucketStat stat = ossClient.getBucketStat(bucketName);
            // Query the total storage capacity of the bucket. Unit: bytes. 
            System.out.println(stat.getStorageSize());
            // Query the total number of objects that are stored in the bucket. 
            System.out.println(stat.getObjectCount());
            // Query the number of multipart upload tasks that have been initiated but are not completed or canceled. 
            System.out.println(stat.getMultipartUploadCount());
            // Query the storage usage of Standard objects in the bucket. Unit: bytes. 
            System.out.println(stat.getStandardStorage());
            // Query the number of Standard objects in the bucket. 
            System.out.println(stat.getStandardObjectCount());
            // Query the number of LiveChannels in the bucket. 
            System.out.println(stat.getLiveChannelCount());
            // Query the time when the obtained information is last modified. The value of stat.getLastModifiedTime() is a UNIX timestamp. Unit: seconds. 
            System.out.println(stat.getLastModifiedTime());
            // Query the billed storage usage of Infrequent Access (IA) objects in the bucket. Unit: bytes. 
            System.out.println(stat.getInfrequentAccessStorage());
            // Query the actual storage usage of IA objects in the bucket. Unit: bytes. 
            System.out.println(stat.getInfrequentAccessRealStorage());
            // Query the number of IA objects in the bucket. 
            System.out.println(stat.getInfrequentAccessObjectCount());
            // Query the billed storage usage of Archive objects in the bucket. Unit: bytes. 
            System.out.println(stat.getArchiveStorage());
            // Query the actual storage usage of Archive objects in the bucket. Unit: bytes. 
            System.out.println(stat.getArchiveRealStorage());
            // Query the number of Archive objects in the bucket. 
            System.out.println(stat.getArchiveObjectCount());
            // Query the billed storage usage of Cold Archive objects in the bucket. Unit: bytes. 
            System.out.println(stat.getColdArchiveStorage());
            // Query the actual storage usage of Cold Archive objects in the bucket. Unit: bytes. 
            System.out.println(stat.getColdArchiveRealStorage());
            // Query the number of Cold Archive objects in the bucket. 
            System.out.println(stat.getColdArchiveObjectCount());
        } 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();
            }
        }
    }
}

References

For more information about the API operation that you can call to query the storage capacity of a specified bucket, the number of objects of different storage classes in the bucket, and the storage usage of the objects, see GetBucketStat.