-
Notifications
You must be signed in to change notification settings - Fork 1k
Handle 200 OK errors in PutObject (multipart) in S3 CRT client #7084
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
alextwoods
wants to merge
1
commit into
master
Choose a base branch
from
alexwoo/fix_crt_s3_200_error_mp
base: master
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.
+425
−0
Open
Changes from all commits
Commits
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
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,6 @@ | ||
| { | ||
| "type": "bugfix", | ||
| "category": "Amazon S3", | ||
| "contributor": "", | ||
| "description": "Handle 200 OK errors in PutObject (multipart) in S3 CRT client" | ||
| } |
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 |
|---|---|---|
|
|
@@ -21,6 +21,9 @@ | |
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.time.Duration; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
@@ -37,6 +40,8 @@ | |
| import software.amazon.awssdk.crt.s3.S3FinishedResponseContext; | ||
| import software.amazon.awssdk.crt.s3.S3MetaRequestProgress; | ||
| import software.amazon.awssdk.crt.s3.S3MetaRequestResponseHandler; | ||
| import software.amazon.awssdk.http.AbortableInputStream; | ||
| import software.amazon.awssdk.http.SdkHttpFullResponse; | ||
| import software.amazon.awssdk.http.SdkHttpResponse; | ||
| import software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler; | ||
| import software.amazon.awssdk.services.s3.model.S3Exception; | ||
|
|
@@ -220,6 +225,15 @@ private void handleServiceError(int responseStatus, HttpHeader[] headers, byte[] | |
| s3Exception.addSuppressed(sdkClientException); | ||
| failResponseHandlerAndFuture(s3Exception); | ||
| notifyResponsePublisherErrorIfNeeded(s3Exception); | ||
| } else if (responseStatus == 200) { | ||
| // handleServiceError is only called when crtCode != SUCCESS. If the response status is 200, this is the | ||
| // S3 "200 with error in body" case (e.g. CompleteMultipartUpload returning HTTP 200 with <Error> XML). | ||
| // We wrap the response in ErrorFlaggedSdkHttpResponse which overrides isSuccessful() to return false. | ||
| // This causes the downstream SDK pipeline (XmlResponseParserUtils, DecorateErrorFromResponseBodyUnmarshaller) | ||
| // to parse the body and detect the error, producing a properly modeled S3Exception. | ||
| SdkHttpFullResponse errorFlagged = new ErrorFlaggedSdkHttpResponse(errorResponse.build()); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming is hard - My other alternative here was something like "SdkHttpResponse200OkError" or something like that. |
||
| initiateResponseHandling(errorFlagged); | ||
| onErrorResponseComplete(errorPayload); | ||
| } else { | ||
| initiateResponseHandling(errorResponse.build()); | ||
| onErrorResponseComplete(errorPayload); | ||
|
|
@@ -303,4 +317,148 @@ private static SdkHttpResponse.Builder populateSdkHttpResponse(SdkHttpResponse.B | |
|
|
||
| private static class NoOpPublisherListener implements PublisherListener<S3MetaRequestProgress> { | ||
| } | ||
|
|
||
| /** | ||
| * An {@link SdkHttpFullResponse} wrapper that overrides {@link #isSuccessful()} to always return {@code false}. | ||
| * This forces the SDK response pipeline ({@code XmlResponseParserUtils}, {@code DecorateErrorFromResponseBodyUnmarshaller}) | ||
| * to parse the response body even for HTTP 200 responses, enabling detection of S3's "200 with error in body" pattern. | ||
| * | ||
| * <p>The {@link #toBuilder()} method returns a builder whose {@code build()} also produces an | ||
| * {@code ErrorFlaggedSdkHttpResponse}, ensuring the override survives the rebuild cycle in | ||
| * {@code AsyncResponseHandler}.</p> | ||
| */ | ||
| static final class ErrorFlaggedSdkHttpResponse implements SdkHttpFullResponse { | ||
| private final SdkHttpFullResponse delegate; | ||
|
|
||
| ErrorFlaggedSdkHttpResponse(SdkHttpResponse delegate) { | ||
| // Ensure we have a full response to delegate to | ||
| if (delegate instanceof SdkHttpFullResponse) { | ||
| this.delegate = (SdkHttpFullResponse) delegate; | ||
| } else { | ||
| this.delegate = SdkHttpFullResponse.builder() | ||
| .statusCode(delegate.statusCode()) | ||
| .headers(delegate.headers()) | ||
| .build(); | ||
| } | ||
| } | ||
|
|
||
| private ErrorFlaggedSdkHttpResponse(SdkHttpFullResponse delegate, @SuppressWarnings("unused") boolean internal) { | ||
| this.delegate = delegate; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isSuccessful() { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public int statusCode() { | ||
| return delegate.statusCode(); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<String> statusText() { | ||
| return delegate.statusText(); | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, List<String>> headers() { | ||
| return delegate.headers(); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<AbortableInputStream> content() { | ||
| return delegate.content(); | ||
| } | ||
|
|
||
| @Override | ||
| public Builder toBuilder() { | ||
| return new ErrorFlaggedBuilder(delegate.toBuilder()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A builder that wraps a standard {@link SdkHttpFullResponse.Builder} but produces an | ||
| * {@link ErrorFlaggedSdkHttpResponse} on {@code build()}, preserving the {@code isSuccessful()=false} override. | ||
| */ | ||
| private static final class ErrorFlaggedBuilder implements SdkHttpFullResponse.Builder { | ||
| private final SdkHttpFullResponse.Builder delegate; | ||
|
|
||
| ErrorFlaggedBuilder(SdkHttpFullResponse.Builder delegate) { | ||
| this.delegate = delegate; | ||
| } | ||
|
|
||
| @Override | ||
| public SdkHttpFullResponse build() { | ||
| return new ErrorFlaggedSdkHttpResponse(delegate.build(), true); | ||
| } | ||
|
|
||
| @Override | ||
| public String statusText() { | ||
| return delegate.statusText(); | ||
| } | ||
|
|
||
| @Override | ||
| public SdkHttpFullResponse.Builder statusText(String statusText) { | ||
| delegate.statusText(statusText); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public int statusCode() { | ||
| return delegate.statusCode(); | ||
| } | ||
|
|
||
| @Override | ||
| public SdkHttpFullResponse.Builder statusCode(int statusCode) { | ||
| delegate.statusCode(statusCode); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, List<String>> headers() { | ||
| return delegate.headers(); | ||
| } | ||
|
|
||
| @Override | ||
| public SdkHttpFullResponse.Builder putHeader(String headerName, List<String> headerValues) { | ||
| delegate.putHeader(headerName, headerValues); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public SdkHttpFullResponse.Builder appendHeader(String headerName, String headerValue) { | ||
| delegate.appendHeader(headerName, headerValue); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public SdkHttpFullResponse.Builder headers(Map<String, List<String>> headers) { | ||
| delegate.headers(headers); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public SdkHttpFullResponse.Builder removeHeader(String headerName) { | ||
| delegate.removeHeader(headerName); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public SdkHttpFullResponse.Builder clearHeaders() { | ||
| delegate.clearHeaders(); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public AbortableInputStream content() { | ||
| return delegate.content(); | ||
| } | ||
|
|
||
| @Override | ||
| public SdkHttpFullResponse.Builder content(AbortableInputStream content) { | ||
| delegate.content(content); | ||
| return this; | ||
| } | ||
| } | ||
| } | ||
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.