-
Notifications
You must be signed in to change notification settings - Fork 55
feat: project setup and upload file #1712
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
0marperez
wants to merge
17
commits into
s3-tm-main
Choose a base branch
from
s3-tm-dev
base: s3-tm-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
Changes from 10 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
bc2fa0a
feat: s3 transfer manager client
0marperez 6895fc3
saving work - upload file
0marperez c9486f1
self review
0marperez 22243e7
pull from main
0marperez dbff057
misc: merge from main
0marperez 835866a
finish upload file
0marperez 8edf604
provide static dummy credentials to tests
0marperez 4137873
add s3 as a test dependency?
0marperez d84516d
refactor tests to be JVM only
0marperez 9d5ec07
Depend on missing 'DefaultAwsSigner'
0marperez 71ea5b7
Merge branch 'main' of github.com:aws/aws-sdk-kotlin into s3-tm-dev
0marperez fb2417f
feedback
0marperez a193a2f
feedback v2
0marperez 010bd4b
add missing KDocs and self review
0marperez 8a821d6
remove old TODO
0marperez ac6c88e
latest feedback
0marperez 32aae24
feedback v3
0marperez 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,26 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| description = "S3 Transfer Manager for the AWS SDK for Kotlin" | ||
| extra["displayName"] = "AWS :: SDK :: Kotlin :: HLL :: S3TransferManager" | ||
0marperez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| extra["moduleName"] = "aws.sdk.kotlin.hll.s3transfermanager" | ||
|
|
||
| kotlin { | ||
| sourceSets { | ||
| commonMain { | ||
| dependencies { | ||
| implementation(project(":aws-runtime:aws-http")) | ||
| implementation(libs.s3) | ||
0marperez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| jvmTest { | ||
| dependencies { | ||
| implementation(libs.smithy.kotlin.test.jvm) | ||
| implementation(libs.smithy.kotlin.testing.jvm) | ||
| implementation(libs.smithy.kotlin.aws.signing.common) | ||
| } | ||
| } | ||
0marperez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
21 changes: 21 additions & 0 deletions
21
...sfer-manager/common/src/aws/sdk/kotlin/hll/s3transfermanager/BusinessMetricInterceptor.kt
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,21 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package aws.sdk.kotlin.hll.s3transfermanager | ||
|
|
||
| import aws.sdk.kotlin.runtime.http.interceptors.businessmetrics.AwsBusinessMetric | ||
| import aws.smithy.kotlin.runtime.businessmetrics.emitBusinessMetric | ||
| import aws.smithy.kotlin.runtime.client.RequestInterceptorContext | ||
| import aws.smithy.kotlin.runtime.http.interceptors.HttpInterceptor | ||
|
|
||
| /** | ||
| * An interceptor that emits the S3 Transfer Manager business metric | ||
| */ | ||
| internal object BusinessMetricInterceptor : HttpInterceptor { | ||
0marperez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| override suspend fun modifyBeforeSerialization(context: RequestInterceptorContext<Any>): Any { | ||
| context.executionContext.emitBusinessMetric(AwsBusinessMetric.S3_TRANSFER) | ||
| return context.request | ||
| } | ||
| } | ||
274 changes: 274 additions & 0 deletions
274
hll/s3-transfer-manager/common/src/aws/sdk/kotlin/hll/s3transfermanager/S3TransferManager.kt
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,274 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package aws.sdk.kotlin.hll.s3transfermanager | ||
|
|
||
| import aws.sdk.kotlin.hll.s3transfermanager.model.MultiPartDownloadType | ||
| import aws.sdk.kotlin.hll.s3transfermanager.model.Part | ||
| import aws.sdk.kotlin.hll.s3transfermanager.model.UploadFileRequest | ||
| import aws.sdk.kotlin.hll.s3transfermanager.model.UploadFileResponse | ||
| import aws.sdk.kotlin.hll.s3transfermanager.utils.buildCompleteMultipartUploadRequest | ||
| import aws.sdk.kotlin.hll.s3transfermanager.utils.buildUploadPartRequest | ||
| import aws.sdk.kotlin.hll.s3transfermanager.utils.ceilDiv | ||
| import aws.sdk.kotlin.hll.s3transfermanager.utils.getNextPart | ||
| import aws.sdk.kotlin.hll.s3transfermanager.utils.resolvePartSize | ||
| import aws.sdk.kotlin.hll.s3transfermanager.utils.toCreateMultiPartUploadRequest | ||
| import aws.sdk.kotlin.hll.s3transfermanager.utils.toPutObjectRequest | ||
| import aws.sdk.kotlin.hll.s3transfermanager.utils.toUploadFileResponse | ||
| import aws.sdk.kotlin.services.s3.S3Client | ||
| import aws.sdk.kotlin.services.s3.abortMultipartUpload | ||
| import aws.sdk.kotlin.services.s3.model.CompleteMultipartUploadRequest | ||
| import aws.sdk.kotlin.services.s3.model.CompleteMultipartUploadResponse | ||
| import aws.sdk.kotlin.services.s3.model.CompletedPart | ||
| import aws.sdk.kotlin.services.s3.model.CreateMultipartUploadRequest | ||
| import aws.sdk.kotlin.services.s3.model.CreateMultipartUploadResponse | ||
| import aws.sdk.kotlin.services.s3.model.PutObjectRequest | ||
| import aws.sdk.kotlin.services.s3.model.PutObjectResponse | ||
| import aws.sdk.kotlin.services.s3.model.UploadPartRequest | ||
| import aws.sdk.kotlin.services.s3.model.UploadPartResponse | ||
| import aws.sdk.kotlin.services.s3.withConfig | ||
| import aws.smithy.kotlin.runtime.content.ByteStream | ||
| import aws.smithy.kotlin.runtime.io.SdkBuffer | ||
| import aws.smithy.kotlin.runtime.telemetry.logging.logger | ||
| import kotlinx.coroutines.Deferred | ||
| import kotlinx.coroutines.async | ||
| import kotlinx.coroutines.coroutineScope | ||
|
|
||
| /** | ||
| * High level utility for managing transfers to Amazon S3. | ||
| */ | ||
| public class S3TransferManager private constructor( | ||
0marperez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public val client: S3Client, | ||
| public val targePartSize: Long, | ||
0marperez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public val multipartUploadThreshold: Long, | ||
0marperez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public val multipartDownloadType: MultiPartDownloadType, | ||
| public val interceptors: MutableList<TransferInterceptor>, | ||
| ) { | ||
| internal var context: TransferContext = TransferContext() | ||
0marperez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public companion object { | ||
| public operator fun invoke(block: Builder.() -> Unit): S3TransferManager = | ||
| Builder().apply(block).build() | ||
| } | ||
|
|
||
| public class Builder { | ||
| public var client: S3Client? = null | ||
| public var targePartSize: Long = 8_000_000 | ||
| public var multipartUploadThreshold: Long = 16_000_000L | ||
| public var multipartDownloadType: MultiPartDownloadType = Part | ||
| public var interceptors: MutableList<TransferInterceptor> = mutableListOf() | ||
0marperez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| internal fun build(): S3TransferManager = | ||
| S3TransferManager( | ||
| client = client?.withConfig { interceptors += BusinessMetricInterceptor } ?: error("client must be set"), | ||
0marperez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| targePartSize = targePartSize, | ||
| multipartUploadThreshold = multipartUploadThreshold, | ||
| multipartDownloadType = multipartDownloadType, | ||
| interceptors = interceptors, | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Executes a sequence of operations around a hook. | ||
| * | ||
| * The execution flow is as follows: | ||
| * 1. Runs all interceptors scheduled to execute **before** the hook. | ||
| * 2. Executes the main hook logic. | ||
| * 3. Runs all interceptors scheduled to execute **after** the hook. | ||
| */ | ||
| private suspend fun operationHook(hook: TransferHook, block: suspend () -> Any) { | ||
| when (hook) { | ||
| is TransferInitiated -> { | ||
| interceptors.forEach { it.readBeforeTransferInitiated(context) } | ||
| interceptors.forEach { context = it.modifyBeforeTransferInitiated(context) } | ||
| block.invoke() | ||
| interceptors.forEach { it.readAfterTransferInitiated(context) } | ||
| interceptors.forEach { context = it.modifyAfterTransferInitiated(context) } | ||
| } | ||
| is BytesTransferred -> { | ||
| interceptors.forEach { it.readBeforeBytesTransferred(context) } | ||
| interceptors.forEach { context = it.modifyBeforeBytesTransferred(context) } | ||
| block.invoke() | ||
| interceptors.forEach { it.readAfterBytesTransferred(context) } | ||
| interceptors.forEach { context = it.modifyAfterBytesTransferred(context) } | ||
| } | ||
| is FileTransferred -> { | ||
| interceptors.forEach { it.readBeforeFileTransferred(context) } | ||
| interceptors.forEach { context = it.modifyBeforeFileTransferred(context) } | ||
| block.invoke() | ||
| interceptors.forEach { it.readAfterFileTransferred(context) } | ||
| interceptors.forEach { context = it.modifyAfterFileTransferred(context) } | ||
| } | ||
| is TransferCompleted -> { | ||
| interceptors.forEach { it.readBeforeTransferCompleted(context) } | ||
| interceptors.forEach { context = it.modifyBeforeTransferCompleted(context) } | ||
| block.invoke() | ||
| interceptors.forEach { it.readAfterTransferCompleted(context) } | ||
| interceptors.forEach { context = it.modifyAfterTransferCompleted(context) } | ||
| } | ||
| else -> error("TransferHook not implemented: ${hook::class.simpleName}") | ||
| } | ||
| } | ||
0marperez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Uploads a byte stream to Amazon S3, automatically using multipart uploads | ||
| * for large objects as needed. | ||
| * | ||
| * This function handles the complexity of splitting the data into parts, | ||
| * uploading each part, and completing the multipart upload. For object smaller than [multipartUploadThreshold], | ||
| * a standard single-part upload is performed automatically. | ||
| * | ||
| * If the specified [targePartSize] for multipart uploads is too small to allow | ||
| * all parts to fit within S3's limit of 10,000 parts, the part size will be | ||
| * automatically increased so that exactly 10,000 parts are uploaded. | ||
| */ | ||
| public suspend fun uploadFile(uploadFileRequest: UploadFileRequest): Deferred<UploadFileResponse> = coroutineScope { | ||
0marperez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
0marperez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| val multiPartUpload = uploadFileRequest.contentLength >= multipartUploadThreshold | ||
| val uploadedParts = mutableListOf<CompletedPart>() | ||
| var mpuUploadId = "null" | ||
0marperez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| val logger = coroutineContext.logger<S3TransferManager>() | ||
0marperez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /* | ||
| Handles transfer initiated hook | ||
| */ | ||
| suspend fun transferInitiated(multiPartUpload: Boolean) { | ||
| context.transferredBytes = 0L | ||
| context.transferableBytes = uploadFileRequest.contentLength | ||
| context.request = if (multiPartUpload) { | ||
| uploadFileRequest.toCreateMultiPartUploadRequest() | ||
| } else { | ||
| uploadFileRequest.toPutObjectRequest() | ||
| } | ||
| operationHook(TransferInitiated) { | ||
| if (multiPartUpload) { | ||
| context.response = client.createMultipartUpload(context.request as CreateMultipartUploadRequest) | ||
| mpuUploadId = (context.response as CreateMultipartUploadResponse).uploadId ?: throw Exception("Missing upload id in create multipart upload response") | ||
0marperez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| Handles bytes transferred hook | ||
| */ | ||
| suspend fun transferBytes(multiPartUpload: Boolean) { | ||
| if (multiPartUpload) { | ||
| try { | ||
| val partSize = resolvePartSize(uploadFileRequest, this@S3TransferManager, logger) | ||
| val numberOfParts = ceilDiv(uploadFileRequest.contentLength, partSize) | ||
| val partSource = when (uploadFileRequest.body) { | ||
| is ByteStream.Buffer -> uploadFileRequest.body.bytes() | ||
| is ByteStream.ChannelStream -> uploadFileRequest.body.readFrom() | ||
| is ByteStream.SourceStream -> uploadFileRequest.body.readFrom() | ||
| else -> error("Unhandled body type: ${uploadFileRequest.body?.let { it::class.simpleName } ?: "null"}") | ||
0marperez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| val partBuffer = SdkBuffer() | ||
| var currentPartNumber = 1L | ||
|
|
||
| while (context.transferredBytes!! < context.transferableBytes!!) { | ||
| partBuffer.getNextPart(partSource, partSize, this@S3TransferManager) | ||
| if (currentPartNumber != numberOfParts) { | ||
| check(partBuffer.size == partSize) { | ||
| "Part #$currentPartNumber size mismatch detected. Expected $partSize, actual: ${partBuffer.size}" | ||
| } | ||
| } | ||
0marperez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| context.request = | ||
| buildUploadPartRequest( | ||
| uploadFileRequest, | ||
| partBuffer, | ||
| currentPartNumber, | ||
| mpuUploadId, | ||
| ) | ||
|
|
||
| operationHook(BytesTransferred) { | ||
| context.response = client.uploadPart(context.request as UploadPartRequest) | ||
| context.transferredBytes = context.transferredBytes!! + partSize | ||
| } | ||
|
|
||
| uploadedParts += CompletedPart { | ||
| partNumber = currentPartNumber.toInt() | ||
| eTag = (context.response as UploadPartResponse).eTag | ||
| } | ||
| currentPartNumber += 1 | ||
| } | ||
0marperez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| check(uploadedParts.size == numberOfParts.toInt()) { | ||
| "The number of uploaded parts does not match the expected count. Expected $numberOfParts, actual: ${uploadedParts.size}" | ||
| } | ||
| } catch (uploadPartThrowable: Throwable) { | ||
| try { | ||
| client.abortMultipartUpload { | ||
| bucket = uploadFileRequest.bucket | ||
| expectedBucketOwner = uploadFileRequest.expectedBucketOwner | ||
| key = uploadFileRequest.key | ||
| requestPayer = uploadFileRequest.requestPayer | ||
| uploadId = mpuUploadId | ||
| } | ||
| throw Exception("Multipart upload failed (ID: $mpuUploadId). One or more parts could not be uploaded", uploadPartThrowable) | ||
| } catch (abortThrowable: Throwable) { | ||
| throw Exception("Multipart upload failed (ID: $mpuUploadId). Unable to abort multipart upload.", abortThrowable) | ||
| } | ||
| } | ||
0marperez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else { | ||
| operationHook(BytesTransferred) { | ||
| context.response = client.putObject(context.request as PutObjectRequest) | ||
| context.transferredBytes = context.transferableBytes | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| Handles transfer completed hook | ||
| */ | ||
| suspend fun transferComplete(multiPartUpload: Boolean) { | ||
| if (multiPartUpload) { | ||
| context.request = buildCompleteMultipartUploadRequest(uploadFileRequest, mpuUploadId, uploadedParts) | ||
| } | ||
| operationHook(TransferCompleted) { | ||
| if (multiPartUpload) { | ||
| try { | ||
| context.response = client.completeMultipartUpload(context.request as CompleteMultipartUploadRequest) | ||
| } catch (t: Throwable) { | ||
| throw Exception("Unable to complete multipart upload with ID: $mpuUploadId", t) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async { | ||
| checkNotNull(uploadFileRequest.body?.contentLength) { | ||
| "UploadFileRequest.body.contentLength must be set" | ||
| } | ||
| check(uploadFileRequest.body.contentLength == uploadFileRequest.contentLength) { | ||
| "contentLength mismatch. uploadFileRequest: ${uploadFileRequest.contentLength}, uploadFileRequest.body.contentLength: ${uploadFileRequest.body.contentLength}" | ||
| } | ||
0marperez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| transferInitiated(multiPartUpload) | ||
| transferBytes(multiPartUpload) | ||
| transferComplete(multiPartUpload) | ||
|
|
||
| when (context.response) { | ||
| is PutObjectResponse -> (context.response as PutObjectResponse).toUploadFileResponse() | ||
| is CompleteMultipartUploadResponse -> (context.response as CompleteMultipartUploadResponse).toUploadFileResponse() | ||
| else -> error("Unexpected response: ${context.response?.let { it::class.simpleName } ?: "null"}") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Uploads a byte stream to Amazon S3, automatically using multipart uploads | ||
| * for large objects as needed. | ||
| * | ||
| * This function handles the complexity of splitting the data into parts, | ||
| * uploading each part, and completing the multipart upload. For object smaller than [multipartUploadThreshold], | ||
| * a standard single-part upload is performed automatically. | ||
| * | ||
| * If the specified [targePartSize] for multipart uploads is too small to allow | ||
| * all parts to fit within S3's limit of 10,000 parts, the part size will be | ||
| * automatically increased so that exactly 10,000 parts are uploaded. | ||
| */ | ||
| public suspend inline fun uploadFile(crossinline block: UploadFileRequest.Builder.() -> Unit): Deferred<UploadFileResponse> = uploadFile(UploadFileRequest.Builder().apply(block).build()) | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.