|
| 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.streaming.imports] |
| 8 | +import AWSS3 |
| 9 | +import S3TransferManager |
| 10 | +import Foundation |
| 11 | +// snippet-end:[swift.s3tm.streaming.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.streaming.config-create] |
| 25 | + let s3Config = try await S3Client.S3ClientConfiguration( |
| 26 | + region: region |
| 27 | + ) |
| 28 | + |
| 29 | + // Create an S3TransferManager object. |
| 30 | + |
| 31 | + let s3tmConfig = try await S3TransferManagerConfig( |
| 32 | + s3ClientConfig: s3Config, // Configuration for the S3Client |
| 33 | + targetPartSizeBytes: 16 * 1024 * 1024, // 16 MB part size |
| 34 | + multipartUploadThresholdBytes: 128 * 1024 * 1024, // 128 MB threshold |
| 35 | + multipartDownloadType: .part |
| 36 | + ) |
| 37 | + |
| 38 | + let s3tm = S3TransferManager(config: s3tmConfig) |
| 39 | + // snippet-end:[swift.s3tm.streaming.config-create] |
| 40 | + |
| 41 | + // Create a listener for events from the download of the bucket, to |
| 42 | + // monitor the overall state of the download. |
| 43 | + |
| 44 | + // snippet-start:[swift.s3tm.streaming.bucket-listener] |
| 45 | + let downloadBucketStreamingTransferListener = DownloadBucketStreamingTransferListener() |
| 46 | + |
| 47 | + Task { |
| 48 | + for try await downloadBucketTransferEvent in downloadBucketStreamingTransferListener.eventStream { |
| 49 | + switch downloadBucketTransferEvent { |
| 50 | + case .initiated(let input, _): |
| 51 | + print("Download of bucket \(input.bucket) started...") |
| 52 | + |
| 53 | + case .complete(let input, _, let snapshot): |
| 54 | + print("Download of bucket \(input.bucket) complete. Downloaded \(snapshot.transferredFiles) files.") |
| 55 | + downloadBucketStreamingTransferListener.closeStream() |
| 56 | + |
| 57 | + case .failed(let input, let snapshot): |
| 58 | + print("*** Download of bucket \(input.bucket) failed after downloading \(snapshot.transferredFiles) files.") |
| 59 | + downloadBucketStreamingTransferListener.closeStream() |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + // snippet-end:[swift.s3tm.streaming.bucket-listener] |
| 64 | + |
| 65 | + // Create the directory to download the bucket into. The new directory |
| 66 | + // is placed into the user's Downloads folder and has the same name as |
| 67 | + // the bucket being downloaded. |
| 68 | + |
| 69 | + guard let downloadsDirectory = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first else { |
| 70 | + print("*** Unable to locate the Downloads directory.") |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + let targetDirectory = downloadsDirectory.appending(component: bucketName, directoryHint: .isDirectory) |
| 75 | + try FileManager.default.createDirectory(at: targetDirectory, withIntermediateDirectories: true) |
| 76 | + |
| 77 | + // Start downloading the bucket by calling S3TransferManager.downloadBucket(input:). |
| 78 | + |
| 79 | + // snippet-start:[swift.s3tm.streaming.downloadBucket] |
| 80 | + let downloadBucketTask = try s3tm.downloadBucket( |
| 81 | + input: DownloadBucketInput( |
| 82 | + bucket: bucketName, |
| 83 | + destination: targetDirectory, |
| 84 | + // The listener for the overall bucket download process. |
| 85 | + directoryTransferListeners: [downloadBucketStreamingTransferListener], |
| 86 | + // A factory that creates a listener for each file being downloaded. |
| 87 | + objectTransferListenerFactory: { |
| 88 | + let objectListener = DownloadObjectStreamingTransferListener() |
| 89 | + |
| 90 | + Task { |
| 91 | + for try await downloadObjectTransferEvent in objectListener.eventStream { |
| 92 | + switch downloadObjectTransferEvent { |
| 93 | + // The download of a file has begun. |
| 94 | + case .initiated(let input, _): |
| 95 | + print(" Downloading file \(input.key)...") |
| 96 | + |
| 97 | + // The number of bytes received so far has been updated. |
| 98 | + case .bytesTransferred(let input, let snapshot): |
| 99 | + print(" Transferred \(snapshot.transferredBytes) total bytes of file \(input.key)...") |
| 100 | + |
| 101 | + // A file download has completed. |
| 102 | + case .complete(let input, _, let snapshot): |
| 103 | + print(" Finished downloading file \(input.key) (\(snapshot.transferredBytes) bytes).") |
| 104 | + objectListener.closeStream() |
| 105 | + |
| 106 | + // The download of the file has failed. |
| 107 | + case .failed(let input, let snapshot): |
| 108 | + print("*** Download of file \(input.key) failed after \(snapshot.transferredBytes) bytes.") |
| 109 | + objectListener.closeStream() |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return [ |
| 115 | + objectListener |
| 116 | + ] |
| 117 | + } |
| 118 | + ) |
| 119 | + ) |
| 120 | + // snippet-end:[swift.s3tm.streaming.downloadBucket] |
| 121 | + |
| 122 | + // Wait for the bucket to finish downloading, then display the results. |
| 123 | + |
| 124 | + // snippet-start:[swift.s3tm.streaming.wait-for-download] |
| 125 | + do { |
| 126 | + let downloadBucketOutput = try await downloadBucketTask.value |
| 127 | + print("Total files downloaded: \(downloadBucketOutput.objectsDownloaded)") |
| 128 | + print("Number of failed downloads: \(downloadBucketOutput.objectsFailed)") |
| 129 | + } catch { |
| 130 | + print("*** Error downloading the bucket: \(error.localizedDescription)") |
| 131 | + } |
| 132 | + // snippet-end:[swift.s3tm.streaming.wait-for-download] |
| 133 | + } |
| 134 | +} |
0 commit comments