Cookie の設定を選択する

当社は、当社のサイトおよびサービスを提供するために必要な必須 Cookie および類似のツールを使用しています。当社は、パフォーマンス Cookie を使用して匿名の統計情報を収集することで、お客様が当社のサイトをどのように利用しているかを把握し、改善に役立てています。必須 Cookie は無効化できませんが、[カスタマイズ] または [拒否] をクリックしてパフォーマンス Cookie を拒否することはできます。

お客様が同意した場合、AWS および承認された第三者は、Cookie を使用して便利なサイト機能を提供したり、お客様の選択を記憶したり、関連する広告を含む関連コンテンツを表示したりします。すべての必須ではない Cookie を受け入れるか拒否するには、[受け入れる] または [拒否] をクリックしてください。より詳細な選択を行うには、[カスタマイズ] をクリックしてください。

Download a stream of unknown size from an Amazon S3 object using an AWS SDK - Amazon Simple Storage Service
このページはお客様の言語に翻訳されていません。 翻訳のリクエスト

Download a stream of unknown size from an Amazon S3 object using an AWS SDK

The following code example shows how to download a stream of unknown size from an Amazon S3 object.

Swift
SDK for Swift
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import ArgumentParser import AWSClientRuntime import AWSS3 import Foundation import Smithy import SmithyHTTPAPI import SmithyStreams /// Download a file from the specified bucket. /// /// - Parameters: /// - bucket: The Amazon S3 bucket name to get the file from. /// - key: The name (or path) of the file to download from the bucket. /// - destPath: The pathname on the local filesystem at which to store /// the downloaded file. func downloadFile(bucket: String, key: String, destPath: String?) async throws { let fileURL: URL // If no destination path was provided, use the key as the name to use // for the file in the downloads folder. if destPath == nil { do { try fileURL = FileManager.default.url( for: .downloadsDirectory, in: .userDomainMask, appropriateFor: URL(string: key), create: true ).appendingPathComponent(key) } catch { throw TransferError.directoryError } } else { fileURL = URL(fileURLWithPath: destPath!) } let config = try await S3Client.S3ClientConfiguration(region: region) let s3Client = S3Client(config: config) // Create a `FileHandle` referencing the local destination. Then // create a `ByteStream` from that. FileManager.default.createFile(atPath: fileURL.path, contents: nil, attributes: nil) let fileHandle = try FileHandle(forWritingTo: fileURL) // Download the file using `GetObject`. let getInput = GetObjectInput( bucket: bucket, key: key ) do { let getOutput = try await s3Client.getObject(input: getInput) guard let body = getOutput.body else { throw TransferError.downloadError("Error: No data returned for download") } // If the body is returned as a `Data` object, write that to the // file. If it's a stream, read the stream chunk by chunk, // appending each chunk to the destination file. switch body { case .data: guard let data = try await body.readData() else { throw TransferError.downloadError("Download error") } // Write the `Data` to the file. do { try data.write(to: fileURL) } catch { throw TransferError.writeError } break case .stream(let stream as ReadableStream): while (true) { let chunk = try await stream.readAsync(upToCount: 5 * 1024 * 1024) guard let chunk = chunk else { break } // Write the chunk to the destination file. do { try fileHandle.write(contentsOf: chunk) } catch { throw TransferError.writeError } } break default: throw TransferError.downloadError("Received data is unknown object type") } } catch { throw TransferError.downloadError("Error downloading the file: \(error)") } print("File downloaded to \(fileURL.path).") }
SDK for Swift
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import ArgumentParser import AWSClientRuntime import AWSS3 import Foundation import Smithy import SmithyHTTPAPI import SmithyStreams /// Download a file from the specified bucket. /// /// - Parameters: /// - bucket: The Amazon S3 bucket name to get the file from. /// - key: The name (or path) of the file to download from the bucket. /// - destPath: The pathname on the local filesystem at which to store /// the downloaded file. func downloadFile(bucket: String, key: String, destPath: String?) async throws { let fileURL: URL // If no destination path was provided, use the key as the name to use // for the file in the downloads folder. if destPath == nil { do { try fileURL = FileManager.default.url( for: .downloadsDirectory, in: .userDomainMask, appropriateFor: URL(string: key), create: true ).appendingPathComponent(key) } catch { throw TransferError.directoryError } } else { fileURL = URL(fileURLWithPath: destPath!) } let config = try await S3Client.S3ClientConfiguration(region: region) let s3Client = S3Client(config: config) // Create a `FileHandle` referencing the local destination. Then // create a `ByteStream` from that. FileManager.default.createFile(atPath: fileURL.path, contents: nil, attributes: nil) let fileHandle = try FileHandle(forWritingTo: fileURL) // Download the file using `GetObject`. let getInput = GetObjectInput( bucket: bucket, key: key ) do { let getOutput = try await s3Client.getObject(input: getInput) guard let body = getOutput.body else { throw TransferError.downloadError("Error: No data returned for download") } // If the body is returned as a `Data` object, write that to the // file. If it's a stream, read the stream chunk by chunk, // appending each chunk to the destination file. switch body { case .data: guard let data = try await body.readData() else { throw TransferError.downloadError("Download error") } // Write the `Data` to the file. do { try data.write(to: fileURL) } catch { throw TransferError.writeError } break case .stream(let stream as ReadableStream): while (true) { let chunk = try await stream.readAsync(upToCount: 5 * 1024 * 1024) guard let chunk = chunk else { break } // Write the chunk to the destination file. do { try fileHandle.write(contentsOf: chunk) } catch { throw TransferError.writeError } } break default: throw TransferError.downloadError("Received data is unknown object type") } } catch { throw TransferError.downloadError("Error downloading the file: \(error)") } print("File downloaded to \(fileURL.path).") }

For a complete list of AWS SDK developer guides and code examples, see Developing with Amazon S3 using the AWS SDKs. This topic also includes information about getting started and details about previous SDK versions.

プライバシーサイト規約Cookie の設定
© 2025, Amazon Web Services, Inc. or its affiliates.All rights reserved.