All Products
Search
Document Center

Object Storage Service:Query the storage usage of a bucket

Last Updated:Oct 20, 2024

This topic describes how to query the storage usage of a specific Object Storage Service (OSS) bucket and the number and storage usage of objects of different storage classes in the bucket.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions, endpoints and open ports.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.

  • To query the storage usage of a bucket, you must have the oss:GetBucketStat permission. For more information, see Attach a custom policy to a RAM user.

Examples

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

Important

Only OSS SDK for Java 3.14.1 and later support all attributes that are included in the following sample code.

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
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 the actual endpoint. 
        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 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 OSSClient instance. 
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);        
        OSS ossClient = OSSClientBuilder.create()
        .endpoint(endpoint)
        .credentialsProvider(credentialsProvider)
        .clientConfiguration(clientBuilderConfiguration)
        .region(region)               
        .build();

        try {
            BucketStat stat = ossClient.getBucketStat(bucketName);
            // Query the total storage usage of the bucket. Unit: bytes. 
            System.out.println("StorageSize:"+stat.getStorageSize());
            // Query the total number of objects that are stored in the bucket. 
            System.out.println("ObjectCount:"+stat.getObjectCount());
            // Query the number of multipart upload tasks that are initiated but are not completed or canceled. 
            System.out.println("MultipartUploadCount:"+stat.getMultipartUploadCount());
            // Query the storage usage of Standard objects in the bucket. Unit: bytes. 
            System.out.println("StandardStorage:"+stat.getStandardStorage());
            // Query the number of Standard objects in the bucket. 
            System.out.println("StandardObjectCount:"+stat.getStandardObjectCount());
            // Query the number of LiveChannels in the bucket. 
            System.out.println("LiveChannelCount:"+stat.getLiveChannelCount());
            // Query the time when the obtained information was last modified. The value is a UNIX timestamp. Unit: seconds. 
            System.out.println("LastModifiedTime:"+stat.getLastModifiedTime());
            // Query the billed storage usage of Infrequent Access (IA) objects in the bucket. Unit: bytes. 
            System.out.println("InfrequentAccessStorage:"+stat.getInfrequentAccessStorage());
            // Query the actual storage usage of IA objects in the bucket. Unit: bytes. 
            System.out.println("InfrequentAccessRealStorage:"+stat.getInfrequentAccessRealStorage());
            // Query the number of IA objects in the bucket. 
            System.out.println("InfrequentAccessObjectCount:"+stat.getInfrequentAccessObjectCount());
            // Query the billed storage usage of Archive objects in the bucket. Unit: bytes. 
            System.out.println("ArchiveStorage:"+stat.getArchiveStorage());
            // Query the actual storage usage of Archive objects in the bucket. Unit: bytes. 
            System.out.println("ArchiveRealStorage:"+stat.getArchiveRealStorage());
            // Query the number of Archive objects in the bucket. 
            System.out.println("ArchiveObjectCount:"+stat.getArchiveObjectCount());
            // Query the billed storage usage of Cold Archive objects in the bucket. Unit: bytes. 
            System.out.println("ColdArchiveStorage:"+stat.getColdArchiveStorage());
            // Query the actual storage usage of Cold Archive objects in the bucket. Unit: bytes. 
            System.out.println("ColdArchiveRealStorage:"+stat.getColdArchiveRealStorage());
            // Query the number of Cold Archive objects in the bucket. 
            System.out.println("ColdArchiveObjectCount:"+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 usage of a specific bucket and the number and storage usage of objects of different storage classes in the bucket, see GetBucketStat.