-
Notifications
You must be signed in to change notification settings - Fork 5.8k
S3TM examples for Swift #7626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shepazon
wants to merge
4
commits into
awsdocs:main
Choose a base branch
from
shepazon:feature/s3tm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
S3TM examples for Swift #7626
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
swift/example_code/s3-transfer-manager/download-streaming/Package.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // swift-tools-version: 5.9 | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| // The swift-tools-version declares the minimum version of Swift required to | ||
| // build this package. | ||
|
|
||
| import PackageDescription | ||
|
|
||
| let package = Package( | ||
| name: "download-streaming", | ||
| // Let Xcode know the minimum Apple platforms supported. | ||
| platforms: [ | ||
| .macOS(.v13), | ||
| ], | ||
| dependencies: [ | ||
| // Dependencies declare other packages that this package depends on. | ||
| .package( | ||
| url: "https://github.com/awslabs/aws-sdk-swift", | ||
| from: "1.4.0"), | ||
| .package( | ||
| url: "https://github.com/aws/aws-sdk-swift-s3-transfer-manager.git", | ||
| branch: "main" | ||
| ), | ||
| .package( | ||
| url: "https://github.com/apple/swift-argument-parser.git", | ||
| branch: "main" | ||
| ) | ||
| ], | ||
| targets: [ | ||
| // Targets are the basic building blocks of a package, defining a module or a test suite. | ||
| // Targets can depend on other targets in this package and products | ||
| // from dependencies. | ||
| .executableTarget( | ||
| name: "download-streaming", | ||
| dependencies: [ | ||
| .product(name: "AWSS3", package: "aws-sdk-swift"), | ||
| .product(name: "S3TransferManager", package: "aws-sdk-swift-s3-transfer-manager"), | ||
| .product(name: "ArgumentParser", package: "swift-argument-parser") | ||
| ], | ||
| path: "Sources") | ||
|
|
||
| ] | ||
| ) | ||
138 changes: 138 additions & 0 deletions
138
swift/example_code/s3-transfer-manager/download-streaming/Sources/Example.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| // The main code for the streaming bucket download example for the | ||
| // S3 Transfer Manager in the AWS SDK for Swift. | ||
|
|
||
| // snippet-start:[swift.s3tm.streaming.imports] | ||
| import AWSS3 | ||
| import S3TransferManager | ||
| import Foundation | ||
| // snippet-end:[swift.s3tm.streaming.imports] | ||
|
|
||
| class Example { | ||
| let region: String | ||
| let bucketName: String | ||
| let s3Prefix: String? | ||
|
|
||
| init(region: String, bucket: String, s3Prefix: String?) { | ||
| self.region = region | ||
| self.bucketName = bucket | ||
| self.s3Prefix = s3Prefix | ||
| } | ||
|
|
||
| /// The body of the example. | ||
| func run() async throws { | ||
| // snippet-start:[swift.s3tm.streaming.config-create] | ||
| let s3Config = try await S3Client.S3ClientConfiguration( | ||
| region: region | ||
| ) | ||
|
|
||
| // Create an S3TransferManager object. | ||
|
|
||
| let s3tmConfig = try await S3TransferManagerConfig( | ||
| s3ClientConfig: s3Config, // Configuration for the S3Client | ||
| targetPartSizeBytes: 16 * 1024 * 1024, // 16 MB part size | ||
| multipartUploadThresholdBytes: 128 * 1024 * 1024, // 128 MB threshold | ||
| multipartDownloadType: .part | ||
| ) | ||
|
|
||
| let s3tm = S3TransferManager(config: s3tmConfig) | ||
| // snippet-end:[swift.s3tm.streaming.config-create] | ||
|
|
||
| // Create a listener for events from the download of the bucket, to | ||
| // monitor the overall state of the download. | ||
|
|
||
| // snippet-start:[swift.s3tm.streaming.bucket-listener] | ||
| let downloadBucketStreamingTransferListener = DownloadBucketStreamingTransferListener() | ||
|
|
||
| Task { | ||
| for try await downloadBucketTransferEvent in downloadBucketStreamingTransferListener.eventStream { | ||
| switch downloadBucketTransferEvent { | ||
| case .initiated(let input, _): | ||
| print("Download of bucket \(input.bucket) started...") | ||
|
|
||
| case .complete(let input, _, let snapshot): | ||
| print("Download of bucket \(input.bucket) complete. Downloaded \(snapshot.transferredFiles) files.") | ||
| downloadBucketStreamingTransferListener.closeStream() | ||
|
|
||
| case .failed(let input, let snapshot): | ||
| print("*** Download of bucket \(input.bucket) failed after downloading \(snapshot.transferredFiles) files.") | ||
| downloadBucketStreamingTransferListener.closeStream() | ||
| } | ||
| } | ||
| } | ||
| // snippet-end:[swift.s3tm.streaming.bucket-listener] | ||
|
|
||
| // Create the directory to download the bucket into. The new directory | ||
| // is placed into the user's Downloads folder and has the same name as | ||
| // the bucket being downloaded. | ||
|
|
||
| guard let downloadsDirectory = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first else { | ||
| print("*** Unable to locate the Downloads directory.") | ||
| return | ||
| } | ||
|
|
||
| let targetDirectory = downloadsDirectory.appending(component: bucketName, directoryHint: .isDirectory) | ||
| try FileManager.default.createDirectory(at: targetDirectory, withIntermediateDirectories: true) | ||
|
|
||
| // Start downloading the bucket by calling S3TransferManager.downloadBucket(input:). | ||
|
|
||
| // snippet-start:[swift.s3tm.streaming.downloadBucket] | ||
| let downloadBucketTask = try s3tm.downloadBucket( | ||
| input: DownloadBucketInput( | ||
| bucket: bucketName, | ||
| destination: targetDirectory, | ||
| s3Prefix: s3Prefix, | ||
| // The listener for the overall bucket download process. | ||
| directoryTransferListeners: [downloadBucketStreamingTransferListener], | ||
| // A factory that creates a listener for each file being downloaded. | ||
| objectTransferListenerFactory: { | ||
| let objectListener = DownloadObjectStreamingTransferListener() | ||
|
|
||
| Task { | ||
| for try await downloadObjectTransferEvent in objectListener.eventStream { | ||
| switch downloadObjectTransferEvent { | ||
| // The download of a file has begun. | ||
| case .initiated(let input, _): | ||
| print("Downloading file \(input.key)...") | ||
|
|
||
| // The number of bytes received so far has been updated. | ||
| case .bytesTransferred(let input, let snapshot): | ||
| print(" Transferred \(snapshot.transferredBytes) total bytes of file \(input.key)...") | ||
|
|
||
| // A file download has completed. | ||
| case .complete(let input, _, let snapshot): | ||
| print("Finished downloading file \(input.key) (\(snapshot.transferredBytes) bytes).") | ||
| objectListener.closeStream() | ||
|
|
||
| // The download of the file has failed. | ||
| case .failed(let input, let snapshot): | ||
| print("*** Download of file \(input.key) failed after \(snapshot.transferredBytes) bytes.") | ||
| objectListener.closeStream() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return [ | ||
| objectListener | ||
| ] | ||
| } | ||
| ) | ||
| ) | ||
| // snippet-end:[swift.s3tm.streaming.downloadBucket] | ||
|
|
||
| // Wait for the bucket to finish downloading, then display the results. | ||
|
|
||
| // snippet-start:[swift.s3tm.streaming.wait-for-download] | ||
| do { | ||
| let downloadBucketOutput = try await downloadBucketTask.value | ||
| print("Total files downloaded: \(downloadBucketOutput.objectsDownloaded)") | ||
| print("Number of failed downloads: \(downloadBucketOutput.objectsFailed)") | ||
| } catch { | ||
| print("*** Error downloading the bucket: \(error.localizedDescription)") | ||
| dump(error) | ||
| } | ||
| // snippet-end:[swift.s3tm.streaming.wait-for-download] | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
swift/example_code/s3-transfer-manager/download-streaming/Sources/entry.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import ArgumentParser | ||
| import Foundation | ||
|
|
||
| struct ExampleCommand: ParsableCommand { | ||
| @Option(help: "AWS Region name") | ||
| var region = "us-east-1" | ||
| @Argument(help: "Name of the Amazon S3 bucket to download") | ||
| var bucketName: String | ||
| @Argument(help: "Prefix to download (allows downloading a subset of the bucket)") | ||
| var s3Prefix: String? | ||
|
|
||
| static var configuration = CommandConfiguration( | ||
| commandName: "download-streaming", | ||
| abstract: """ | ||
| Downloads a bucket from Amazon S3 using the S3 Transfer Manager, with progress updates. | ||
| """, | ||
| discussion: """ | ||
| """ | ||
| ) | ||
|
|
||
| /// Called by ``main()`` to do the actual running of the AWS | ||
| /// example. | ||
| func runAsync() async throws { | ||
| let example = Example(region: region, bucket: bucketName, s3Prefix: s3Prefix) | ||
|
|
||
| try await example.run() | ||
| } | ||
| } | ||
|
|
||
| /// The program's asynchronous entry point. | ||
| @main | ||
| struct Main { | ||
| /// The function that serves as the main asynchronous entry point for the | ||
| /// example. It parses the command line using the Swift Argument Parser, | ||
| /// then calls the `runAsync()` function to run the example itself. | ||
| static func main() async { | ||
| let args = Array(CommandLine.arguments.dropFirst()) | ||
|
|
||
| do { | ||
| let command = try ExampleCommand.parse(args) | ||
| try await command.runAsync() | ||
| } catch { | ||
| ExampleCommand.exit(withError: error) | ||
| } | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
swift/example_code/s3-transfer-manager/get-bucket/Package.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // swift-tools-version: 5.9 | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| // The swift-tools-version declares the minimum version of Swift required to | ||
| // build this package. | ||
|
|
||
| import PackageDescription | ||
|
|
||
| let package = Package( | ||
| name: "get-bucket", | ||
| // Let Xcode know the minimum Apple platforms supported. | ||
| platforms: [ | ||
| .macOS(.v13), | ||
| ], | ||
| dependencies: [ | ||
| // Dependencies declare other packages that this package depends on. | ||
| .package( | ||
| url: "https://github.com/awslabs/aws-sdk-swift", | ||
| from: "1.4.0"), | ||
| .package( | ||
| url: "https://github.com/aws/aws-sdk-swift-s3-transfer-manager.git", | ||
| branch: "main" | ||
| ), | ||
| .package( | ||
| url: "https://github.com/apple/swift-argument-parser.git", | ||
| branch: "main" | ||
| ) | ||
| ], | ||
| targets: [ | ||
| // Targets are the basic building blocks of a package, defining a module or a test suite. | ||
| // Targets can depend on other targets in this package and products | ||
| // from dependencies. | ||
| .executableTarget( | ||
| name: "get-bucket", | ||
| dependencies: [ | ||
| .product(name: "AWSS3", package: "aws-sdk-swift"), | ||
| .product(name: "S3TransferManager", package: "aws-sdk-swift-s3-transfer-manager"), | ||
| .product(name: "ArgumentParser", package: "swift-argument-parser") | ||
| ], | ||
| path: "Sources") | ||
|
|
||
| ] | ||
| ) |
70 changes: 70 additions & 0 deletions
70
swift/example_code/s3-transfer-manager/get-bucket/Sources/Example.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| // The main code for the streaming bucket download example for the | ||
| // S3 Transfer Manager in the AWS SDK for Swift. | ||
|
|
||
| // snippet-start:[swift.s3tm.getbucket.imports] | ||
| import AWSS3 | ||
| import S3TransferManager | ||
| import Foundation | ||
| // snippet-end:[swift.s3tm.getbucket.imports] | ||
|
|
||
| class Example { | ||
| let region: String | ||
| let bucketName: String | ||
|
|
||
| init(region: String, bucket: String) { | ||
| self.region = region | ||
| self.bucketName = bucket | ||
| } | ||
|
|
||
| /// The body of the example. | ||
| func run() async throws { | ||
| // snippet-start:[swift.s3tm.getbucket.config-create] | ||
| // Create an S3ClientConfiguration object. | ||
| let s3Config = try await S3Client.S3ClientConfiguration( | ||
| region: region | ||
| ) | ||
|
|
||
| // Create an S3TransferManager using the S3 configuration. | ||
|
|
||
| let s3tmConfig = try await S3TransferManagerConfig( | ||
| s3ClientConfig: s3Config | ||
| ) | ||
|
|
||
| let s3tm = S3TransferManager(config: s3tmConfig) | ||
| // snippet-end:[swift.s3tm.getbucket.config-create] | ||
|
|
||
| // Create the directory to download the bucket into. The new directory | ||
| // is placed into the user's Downloads folder and has the same name as | ||
| // the bucket being downloaded. | ||
|
|
||
| guard let downloadsDirectory = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first else { | ||
| print("*** Unable to locate the Downloads directory.") | ||
| return | ||
| } | ||
|
|
||
| let targetDirectory = downloadsDirectory.appending(component: bucketName, directoryHint: .isDirectory) | ||
| try FileManager.default.createDirectory(at: targetDirectory, withIntermediateDirectories: true) | ||
|
|
||
| // Start downloading the bucket by calling S3TransferManager.downloadBucket(input:). | ||
|
|
||
| // snippet-start:[swift.s3tm.getbucket.downloadBucket] | ||
| let downloadBucketTask = try s3tm.downloadBucket( | ||
| input: DownloadBucketInput( | ||
| bucket: bucketName, | ||
| destination: targetDirectory | ||
| ) | ||
| ) | ||
|
|
||
| do { | ||
| let downloadBucketOutput = try await downloadBucketTask.value | ||
| print("Total files downloaded: \(downloadBucketOutput.objectsDownloaded)") | ||
| print("Number of failed downloads: \(downloadBucketOutput.objectsFailed)") | ||
| } catch { | ||
| print("*** Error downloading the bucket: \(error.localizedDescription)") | ||
| } | ||
| // snippet-end:[swift.s3tm.getbucket.downloadBucket] | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
swift/example_code/s3-transfer-manager/get-bucket/Sources/entry.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import ArgumentParser | ||
| import Foundation | ||
|
|
||
| struct ExampleCommand: ParsableCommand { | ||
| @Option(help: "AWS Region name") | ||
| var region = "us-east-1" | ||
| @Argument(help: "Name of the Amazon S3 bucket to download") | ||
| var bucketName: String | ||
|
|
||
| static var configuration = CommandConfiguration( | ||
| commandName: "get-bucket", | ||
| abstract: """ | ||
| Quietly fetch an entire bucket from Amazon S3. | ||
| """, | ||
| discussion: """ | ||
| """ | ||
| ) | ||
|
|
||
| /// Called by ``main()`` to do the actual running of the AWS | ||
| /// example. | ||
| func runAsync() async throws { | ||
| let example = Example(region: region, bucket: bucketName) | ||
|
|
||
| try await example.run() | ||
| } | ||
| } | ||
|
|
||
| /// The program's asynchronous entry point. | ||
| @main | ||
| struct Main { | ||
| /// The function that serves as the main asynchronous entry point for the | ||
| /// example. It parses the command line using the Swift Argument Parser, | ||
| /// then calls the `runAsync()` function to run the example itself. | ||
| static func main() async { | ||
| let args = Array(CommandLine.arguments.dropFirst()) | ||
|
|
||
| do { | ||
| let command = try ExampleCommand.parse(args) | ||
| try await command.runAsync() | ||
| } catch { | ||
| ExampleCommand.exit(withError: error) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, on building this, I am running into this error:
remote: Repository not found.
fatal: repository 'https://github.com/aws/aws-sdk-swift-s3-transfer-manager.git/' not found
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The link you shared in the first comment also leads to 404 not found. I believe both of these are related.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requested further details from Chen Yoo (Swift SDK)