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
| Field | Description |
|---|---|
Storage | Total storage capacity of the bucket, in bytes. |
ObjectCount | Total number of objects in the bucket. |
MultipartUploadCount | Number of multipart uploads that have been initiated but not completed or aborted. |
LiveChannelCount | Number of LiveChannels in the bucket. |
LastModifiedTime | Time when the storage statistics were collected, as a UNIX timestamp in seconds. |
StandardStorage | Storage capacity of Standard objects, in bytes. |
StandardObjectCount | Number 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.
| Field | Description |
|---|---|
InfrequentAccessStorage | Billable storage capacity of Infrequent Access objects, in bytes. |
InfrequentAccessRealStorage | Actual storage capacity of Infrequent Access objects, in bytes. |
InfrequentAccessObjectCount | Number of Infrequent Access objects. |
ArchiveStorage | Billable storage capacity of Archive objects, in bytes. |
ArchiveRealStorage | Actual storage capacity of Archive objects, in bytes. |
ArchiveObjectCount | Number of Archive objects. |
ColdArchiveStorage | Billable storage capacity of Cold Archive objects, in bytes. |
ColdArchiveRealStorage | Actual storage capacity of Cold Archive objects, in bytes. |
ColdArchiveObjectCount | Number of Cold Archive objects. |
What's next
For the full API reference, see GetBucketStat.