Obtain bucket information (Swift SDK)

Updated at:

This topic describes how to use the Object Storage Service (OSS) Swift software development kit (SDK) to obtain information about a specified bucket. This information includes its access tracking status, region, creation date, access control list (ACL), owner's name and ID, storage class, data disaster recovery type, public endpoint, internal same-region endpoint, cross-region replication status, versioning status, and encryption method.

Prerequisites

  • The sample code in this topic uses the region ID cn-hangzhou for China (Hangzhou) as an example. By default, the public endpoint is used. If you want to access OSS from other Alibaba Cloud products in the same region, use the internal endpoint. For more information about the mappings between OSS-supported regions and endpoints, see Regions and Endpoints.

  • To obtain bucket information, you must have the oss:GetBucketInfo permission. For more information, see Grant custom permissions to a Resource Access Management (RAM) user.

Sample code

Use the following code to obtain bucket information.

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 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 OSS client parameters.
            let config = Configuration.default()
                .withRegion(region) // Set the region.
                .withCredentialsProvider(credentialsProvider) // Set the credentials provider.
                
            // Set the endpoint.
            if let endpoint = endpoint {
                config.withEndpoint(endpoint)
            }

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

            // Get the bucket information.
            let result = try await client.getBucketInfo(
                GetBucketInfoRequest(
                    bucket: bucket
                )
            )

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

        } catch {
            // Catch and handle exceptions.
            print("error:\n\(error)")
        }
    }
}

Common bucket information

Parameter

Description

BucketInfo.Name

Bucket name

BucketInfo.AccessMonitor

Access tracking status of the bucket

BucketInfo.Location

Region where the bucket is located

BucketInfo.CreationDate

Creation date of the bucket

BucketInfo.ExtranetEndpoint

Public endpoint of the bucket

BucketInfo.IntranetEndpoint

Internal endpoint for access from an ECS instance in the same region

BucketInfo.ACL

ACL of the bucket

BucketInfo.RedundancyType

Data disaster recovery type of the bucket

BucketInfo.Owner

Contains the following parameters:

  • BucketInfo.Owner.ID: User ID of the bucket owner

  • BucketInfo.Owner.DisplayName: Name of the bucket owner

BucketInfo.StorageClass

Storage class of the bucket

BucketInfo.SseRule

Contains the following parameters:

  • BucketInfo.SseRule.KMSDataEncryption: Indicates whether Key Management Service (KMS) encryption is used

  • BucketInfo.SseRule.KMSMasterKeyID: ID of the KMS key in use

  • BucketInfo.SseRule.SSEAlgorithm: Default server-side encryption method

BucketInfo.Versioning

Versioning status of the bucket

BucketInfo.CrossRegionReplication

Cross-region replication status of the bucket

References

  • For more information about buckets, see Bucket overview.

  • For the complete sample code that shows how to obtain bucket information, see the example on GitHub.

  • For more information about the API operation to obtain bucket information, see GetBucketInfo.