aws-sdk-swift-usage について
aws-sdk-swift-usageは、AWS SDK for Swiftを使う開発者向けのスキルです。クライアント設定、認証情報、非同期処理、一般的なAPIの使い方を扱い、ページネーション、Waiter、署名付きURLの例も示します。
主な機能
- 01非同期SDK操作のパターン
- 02構造体によるクライアント設定
- 03認証情報リゾルバーの設定
- 04ページネーションとWaiterの利用
- 05署名付きURLの生成
活用シーン
- 01AWSサービスクライアントの作成
- 02リージョンと認証情報の指定
- 03S3オブジェクトのページ単位取得
- 04リソースが利用可能になるまで待機
インストール
コマンドをコピーしてターミナルで実行します。
npx skills add https:/ / github.com/ aws/ agent-toolkit-for-aws --skill aws-sdk-swift-usageインストール後、Claude Code で/aws-sdk-swift-usageを入力して読み込まれたか確認します。
詳細
AWS SDK for Swift
非同期コードの構造
SDK のすべての操作は非同期です。エントリーポイントには @main を使用します。
@main
struct Main {
static func main() async throws {
let client = try await S3Client()
// ... async operations
}
}
重要:構造体の設定型を使用する
S3ClientConfiguration や DynamoDBClientConfiguration は絶対に使用しないでください。これらは非推奨のクラスです。
必ず構造体ベースの設定型を使用してください。
S3Client.S3ClientConfig(S3ClientConfigurationではありません)DynamoDBClient.DynamoDBClientConfig(DynamoDBClientConfigurationではありません)STSClient.STSClientConfig(STSClientConfigurationではありません)
設定パラメーターは、宣言順に指定する必要があります。設定を作成する際は、リージョンが必ず必要です。正確な順序はサービスクライアントのソースを確認してください。
// CORRECT - struct config
let 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")
クライアントの作成
すべてのサービスクライアントは、<Service>Client と <Service>Client.<Service>ClientConfig を使用する同じパターンに従います。
リクエストとレスポンスで使用するモデル型(構造体/列挙型)は、<Service>ClientTypes の下に名前空間化されています。
S3ClientTypes.Bucket、S3ClientTypes.ObjectDynamoDBClientTypes.AttributeValueCloudWatchClientTypes.MetricDatum、CloudWatchClientTypes.Dimension
import AWSS3
import AWSDynamoDB
// Simple - auto-detects region
let s3 = try await S3Client()
let dynamo = try await DynamoDBClient()
// With region
let s3 = try S3Client(region: "us-west-2")
// With config - parameters must be in declaration order
let config = try await S3Client.S3ClientConfig(
useFIPS: true,
awsRetryMode: .adaptive,
maxAttempts: 5,
region: "us-west-2"
)
let client = S3Client(config: config)
// With custom endpoint and credentials
let config = try await S3Client.S3ClientConfig(
awsCredentialIdentityResolver: resolver,
region: "us-west-2",
endpoint: "https://s3.custom-endpoint.com"
)
一般的な設定パラメーター(宣言順に指定する必要があります)。
-
awsCredentialIdentityResolver- カスタム認証情報 -
useFIPS- FIPS エンドポイントを有効にします -
useDualStack- デュアルスタックエンドポイントを有効にします -
awsRetryMode- リトライ戦略(.adaptive、.standard、.legacy) -
maxAttempts- 最大リトライ回数 -
region- AWS リージョン -
httpClientEngine- カスタム HTTP クライアント(HttpClientConfiguration パラメーターが必要です)。import ClientRuntime let httpConfig = HttpClientConfiguration() let httpClient = URLSessionHTTPClient(httpClientConfiguration: httpConfig) let config = try await S3Client.S3ClientConfig( region: "us-east-1", httpClientEngine: httpClient ) -
endpoint- カスタムエンドポイント URL
サービス固有の設定オプションや正確なパラメーター順序については、SDK の Sources/Services/AWS<Service>/Sources/AWS<Service>/<Service>Client.swift を確認してください。
認証情報リゾルバー
import AWSSDKIdentity
import SmithyIdentity
// Static credentials - pass credential object directly
let creds = AWSCredentialIdentity(accessKey: "AKIA...", secret: "...")
let resolver = StaticAWSCredentialIdentityResolver(creds)
// Assume role - REQUIRES underlying resolver
let underlying = try DefaultAWSCredentialIdentityResolverChain()
let resolver = try STSAssumeRoleAWSCredentialIdentityResolver(
awsCredentialIdentityResolver: underlying,
roleArn: "arn:aws:iam::123456789012:role/MyRole",
sessionName: "session-name"
)
// Use in config
let config = try await S3Client.S3ClientConfig(
awsCredentialIdentityResolver: resolver,
region: "us-west-2"
)
ウェイター
SmithyWaitersAPI をインポートします。WaiterOptions には maxWaitTime パラメーターが必要です。
import AWSS3
import SmithyWaitersAPI
let client = try await S3Client()
_ = try await client.waitUntilBucketExists(
options: WaiterOptions(maxWaitTime: 120.0),
input: HeadBucketInput(bucket: "my-bucket")
)
ページネーション
let input = ListObjectsV2Input(bucket: "my-bucket")
for try await page in client.listObjectsV2Paginated(input: input) {
for object in page.contents ?? [] {
print(object.key ?? "")
}
}
署名付き URL
let url = try await client.presignedURLForGetObject(
input: GetObjectInput(bucket: "my-bucket", key: "file.pdf"),
expiration: 3600
)
一般的な操作
// Put object
_ = try await client.putObject(input: PutObjectInput(
body: .data(data),
bucket: "bucket",
key: "key"
))
// Get object
let output = try await client.getObject(input: GetObjectInput(bucket: "bucket", key: "key"))
let data = try await output.body?.readData()
// List buckets
let response = try await client.listBuckets(input: ListBucketsInput())
for bucket in response.buckets ?? [] {
print(bucket.name ?? "")
}