Query endpoint information (Swift SDK)
This topic describes how to use Swift SDK V2 to query endpoint information for all supported regions or a specific region. This information includes public endpoints (IPv4), internal endpoints for classic network or Virtual Private Cloud (VPC) access, and acceleration endpoints for global upload and download acceleration.
Precautions
-
Endpoint information is available for all regions where Object Storage Service (OSS) is supported, regardless of whether you have a bucket in a specific region.
-
The sample code in this topic uses the China (Hangzhou) region (
cn-hangzhou) as an example. By default, the code uses a public endpoint. If you want to access OSS from other Alibaba Cloud products in the same region, you must use an internal endpoint. For more information about the mappings between OSS regions and endpoints, see Regions and endpoints.
Query endpoint information for all supported regions
The following code shows how to query endpoint information for all 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 domain name to access OSS. For example, for China (Hangzhou), set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
let endpoint: String? = nil
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
let credentialsProvider = EnvironmentCredentialsProvider()
// Configure the OSS client.
let config = Configuration.default()
.withRegion(region) // Set the region where the bucket is located.
.withCredentialsProvider(credentialsProvider) // Set the access credentials.
// 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 the fault.
print("error: \(error)")
}
}
}