Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2010-2015, 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
  * or in the "license" file accompanying this file. This file is distributed
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.
The internal implementation of AuthErrorRetryStrategy which automatically switches to V4 signer when the S3 returns auth error asking for v4 authentication.
 public class S3V4AuthErrorRetryStrategy implements AuthErrorRetryStrategy {
     private static Log log = LogFactory.getLog(S3V4AuthErrorRetryStrategy.class);
     private static final String V4_REGION_WARNING = "please use region-specific endpoint to access "
             + "buckets located in regions that require V4 signing.";
     private final S3RequestEndpointResolver endpointResolver;
     public S3V4AuthErrorRetryStrategy(S3RequestEndpointResolver endpointResolver) {
         this. = endpointResolver;
         this. = new IsSigV4RetryablePredicate();

Currently only used for testing
             SdkPredicate<AmazonServiceExceptionisSigV4Retryable) {
         this. = endpointResolver;
         this. = isSigV4Retryable;
                                                         HttpResponse response,
                                                         AmazonServiceException ase) {
         if (!.test(ase)) {
             return null;
         if (hasServingRegionHeader(response)) {
             return redirectToRegionInHeader(requestresponse);
         } else if (canUseVirtualAddressing()) {
             return redirectToS3External();
         } else {
             throw new AmazonClientException(ase);
     private boolean canUseVirtualAddressing() {
         return BucketNameUtils.isDNSBucketName(.getBucketName());
     private AuthRetryParameters redirectToRegionInHeader(Request<?> requestHttpResponse response) {
         final String region = getServingRegionHeader(response);
         AWSS3V4Signer v4Signer = buildSigV4Signer(region);
         return buildRetryParams(v4Signerrequest.getEndpoint());

If the response doesn't have the x-amz-region header we have to resort to sending a request to s3-external-1

         AWSS3V4Signer v4Signer = buildSigV4Signer(..toString());
        try {
            URI bucketEndpoint = new URI(String.format("".getBucketName()));
            return buildRetryParams(v4SignerbucketEndpoint);
        } catch (URISyntaxException e) {
            throw new AmazonClientException("Failed to re-send the request to \"\". "
                    + e);
    private AWSS3V4Signer buildSigV4Signer(final String region) {
        AWSS3V4Signer v4Signer = new AWSS3V4Signer();
        return v4Signer;
    private AuthRetryParameters buildRetryParams(AWSS3V4Signer signerURI endpoint) {
        .warn("Attempting to re-send the request to " + endpoint.getHost() + " with AWS V4 authentication. "
                + "To avoid this warning in the future, " + );
        return new AuthRetryParameters(signerendpoint);
    private static boolean hasServingRegionHeader(HttpResponse response) {
        return !StringUtils.isNullOrEmpty(getServingRegionHeader(response));
    private static String getServingRegionHeader(HttpResponse response) {
        return response.getHeaders().get(.);
New to GrepCode? Check out our FAQ X