Tous les produits
Search
Centre de documentation

Object Storage Service:Interroger la capacité de stockage d'un bucket (OSS SDK for Java 1.0)

Dernière mise à jour :Aug 18, 2026

Cette rubrique explique comment récupérer la capacité de stockage d'un bucket, ainsi que le nombre et la capacité de stockage des objets pour chaque classe de stockage au sein du bucket.

Notes d'utilisation

  • Cette rubrique utilise l'endpoint public de la région Chine (Hangzhou). Pour accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un endpoint interne. Pour plus de détails sur les régions et endpoints pris en charge, consultez Régions et endpoints.

  • Les identifiants d'accès sont obtenus à partir des variables d'environnement. Pour plus d'informations, consultez Configuration des identifiants d'accès.

  • Cette rubrique montre comment créer une instance OSSClient avec un endpoint OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Configuration du client.

  • Pour interroger la capacité de stockage d'un bucket, vous devez disposer de l'autorisation oss:GetBucketStat. Pour plus d'informations, consultez Accorder une politique personnalisée.

Exemple de code

Le code suivant récupère la capacité de stockage d'un bucket nommé examplebucket, ainsi que le nombre et la capacité de stockage des objets pour chaque classe de stockage dans le bucket.

Important

Seules les versions 3.14.1 et ultérieures d'OSS SDK for Java prennent en charge toutes les propriétés incluses dans l'exemple de code ci-dessous.

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

public class Demo {
    public static void main(String[] args) throws Exception {
        // This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint.
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the bucket name. For example, examplebucket.
        String bucketName = "examplebucket";
        // Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.
        String region = "cn-hangzhou";

        // Create an OSSClient instance.
        // When the OSSClient instance is no longer needed, call the shutdown method to release resources.
        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);
            // Get the total storage capacity of the bucket in bytes.
            System.out.println("StorageSize:"+stat.getStorageSize());
            // Get the total number of objects in the bucket.
            System.out.println("ObjectCount:"+stat.getObjectCount());
            // Get the number of multipart upload tasks that are initiated but not completed or aborted.
            System.out.println("MultipartUploadCount:"+stat.getMultipartUploadCount());
            // Get the storage capacity of Standard objects in bytes.
            System.out.println("StandardStorage:"+stat.getStandardStorage());
            // Get the number of Standard objects.
            System.out.println("StandardObjectCount:"+stat.getStandardObjectCount());
            // Get the number of LiveChannels in the bucket.
            System.out.println("LiveChannelCount:"+stat.getLiveChannelCount());
            // The time when the storage information was obtained. The value is a UNIX timestamp in seconds.
            System.out.println("LastModifiedTime:"+stat.getLastModifiedTime());
            // Get the billable storage capacity of Infrequent Access (IA) objects in bytes.
            System.out.println("InfrequentAccessStorage:"+stat.getInfrequentAccessStorage());
            // Get the actual storage capacity of IA objects in bytes.
            System.out.println("InfrequentAccessRealStorage:"+stat.getInfrequentAccessRealStorage());
            // Get the number of IA objects.
            System.out.println("InfrequentAccessObjectCount:"+stat.getInfrequentAccessObjectCount());
            // Get the billable storage capacity of Archive Storage objects in bytes.
            System.out.println("ArchiveStorage:"+stat.getArchiveStorage());
            // Get the actual storage capacity of Archive Storage objects in bytes.
            System.out.println("ArchiveRealStorage:"+stat.getArchiveRealStorage());
            // Get the number of Archive Storage objects.
            System.out.println("ArchiveObjectCount:"+stat.getArchiveObjectCount());
            // Get the billable storage capacity of Cold Archive objects in bytes.
            System.out.println("ColdArchiveStorage:"+stat.getColdArchiveStorage());
            // Get the actual storage capacity of Cold Archive objects in bytes.
            System.out.println("ColdArchiveRealStorage:"+stat.getColdArchiveRealStorage());
            // Get the number of Cold Archive objects.
            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();
            }
        }
    }
}

Références

Pour plus d'informations sur l'opération API permettant de récupérer la capacité de stockage d'un bucket, ainsi que le nombre et la capacité de stockage des objets pour chaque classe de stockage dans le bucket, consultez GetBucketStat.