All Products
Search
Document Center

Object Storage Service:Obtain the storage capacity of a bucket (Swift SDK)

Last Updated:Mar 20, 2026

Use getBucketStat to retrieve storage statistics for a bucket — the total capacity, object counts, and per-storage-class breakdowns.

Sample code

The following example calls getBucketStat and prints the response.

import AlibabaCloudOSS
import Foundation

@main
struct Main {
    static func main() async {
        do {
            // Specify the region where the bucket is located. For example, set the region to cn-hangzhou for China (Hangzhou).
            let region = "cn-hangzhou"
            // Specify the bucket name.
            let bucket = "yourBucketName"
            // Optional. Specify the domain name used to access OSS. For example, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou).
            let endpoint: String? = nil

            // Load credentials from environment variables. You must set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET in advance.
            let credentialsProvider = EnvironmentCredentialsProvider()

            // Configure the OSS client parameters.
            let config = Configuration.default()
                .withRegion(region)
                .withCredentialsProvider(credentialsProvider)

            // Set the endpoint.
            if let endpoint = endpoint {
                config.withEndpoint(endpoint)
            }

            // Create an OSS client instance.
            let client = Client(config)

            // Retrieve storage statistics for the bucket.
            let result = try await client.getBucketStat(
                GetBucketStatRequest(
                    bucket: bucket
                )
            )

            // Print the operation result.
            print("result:\n\(result)")

        } catch {
            print("error:\n\(error)")
        }
    }
}

For the complete sample, see GitHub.

Note: The sample code uses China (Hangzhou) (region ID: cn-hangzhou) as an example and the public endpoint by default. To access OSS from other Alibaba Cloud products in the same region, use the internal endpoint. For more information, see Regions and endpoints.

Response fields

General fields

FieldDescription
StorageTotal storage capacity of the bucket, in bytes.
ObjectCountTotal number of objects in the bucket.
MultipartUploadCountNumber of multipart uploads that have been initiated but not completed or aborted.
LiveChannelCountNumber of LiveChannels in the bucket.
LastModifiedTimeTime when the storage statistics were collected, as a UNIX timestamp in seconds.
StandardStorageStorage capacity of Standard objects, in bytes.
StandardObjectCountNumber of Standard objects.

Per-storage-class fields

For Infrequent Access (IA), Archive, and Cold Archive storage classes, OSS returns two capacity values: billable and actual. The billable capacity may exceed the actual capacity because OSS charges a minimum storage size for small objects in these classes.

FieldDescription
InfrequentAccessStorageBillable storage capacity of Infrequent Access objects, in bytes.
InfrequentAccessRealStorageActual storage capacity of Infrequent Access objects, in bytes.
InfrequentAccessObjectCountNumber of Infrequent Access objects.
ArchiveStorageBillable storage capacity of Archive objects, in bytes.
ArchiveRealStorageActual storage capacity of Archive objects, in bytes.
ArchiveObjectCountNumber of Archive objects.
ColdArchiveStorageBillable storage capacity of Cold Archive objects, in bytes.
ColdArchiveRealStorageActual storage capacity of Cold Archive objects, in bytes.
ColdArchiveObjectCountNumber of Cold Archive objects.

What's next