Query endpoint information (Swift SDK)

更新时间:
复制 MD 格式

Use the OSS SDK for Swift V2 to query endpoint information for OSS-supported regions — either all regions at once or a specific region. Each region entry includes public endpoints (IPv4), internal endpoints for classic network or Virtual Private Cloud (VPC) access, and acceleration endpoints for global upload and download acceleration.

Usage notes

  • Endpoint information is available for all regions where OSS is supported, regardless of whether you have a bucket in that region.

  • The examples below use the China (Hangzhou) region (cn-hangzhou) and a public endpoint by default. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. For region-to-endpoint mappings, see Regions and endpoints.

Prerequisites

Before you begin, ensure that you have:

  • OSS SDK for Swift V2 installed

  • The OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables set with a valid AccessKey ID and AccessKey secret

Query endpoints for all regions

Call describeRegions without a region parameter to retrieve endpoint information for all OSS-supported regions.

import AlibabaCloudOSS
import Foundation

@main
struct Main {
    static func main() async {

        do {
            // Specify the region where the bucket is located. Example: China (Hangzhou) is cn-hangzhou.
            let region = "cn-hangzhou"
            // Optional. Specify the endpoint to access OSS. For example, for China (Hangzhou), set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
            let endpoint: String? = nil

            // Read access credentials from environment variables.
            // Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this code.
            let credentialsProvider = EnvironmentCredentialsProvider()

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

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

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

            // Query endpoint information for all supported regions.
            let result = try await client.describeRegions(
                DescribeRegionsRequest()
            )
            print("result:\n\(result)")

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

What's next