-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathentry.swift
More file actions
79 lines (70 loc) · 2.67 KB
/
entry.swift
File metadata and controls
79 lines (70 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
/// SPDX-License-Identifier: Apache-2.0
///
/// A simple example that shows how to use the AWS SDK for Swift with the
/// Amazon Simple Storage Service (Amazon S3) function `ListBuckets`.
// snippet-start:[s3.swift.intro.imports]
import AWSClientRuntime
import AWSS3
import Foundation
// snippet-end:[s3.swift.intro.imports]
// snippet-start:[s3.swift.intro.getbucketnames]
// Return an array containing the names of all available buckets.
//
// - Returns: An array of strings listing the buckets.
func getBucketNames() async throws -> [String] {
do {
// Get an S3Client with which to access Amazon S3.
// snippet-start:[s3.swift.intro.client-init]
let configuration = try await S3Client.S3ClientConfiguration()
// configuration.region = "us-east-2" // Uncomment this to set the region programmatically.
let client = S3Client(config: configuration)
// snippet-end:[s3.swift.intro.client-init]
// snippet-start:[s3.swift.intro.listbuckets_getnames]
// Use "Paginated" to get all the buckets.
// This lets the SDK handle the 'continuationToken' in "ListBucketsOutput".
let pages = client.listBucketsPaginated(
input: ListBucketsInput( maxBuckets: 10)
)
// snippet-end:[s3.swift.intro.listbuckets_getnames]
// Get the bucket names.
var bucketNames: [String] = []
do {
for try await page in pages {
guard let buckets = page.buckets else {
print("Error: no buckets returned.")
continue
}
for bucket in buckets {
bucketNames.append(bucket.name ?? "<unknown>")
}
}
return bucketNames
} catch {
print("ERROR: listBuckets:", dump(error))
throw error
}
}
}
// snippet-end:[s3.swift.intro.getbucketnames]
/// The program's asynchronous entry point.
// snippet-start:[s3.swift.intro.main]
@main
struct Main {
static func main() async {
do {
let names = try await getBucketNames()
print("Found \(names.count) buckets:")
for name in names {
print(" \(name)")
}
} catch let error as AWSServiceError {
print("An Amazon S3 service error occurred: \(error.message ?? "No details available")")
} catch {
print("An unknown error occurred: \(dump(error))")
}
}
}
// snippet-end:[s3.swift.intro.main]