

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

# Operasi streaming
<a name="streaming-ops"></a>

Dalam AWS SDK untuk Kotlin, data biner (aliran) direpresentasikan sebagai [https://docs.aws.amazon.com/smithy-kotlin/api/latest/runtime-core/aws.smithy.kotlin.runtime.content/-byte-stream/index.html](https://docs.aws.amazon.com/smithy-kotlin/api/latest/runtime-core/aws.smithy.kotlin.runtime.content/-byte-stream/index.html)tipe, yang merupakan aliran byte hanya-baca abstrak.

## Respons streaming
<a name="streaming-responses"></a>

Respons dengan aliran biner (seperti operasi API Amazon Simple Storage Service (Amazon S3) Simple Storage Service (Amazon [GetObject](https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObject.html)S3) ditangani secara berbeda dari metode lain. Metode ini mengambil fungsi lambda yang menangani respons daripada mengembalikan respons secara langsung. Ini membatasi cakupan respons terhadap fungsi dan menyederhanakan manajemen seumur hidup untuk pemanggil dan runtime SDK.

Setelah fungsi lambda kembali, sumber daya apa pun seperti koneksi HTTP yang mendasarinya dilepaskan. (`ByteStream`Seharusnya tidak diakses setelah lambda kembali dan tidak boleh dilewatkan dari penutupan.) Hasil panggilan adalah apa pun yang dikembalikan lambda.

Contoh kode berikut menunjukkan fungsi [getObject](https://docs.aws.amazon.com/sdk-for-kotlin/api/latest/s3/aws.sdk.kotlin.services.s3/-s3-client/get-object.html) menerima parameter lambda, yang menangani respon.

```
val s3Client = S3Client.fromEnvironment()
val req = GetObjectRequest { ... }

val path = Paths.get("/tmp/download.txt")

// S3Client.getObject has the following signature:
// suspend fun <T> getObject(input: GetObjectRequest, block: suspend (GetObjectResponse) -> T): T

val contentSize = s3Client.getObject(req) { resp ->
    // resp is valid until the end of the block.
    // Do not attempt to store or process the stream after the block returns.
    
    // resp.body is of type ByteStream.
    val rc = resp.body?.writeToFile(path)
    rc
}
println("wrote $contentSize bytes to $path")
```

`ByteStream`Jenis ini memiliki ekstensi berikut untuk cara umum mengkonsumsinya:
+ `ByteStream.writeToFile(file: File): Long`
+ `ByteStream.writeToFile(path: Path): Long`
+ `ByteStream.toByteArray(): ByteArray`
+ `ByteStream.decodeToString(): String`

Semua ini didefinisikan dalam `aws.smithy.kotlin.runtime.content` paket.

## Permintaan streaming
<a name="streaming-requests"></a>

Untuk memasok a`ByteStream`, ada juga beberapa metode kenyamanan, termasuk yang berikut:
+ `ByteStream.fromFile(file: File)`
+ `File.asByteStream(): ByteStream`
+ `Path.asByteStream(): ByteStream`
+ `ByteStream.fromBytes(bytes: ByteArray)`
+ `ByteStream.fromString(str: String)`

Semua ini didefinisikan dalam `aws.smithy.kotlin.runtime.content` paket.

Contoh kode berikut menunjukkan penggunaan metode `ByteStream` kenyamanan yang menyediakan properti tubuh dalam pembuatan [PutObjectRequest](https://docs.aws.amazon.com/sdk-for-kotlin/api/latest/s3/aws.sdk.kotlin.services.s3.model/-put-object-request/index.html):

```
val req = PutObjectRequest {
    ...
    body = ByteStream.fromFile(file)
    // body = ByteStream.fromBytes(byteArray)
    // body = ByteStream.fromString("string")
    // etc
}
```