Aws Sdk Swift Usage

作者 aws0d6167ad2e6d無授權條款2.8K 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫今天更新

AWS SDK for Swift development patterns. Use when writing Swift code that uses AWS services via aws-sdk-swift package.

AI 產生的概覽

使用 AWS SDK for Swift 撰寫 Swift 程式的參考模式。

功能
提供使用 AWS SDK for Swift 的程式撰寫模式與範例,涵蓋非同步進入點、以結構為基礎的用戶端設定、憑證解析器、等待器、分頁、預簽章 URL 以及常見的 S3 操作。它是僅含說明的參考資料,附帶程式碼片段,而不是產生檔案的軟體。內容強調正確的設定型別與參數順序。
適用情境
在撰寫或審查透過 aws-sdk-swift 套件呼叫 AWS 服務的 Swift 程式時使用。它有助於用戶端設定、憑證、分頁和常見服務操作。
執行需求
此技能不附帶指令碼或套件,僅為說明性內容。套用這些模式需要 Swift 工具鏈與 aws-sdk-swift 套件,實際呼叫服務還需要 AWS 憑證和網路存取。

AWS SDK for Swift

Async Code Structure

All SDK operations are async. Use @main entry point:

swift
@mainstruct Main {    static func main() async throws {        let client = try await S3Client()        // ... async operations    }}

CRITICAL: Use Struct Config Types

NEVER use S3ClientConfiguration or DynamoDBClientConfiguration - these are DEPRECATED classes.

ALWAYS use the struct-based config types:

  • S3Client.S3ClientConfig (not S3ClientConfiguration)
  • DynamoDBClient.DynamoDBClientConfig (not DynamoDBClientConfiguration)
  • STSClient.STSClientConfig (not STSClientConfiguration)

Config parameters MUST be in declaration order. Region is ALWAYS required when creating a config. Check the service client source for exact order.

swift
// CORRECT - struct configlet config = try await S3Client.S3ClientConfig(region: "us-west-2")let client = S3Client(config: config)
// WRONG - deprecated class// let config = try await S3Client.S3ClientConfiguration(region: "us-west-2")

Client Creation

All service clients follow the same pattern: <Service>Client with <Service>Client.<Service>ClientConfig.

Model types (structs/enums used in requests/responses) are namespaced under <Service>ClientTypes:

  • S3ClientTypes.Bucket, S3ClientTypes.Object
  • DynamoDBClientTypes.AttributeValue
  • CloudWatchClientTypes.MetricDatum, CloudWatchClientTypes.Dimension
swift
import AWSS3import AWSDynamoDB
// Simple - auto-detects regionlet s3 = try await S3Client()let dynamo = try await DynamoDBClient()
// With regionlet s3 = try S3Client(region: "us-west-2")
// With config - parameters must be in declaration orderlet config = try await S3Client.S3ClientConfig(    useFIPS: true,    awsRetryMode: .adaptive,    maxAttempts: 5,    region: "us-west-2")let client = S3Client(config: config)
// With custom endpoint and credentialslet config = try await S3Client.S3ClientConfig(    awsCredentialIdentityResolver: resolver,    region: "us-west-2",    endpoint: "https://s3.custom-endpoint.com")

Common config parameters (MUST follow declaration order):

  • awsCredentialIdentityResolver - Custom credentials

  • useFIPS - Enable FIPS endpoints

  • useDualStack - Enable dual-stack endpoints

  • awsRetryMode - Retry strategy (.adaptive, .standard, .legacy)

  • maxAttempts - Max retry attempts

  • region - AWS region

  • httpClientEngine - Custom HTTP client (requires HttpClientConfiguration parameter):

    swift
    import ClientRuntimelet httpConfig = HttpClientConfiguration()let httpClient = URLSessionHTTPClient(httpClientConfiguration: httpConfig)let config = try await S3Client.S3ClientConfig(    region: "us-east-1",    httpClientEngine: httpClient)
  • endpoint - Custom endpoint URL

For service-specific config options or exact parameter order, check Sources/Services/AWS<Service>/Sources/AWS<Service>/<Service>Client.swift in the SDK.

Credential Resolvers

swift
import AWSSDKIdentityimport SmithyIdentity
// Static credentials - pass credential object directlylet creds = AWSCredentialIdentity(accessKey: "AKIA...", secret: "...")let resolver = StaticAWSCredentialIdentityResolver(creds)
// Assume role - REQUIRES underlying resolverlet underlying = try DefaultAWSCredentialIdentityResolverChain()let resolver = try STSAssumeRoleAWSCredentialIdentityResolver(    awsCredentialIdentityResolver: underlying,    roleArn: "arn:aws:iam::123456789012:role/MyRole",    sessionName: "session-name")
// Use in configlet config = try await S3Client.S3ClientConfig(    awsCredentialIdentityResolver: resolver,    region: "us-west-2")

Waiters

Import SmithyWaitersAPI. WaiterOptions requires maxWaitTime parameter:

swift
import AWSS3import SmithyWaitersAPI
let client = try await S3Client()_ = try await client.waitUntilBucketExists(    options: WaiterOptions(maxWaitTime: 120.0),    input: HeadBucketInput(bucket: "my-bucket"))

Pagination

swift
let input = ListObjectsV2Input(bucket: "my-bucket")for try await page in client.listObjectsV2Paginated(input: input) {    for object in page.contents ?? [] {        print(object.key ?? "")    }}

Presigned URLs

swift
let url = try await client.presignedURLForGetObject(    input: GetObjectInput(bucket: "my-bucket", key: "file.pdf"),    expiration: 3600)

Common Operations

swift
// Put object_ = try await client.putObject(input: PutObjectInput(    body: .data(data),    bucket: "bucket",    key: "key"))
// Get objectlet output = try await client.getObject(input: GetObjectInput(bucket: "bucket", key: "key"))let data = try await output.body?.readData()
// List bucketslet response = try await client.listBuckets(input: ListBucketsInput())for bucket in response.buckets ?? [] {    print(bucket.name ?? "")}

來源與署名

來源:aws/agent-toolkit-for-aws位於plugins/aws-core/skills/aws-sdk-swift-usage提交0d6167a

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架