Skip to content

Commit 3c8eb77

Browse files
authored
Swift: Add pagination example (#6949)
1 parent fae5994 commit 3c8eb77

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// swift-tools-version:5.9
2+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// The swift-tools-version declares the minimum version of Swift required to
6+
// build this package.
7+
8+
import PackageDescription
9+
10+
let package = Package(
11+
name: "pagination",
12+
// Let Xcode know the minimum Apple platforms supported.
13+
platforms: [
14+
.macOS(.v11),
15+
.iOS(.v13)
16+
],
17+
dependencies: [
18+
// Dependencies declare other packages that this package depends on.
19+
.package(
20+
url: "https://github.com/awslabs/aws-sdk-swift",
21+
from: "1.0.0"
22+
),
23+
],
24+
targets: [
25+
// Targets are the basic building blocks of a package, defining a module or a test suite.
26+
// Targets can depend on other targets in this package and products from dependencies.
27+
.executableTarget(
28+
name: "retry",
29+
dependencies: [
30+
.product(name: "AWSS3", package: "aws-sdk-swift"),
31+
],
32+
path: "Sources"),
33+
]
34+
)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
// An example to demonstrate the use of pagination with the AWS SDK
5+
// for Swift.
6+
7+
import AWSS3
8+
import Foundation
9+
10+
@main
11+
struct PaginatorExample {
12+
static func main() async {
13+
// snippet-start:[swift.pagination]
14+
let PAGE_SIZE = 10
15+
let client: S3Client
16+
17+
// Create the Amazon S3 client.
18+
19+
do {
20+
client = try await S3Client()
21+
} catch {
22+
print("ERROR: Unable to create the Amazon S3 client.")
23+
return
24+
}
25+
26+
// Start pagination by using the `Paginated` version of the
27+
// `listBuckets(input:)` function. Each page has up to 10 buckets in
28+
// it.
29+
30+
// snippet-start:[swift.create-paginator]
31+
let pages = client.listBucketsPaginated(
32+
input: ListBucketsInput(maxBuckets: PAGE_SIZE)
33+
)
34+
// snippet-end:[swift.create-paginator]
35+
36+
// Go through the pages, printing each page's buckets to the console.
37+
// The paginator handles the continuation tokens automatically.
38+
39+
// snippet-start:[swift.process-paginator]
40+
var pageNumber = 0
41+
42+
do {
43+
for try await page in pages {
44+
pageNumber += 1
45+
46+
guard let pageBuckets = page.buckets else {
47+
print("ERROR: No buckets returned in page \(pageNumber)")
48+
continue
49+
}
50+
51+
print("\nPage \(pageNumber):")
52+
53+
// Print this page's bucket names.
54+
55+
for bucket in pageBuckets {
56+
print(" " + (bucket.name ?? "<unknown>"))
57+
}
58+
}
59+
} catch {
60+
print("ERROR: Unable to process bucket list pages.")
61+
}
62+
// snippet-end:[swift.process-paginator]
63+
}
64+
// snippet-end:[swift.pagination]
65+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
echo "No automated tests available."

0 commit comments

Comments
 (0)