All Products
Search
Document Center

Object Storage Service:Query the storage capacity of a bucket

Last Updated:Sep 06, 2023

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

Usage notes

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

  • 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 Initialization.

Sample code

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:

Important

OSS SDK for Go 2.2.5 and later support all attributes that are included in the following sample code:

package main

import (
    "fmt"
    "os"
    "io/ioutil"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
    /// 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. 
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create an OSSClient instance. 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Specify the name of the bucket. Example: examplebucket. 
    stat, err := client.GetBucketStat("examplebucket")
    if err != nil {
        HandleError(err)
    }
    // Query the total storage capacity of the bucket. Unit: bytes. 
    fmt.Println("Bucket Stat Storage:", stat.Storage)
    // Query the total number of objects that are stored in the bucket. 
    fmt.Println("Bucket Stat Object Count:", stat.ObjectCount)
    // Query the number of multipart upload tasks that have been initiated but are not completed or canceled. 
    fmt.Println("Bucket Stat Multipart Upload Count:", stat.MultipartUploadCount)
    // Query the number of LiveChannels in the bucket. 
    fmt.Println("Bucket Stat Live Channel Count:", stat.LiveChannelCount)
    // Query the time when the obtained information was last modified. The value is a UNIX timestamp. Unit: seconds. 
    fmt.Println("Bucket Stat Last Modified Time:", stat.LastModifiedTime)
    // Query the storage usage of Standard objects in the bucket. Unit: bytes. 
    fmt.Println("Bucket Stat Standard Storage:", stat.StandardStorage)
    // Query the number of Standard objects in the bucket. 
    fmt.Println("Bucket Stat Standard Object Count:", stat.StandardObjectCount)
    // Query the billed storage usage of Infrequent Access (IA) objects in the bucket. Unit: bytes. 
    fmt.Println("Bucket Stat Infrequent Access Storage:", stat.InfrequentAccessStorage)
    // Query the actual storage usage of IA objects in the bucket. Unit: bytes. 
    fmt.Println("Bucket Stat Infrequent Access Real Storage:", stat.InfrequentAccessRealStorage)
    // Query the number of IA objects in the bucket. 
    fmt.Println("Bucket Stat Infrequent Access Object Count:", stat.InfrequentAccessObjectCount)
    // Query the billed storage usage of Archive objects in the bucket. Unit: bytes. 
    fmt.Println("Bucket Stat Archive Storage:", stat.ArchiveStorage)
    // Query the actual storage usage of Archive objects in the bucket. Unit: bytes. 
    fmt.Println("Bucket Stat Archive Real Storage:", stat.ArchiveRealStorage)
    // Query the number of Archive objects in the bucket.    
    fmt.Println("Bucket Stat Archive Object Count:", stat.ArchiveObjectCount)
    // Query the billed storage usage of Cold Archive objects in the bucket. Unit: bytes. 
    fmt.Println("Bucket Stat Cold Archive Storage:", stat.ColdArchiveStorage)
    // Query the actual storage usage of Cold Archive objects in the bucket. Unit: bytes. 
    fmt.Println("Bucket Stat Cold Archive Real Storage:", stat.ColdArchiveRealStorage)
    // Query the number of Cold Archive objects in the bucket. 
    fmt.Println("Bucket Stat Cold Archive Object Count:", stat.ColdArchiveObjectCount)
}

References

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