Skip to content

Commit 5c32a00

Browse files
committed
Revamp and enhance S3TM examples
1 parent 697e9d9 commit 5c32a00

File tree

8 files changed

+169
-7
lines changed

8 files changed

+169
-7
lines changed

swift/example_code/s3-transfer-manager/download-streaming/Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import PackageDescription
99

1010
let package = Package(
11-
name: "downloadbucket",
11+
name: "download-streaming",
1212
// Let Xcode know the minimum Apple platforms supported.
1313
platforms: [
1414
.macOS(.v13),
@@ -32,7 +32,7 @@ let package = Package(
3232
// Targets can depend on other targets in this package and products
3333
// from dependencies.
3434
.executableTarget(
35-
name: "downloadbucket",
35+
name: "download-streaming",
3636
dependencies: [
3737
.product(name: "AWSS3", package: "aws-sdk-swift"),
3838
.product(name: "S3TransferManager", package: "aws-sdk-swift-s3-transfer-manager"),

swift/example_code/s3-transfer-manager/download-streaming/Sources/Example.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class Example {
131131
print("Number of failed downloads: \(downloadBucketOutput.objectsFailed)")
132132
} catch {
133133
print("*** Error downloading the bucket: \(error.localizedDescription)")
134+
dump(error)
134135
}
135136
// snippet-end:[swift.s3tm.streaming.wait-for-download]
136137
}

swift/example_code/s3-transfer-manager/download-streaming/Sources/entry.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct ExampleCommand: ParsableCommand {
1313
var s3Prefix: String?
1414

1515
static var configuration = CommandConfiguration(
16-
commandName: "downloadbucket",
16+
commandName: "download-streaming",
1717
abstract: """
1818
Downloads a bucket from Amazon S3 using the S3 Transfer Manager, with progress updates.
1919
""",
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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: "get-bucket",
12+
// Let Xcode know the minimum Apple platforms supported.
13+
platforms: [
14+
.macOS(.v13),
15+
],
16+
dependencies: [
17+
// Dependencies declare other packages that this package depends on.
18+
.package(
19+
url: "https://github.com/awslabs/aws-sdk-swift",
20+
from: "1.4.0"),
21+
.package(
22+
url: "https://github.com/aws/aws-sdk-swift-s3-transfer-manager.git",
23+
branch: "main"
24+
),
25+
.package(
26+
url: "https://github.com/apple/swift-argument-parser.git",
27+
branch: "main"
28+
)
29+
],
30+
targets: [
31+
// Targets are the basic building blocks of a package, defining a module or a test suite.
32+
// Targets can depend on other targets in this package and products
33+
// from dependencies.
34+
.executableTarget(
35+
name: "get-bucket",
36+
dependencies: [
37+
.product(name: "AWSS3", package: "aws-sdk-swift"),
38+
.product(name: "S3TransferManager", package: "aws-sdk-swift-s3-transfer-manager"),
39+
.product(name: "ArgumentParser", package: "swift-argument-parser")
40+
],
41+
path: "Sources")
42+
43+
]
44+
)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
// The main code for the streaming bucket download example for the
5+
// S3 Transfer Manager in the AWS SDK for Swift.
6+
7+
// snippet-start:[swift.s3tm.getbucket.imports]
8+
import AWSS3
9+
import S3TransferManager
10+
import Foundation
11+
// snippet-end:[swift.s3tm.getbucket.imports]
12+
13+
class Example {
14+
let region: String
15+
let bucketName: String
16+
17+
init(region: String, bucket: String) {
18+
self.region = region
19+
self.bucketName = bucket
20+
}
21+
22+
/// The body of the example.
23+
func run() async throws {
24+
// snippet-start:[swift.s3tm.getbucket.config-create]
25+
// Create an S3ClientConfiguration object.
26+
let s3Config = try await S3Client.S3ClientConfiguration(
27+
region: region
28+
)
29+
30+
// Create an S3TransferManager using the S3 configuration.
31+
32+
let s3tmConfig = try await S3TransferManagerConfig(
33+
s3ClientConfig: s3Config
34+
)
35+
36+
let s3tm = S3TransferManager(config: s3tmConfig)
37+
// snippet-end:[swift.s3tm.getbucket.config-create]
38+
39+
// Create the directory to download the bucket into. The new directory
40+
// is placed into the user's Downloads folder and has the same name as
41+
// the bucket being downloaded.
42+
43+
guard let downloadsDirectory = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first else {
44+
print("*** Unable to locate the Downloads directory.")
45+
return
46+
}
47+
48+
let targetDirectory = downloadsDirectory.appending(component: bucketName, directoryHint: .isDirectory)
49+
try FileManager.default.createDirectory(at: targetDirectory, withIntermediateDirectories: true)
50+
51+
// Start downloading the bucket by calling S3TransferManager.downloadBucket(input:).
52+
53+
// snippet-start:[swift.s3tm.getbucket.downloadBucket]
54+
let downloadBucketTask = try s3tm.downloadBucket(
55+
input: DownloadBucketInput(
56+
bucket: bucketName,
57+
destination: targetDirectory
58+
)
59+
)
60+
61+
do {
62+
let downloadBucketOutput = try await downloadBucketTask.value
63+
print("Total files downloaded: \(downloadBucketOutput.objectsDownloaded)")
64+
print("Number of failed downloads: \(downloadBucketOutput.objectsFailed)")
65+
} catch {
66+
print("*** Error downloading the bucket: \(error.localizedDescription)")
67+
}
68+
// snippet-end:[swift.s3tm.getbucket.downloadBucket]
69+
}
70+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import ArgumentParser
5+
import Foundation
6+
7+
struct ExampleCommand: ParsableCommand {
8+
@Option(help: "AWS Region name")
9+
var region = "us-east-1"
10+
@Argument(help: "Name of the Amazon S3 bucket to download")
11+
var bucketName: String
12+
13+
static var configuration = CommandConfiguration(
14+
commandName: "get-bucket",
15+
abstract: """
16+
Quietly fetch an entire bucket from Amazon S3.
17+
""",
18+
discussion: """
19+
"""
20+
)
21+
22+
/// Called by ``main()`` to do the actual running of the AWS
23+
/// example.
24+
func runAsync() async throws {
25+
let example = Example(region: region, bucket: bucketName)
26+
27+
try await example.run()
28+
}
29+
}
30+
31+
/// The program's asynchronous entry point.
32+
@main
33+
struct Main {
34+
/// The function that serves as the main asynchronous entry point for the
35+
/// example. It parses the command line using the Swift Argument Parser,
36+
/// then calls the `runAsync()` function to run the example itself.
37+
static func main() async {
38+
let args = Array(CommandLine.arguments.dropFirst())
39+
40+
do {
41+
let command = try ExampleCommand.parse(args)
42+
try await command.runAsync()
43+
} catch {
44+
ExampleCommand.exit(withError: error)
45+
}
46+
}
47+
}

swift/example_code/s3-transfer-manager/upload-file/Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import PackageDescription
1515

1616
let package = Package(
17-
name: "putfile",
17+
name: "upload-file",
1818
// Let Xcode know the minimum Apple platforms supported.
1919
platforms: [
2020
.macOS(.v13),
@@ -38,7 +38,7 @@ let package = Package(
3838
// Targets can depend on other targets in this package and products
3939
// from dependencies.
4040
.executableTarget(
41-
name: "putfile",
41+
name: "upload-file",
4242
dependencies: [
4343
.product(name: "AWSS3", package: "aws-sdk-swift"),
4444
.product(name: "S3TransferManager", package: "aws-sdk-swift-s3-transfer-manager"),

swift/example_code/s3-transfer-manager/upload-file/Sources/entry.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ struct ExampleCommand: ParsableCommand {
1111
var bucketName: String
1212

1313
static var configuration = CommandConfiguration(
14-
commandName: "putfile",
14+
commandName: "upload-file",
1515
abstract: """
16-
Writes a file into an Amazon S3 bucket using the S3 Transfer Manager.
16+
Uploads a file into an Amazon S3 bucket using the S3 Transfer Manager.
1717
""",
1818
discussion: """
1919
"""

0 commit comments

Comments
 (0)