-
Notifications
You must be signed in to change notification settings - Fork 931
Add DefaultPresignedUrlManager getObject implementation for S3 pre-signed URL #6241
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
Merged
joviegas
merged 6 commits into
aws:feature/master/pre-signed-url-getobject
from
jencymaryjoseph:feature/presignedUrlGet-getObject
Jul 10, 2025
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0d70dde
getobject implementation and tests
jencymaryjoseph 578bbd2
corrected the getObject params in test:
jencymaryjoseph 8a3a25e
fixed build failure-dependency issue with Retrystrategy in test
jencymaryjoseph cff5de0
fixed sonarcube issues
jencymaryjoseph 43e9f6d
addressed sonarcube issue
jencymaryjoseph d121a82
edited javadocs, added invalidurl test
jencymaryjoseph 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
125 changes: 125 additions & 0 deletions
125
.../software/amazon/awssdk/services/s3/internal/presignedurl/DefaultPresignedUrlManager.java
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,125 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.services.s3.internal.presignedurl; | ||
|
||
import static software.amazon.awssdk.core.client.config.SdkClientOption.SIGNER_OVERRIDDEN; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.awscore.exception.AwsServiceException; | ||
import software.amazon.awssdk.awscore.internal.AwsProtocolMetadata; | ||
import software.amazon.awssdk.core.client.config.SdkAdvancedClientOption; | ||
import software.amazon.awssdk.core.client.config.SdkClientConfiguration; | ||
import software.amazon.awssdk.core.client.config.SdkClientOption; | ||
import software.amazon.awssdk.core.client.handler.ClientExecutionParams; | ||
import software.amazon.awssdk.core.client.handler.SyncClientHandler; | ||
import software.amazon.awssdk.core.exception.SdkClientException; | ||
import software.amazon.awssdk.core.http.HttpResponseHandler; | ||
import software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute; | ||
import software.amazon.awssdk.core.metrics.CoreMetric; | ||
import software.amazon.awssdk.core.signer.NoOpSigner; | ||
import software.amazon.awssdk.core.sync.ResponseTransformer; | ||
import software.amazon.awssdk.metrics.MetricCollector; | ||
import software.amazon.awssdk.metrics.MetricPublisher; | ||
import software.amazon.awssdk.metrics.NoOpMetricCollector; | ||
import software.amazon.awssdk.protocols.xml.AwsS3ProtocolFactory; | ||
import software.amazon.awssdk.protocols.xml.XmlOperationMetadata; | ||
import software.amazon.awssdk.services.s3.internal.presignedurl.model.PresignedUrlGetObjectRequestWrapper; | ||
import software.amazon.awssdk.services.s3.model.GetObjectResponse; | ||
import software.amazon.awssdk.services.s3.model.InvalidObjectStateException; | ||
import software.amazon.awssdk.services.s3.model.NoSuchKeyException; | ||
import software.amazon.awssdk.services.s3.model.S3Exception; | ||
import software.amazon.awssdk.services.s3.presignedurl.PresignedUrlManager; | ||
import software.amazon.awssdk.services.s3.presignedurl.model.PresignedUrlGetObjectRequest; | ||
|
||
/** | ||
* Default implementation of {@link PresignedUrlManager} for executing S3 operations using presigned URLs. | ||
*/ | ||
@SdkInternalApi | ||
public final class DefaultPresignedUrlManager implements PresignedUrlManager { | ||
|
||
private final SyncClientHandler clientHandler; | ||
private final AwsS3ProtocolFactory protocolFactory; | ||
private final SdkClientConfiguration clientConfiguration; | ||
private final AwsProtocolMetadata protocolMetadata; | ||
|
||
public DefaultPresignedUrlManager(SyncClientHandler clientHandler, | ||
AwsS3ProtocolFactory protocolFactory, | ||
SdkClientConfiguration clientConfiguration, | ||
AwsProtocolMetadata protocolMetadata) { | ||
this.clientHandler = clientHandler; | ||
this.protocolFactory = protocolFactory; | ||
this.clientConfiguration = clientConfiguration; | ||
this.protocolMetadata = protocolMetadata; | ||
} | ||
|
||
/** | ||
* Downloads an S3 object using a presigned URL. | ||
*/ | ||
@Override | ||
public <ReturnT> ReturnT getObject(PresignedUrlGetObjectRequest presignedUrlGetObjectRequest, | ||
ResponseTransformer<GetObjectResponse, ReturnT> responseTransformer) | ||
throws NoSuchKeyException, InvalidObjectStateException, | ||
AwsServiceException, SdkClientException, S3Exception { | ||
|
||
HttpResponseHandler<GetObjectResponse> responseHandler = protocolFactory.createResponseHandler( | ||
GetObjectResponse::builder, new XmlOperationMetadata().withHasStreamingSuccessResponse(true)); | ||
|
||
HttpResponseHandler<AwsServiceException> errorResponseHandler = protocolFactory.createErrorResponseHandler(); | ||
|
||
PresignedUrlGetObjectRequestWrapper internalRequest = PresignedUrlGetObjectRequestWrapper.builder() | ||
.url(presignedUrlGetObjectRequest.presignedUrl()) | ||
.range(presignedUrlGetObjectRequest.range()) | ||
.build(); | ||
|
||
SdkClientConfiguration updatedClientConfiguration = updateSdkClientConfiguration(this.clientConfiguration); | ||
List<MetricPublisher> metricPublishers = Optional.ofNullable( | ||
updatedClientConfiguration.option(SdkClientOption.METRIC_PUBLISHERS)) | ||
.orElse(Collections.emptyList()); | ||
MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? | ||
NoOpMetricCollector.create() : MetricCollector.create("ApiCall"); | ||
try { | ||
apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "S3"); | ||
//TODO: Discuss if we need to change OPERATION_NAME as part of Surface API Review | ||
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "GetObject"); | ||
|
||
return clientHandler.execute( | ||
new ClientExecutionParams<PresignedUrlGetObjectRequestWrapper, GetObjectResponse>() | ||
.withOperationName("PresignedUrlGetObject") | ||
.withProtocolMetadata(protocolMetadata) | ||
.withResponseHandler(responseHandler) | ||
.withErrorResponseHandler(errorResponseHandler) | ||
.withRequestConfiguration(updatedClientConfiguration) | ||
.withInput(internalRequest) | ||
.withMetricCollector(apiCallMetricCollector) | ||
// TODO: Deprecate IS_DISCOVERED_ENDPOINT, use new SKIP_ENDPOINT_RESOLUTION for better semantics | ||
.putExecutionAttribute(SdkInternalExecutionAttribute.IS_DISCOVERED_ENDPOINT, true) | ||
.withMarshaller(new PresignedUrlGetObjectRequestMarshaller(protocolFactory)), responseTransformer); | ||
} finally { | ||
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); | ||
} | ||
} | ||
|
||
private SdkClientConfiguration updateSdkClientConfiguration(SdkClientConfiguration clientConfiguration) { | ||
SdkClientConfiguration.Builder configBuilder = clientConfiguration.toBuilder(); | ||
configBuilder.option(SdkAdvancedClientOption.SIGNER, new NoOpSigner()); | ||
configBuilder.option(SIGNER_OVERRIDDEN, true); | ||
return configBuilder.build(); | ||
} | ||
|
||
} |
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.