/** * Retrieves the region of the bucket by making a HeadBucket request to us-west-1 region. * * Currently S3 doesn't return region in a HEAD Bucket request if the bucket * owner has enabled bucket to accept only SigV4 requests via bucket * policies. */ private String getBucketRegionViaHeadRequest(String bucketName) { String bucketRegion = null; try { Request<HeadBucketRequest> request = createRequest(bucketName, null, new HeadBucketRequest(bucketName), HttpMethodName.HEAD); request.addHandlerContext(HandlerContextKey.OPERATION_NAME, "HeadBucket"); HeadBucketResult result = invoke(request, new HeadBucketResultHandler(), bucketName, null, true); bucketRegion = result.getBucketRegion(); } catch (AmazonS3Exception exception) { if (exception.getAdditionalDetails() != null) { bucketRegion = exception.getAdditionalDetails().get( Headers.S3_BUCKET_REGION); } } if (bucketRegion == null && log.isDebugEnabled()) { log.debug("Not able to derive region of the " + bucketName + " from the HEAD Bucket requests."); } return bucketRegion; }
if (ase.getAdditionalDetails() != null) { String region = ase.getAdditionalDetails().get(Headers.S3_BUCKET_REGION); bucketRegionCache.put(bucket, region); ase.setErrorMessage("The bucket is in this region: " + region +
/** * Retrieves the region of the bucket by making a HeadBucket request to * us-west-1 region. Currently S3 doesn't return region in a HEAD Bucket * request if the bucket owner has enabled bucket to accept only SigV4 * requests via bucket policies. */ private String getBucketRegionViaHeadRequest(String bucketName) { String bucketRegion = null; try { final String endpoint = "https://s3-us-west-1.amazonaws.com"; final Request<HeadBucketRequest> request = createRequest(bucketName, null, new HeadBucketRequest(bucketName), HttpMethodName.HEAD, new URI(endpoint)); final HeadBucketResult result = invoke(request, new HeadBucketResultHandler(), bucketName, null); bucketRegion = result.getBucketRegion(); } catch (final AmazonS3Exception exception) { if (exception.getAdditionalDetails() != null) { bucketRegion = exception.getAdditionalDetails().get( Headers.S3_BUCKET_REGION); } } catch (final URISyntaxException e) { log.warn("Error while creating URI"); } if (bucketRegion == null && log.isDebugEnabled()) { log.debug("Not able to derive region of the " + bucketName + " from the HEAD Bucket requests."); } return bucketRegion; }
if (ase.getAdditionalDetails() != null) { final String region = ase.getAdditionalDetails().get(Headers.S3_BUCKET_REGION); bucketRegionCache.put(bucket, region); ase.setErrorMessage("The bucket is in this region: " + region +
private AmazonS3 buildAmazonS3ForRedirectLocation(AmazonS3 prototype, AmazonS3Exception e) { try { return this.amazonS3ClientFactory.createClientForEndpointUrl(prototype, "https://" + e.getAdditionalDetails().get("Endpoint")); } catch (Exception ex) { LOGGER.error("Error getting new Amazon S3 for redirect", ex); throw new RuntimeException(e); } } }
private AmazonS3 buildAmazonS3ForRedirectLocation(AmazonS3 prototype, AmazonS3Exception e) { try { return this.amazonS3ClientFactory.createClientForEndpointUrl(prototype, "https://" + e.getAdditionalDetails().get("Endpoint")); } catch (Exception ex) { LOGGER.error("Error getting new Amazon S3 for redirect", ex); throw new RuntimeException(e); } } }
public Map<String, String> getAdditionalDetails() { return getCause().getAdditionalDetails(); }
public Map<String, String> getAdditionalDetails() { return getCause().getAdditionalDetails(); }
public Map<String, String> getAdditionalDetails() { return getCause().getAdditionalDetails(); }
/** * Get low level details of an amazon exception for logging; multi-line. * @param e exception * @return string details */ public static String stringify(AmazonS3Exception e) { // get the low level details of an exception, StringBuilder builder = new StringBuilder( stringify((AmazonServiceException) e)); Map<String, String> details = e.getAdditionalDetails(); if (details != null) { builder.append('\n'); for (Map.Entry<String, String> d : details.entrySet()) { builder.append(d.getKey()).append('=') .append(d.getValue()).append('\n'); } } return builder.toString(); }
/** * Retrieves the region of the bucket by making a HeadBucket request to us-west-1 region. * * Currently S3 doesn't return region in a HEAD Bucket request if the bucket * owner has enabled bucket to accept only SigV4 requests via bucket * policies. */ private String getBucketRegionViaHeadRequest(String bucketName) { String bucketRegion = null; try { Request<HeadBucketRequest> request = createRequest(bucketName, null, new HeadBucketRequest(bucketName), HttpMethodName.HEAD); HeadBucketResult result = invoke(request, new HeadBucketResultHandler(), bucketName, null, true); bucketRegion = result.getBucketRegion(); } catch (AmazonS3Exception exception) { if (exception.getAdditionalDetails() != null) { bucketRegion = exception.getAdditionalDetails().get( Headers.S3_BUCKET_REGION); } } if (bucketRegion == null && log.isDebugEnabled()) { log.debug("Not able to derive region of the " + bucketName + " from the HEAD Bucket requests."); } return bucketRegion; }
class CloudFrontRetryCondition implements RetryCondition { @Override public boolean shouldRetry(AmazonWebServiceRequest originalRequest, AmazonClientException exception, int retriesAttempted) { if(exception instanceof AmazonS3Exception) { final AmazonS3Exception s3Exception = (AmazonS3Exception) exception; return s3Exception.getStatusCode() == 400 && s3Exception.getErrorCode().equals("MalformedPolicy") && s3Exception.getErrorMessage().equals("Invalid principal in policy") && s3Exception.getAdditionalDetails().get("Detail").contains("arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity"); } else { return false; } } }
if (s3Exception.getAdditionalDetails() != null && s3Exception.getAdditionalDetails().containsKey(ENDPOINT_KEY)) { message = String.format( "Received permanent redirect response to " + "configured in %s does not match the region containing " + "the bucket.", s3Exception.getAdditionalDetails().get(ENDPOINT_KEY), ENDPOINT_URL);
if (s3Exception.getAdditionalDetails() != null && s3Exception.getAdditionalDetails().containsKey(ENDPOINT_KEY)) { message = String.format( "Received permanent redirect response to " + "configured in %s does not match the region containing " + "the bucket.", s3Exception.getAdditionalDetails().get(ENDPOINT_KEY), ENDPOINT_URL);
private static String determineEndpointForBucket(String bucket, AWSCredentialsProvider credentialsProvider, @Nullable ClientConfiguration s3Config, String rootPath) { // Guess us-east-1. If wrong AWS will return a redirect with the correct endpoint AmazonS3 s3 = createS3ClientForRegion(Regions.US_EAST_1.getName(), credentialsProvider, s3Config); if (rootPath.startsWith("/")) { rootPath = rootPath.substring(1); } if (!rootPath.endsWith("/")) { rootPath = rootPath + "/"; } try { // Any request will work but presumably the client has list access for stash so perform a list. s3.listObjects(new ListObjectsRequest() .withBucketName(bucket) .withPrefix(rootPath) .withDelimiter("/") .withMaxKeys(1)); // If this didn't error out then the presumed us-east-1 region was correct return "s3.us-east-1.amazonaws.com"; } catch (AmazonS3Exception e) { if (e.getStatusCode() == 301 /* MOVED_PERMANENTLY */) { String endPoint = e.getAdditionalDetails().get("Endpoint"); // The end point is prefixed with the bucket name, so strip it return endPoint.substring(bucket.length() + 1); } throw e; } }
private static String determineEndpointForBucket(String bucket, AWSCredentialsProvider credentialsProvider, @Nullable ClientConfiguration s3Config, String rootPath) { // Guess us-east-1. If wrong AWS will return a redirect with the correct endpoint AmazonS3 s3 = createS3ClientForRegion(Regions.US_EAST_1.getName(), credentialsProvider, s3Config); if (rootPath.startsWith("/")) { rootPath = rootPath.substring(1); } if (!rootPath.endsWith("/")) { rootPath = rootPath + "/"; } try { // Any request will work but presumably the client has list access for stash so perform a list. s3.listObjects(new ListObjectsRequest() .withBucketName(bucket) .withPrefix(rootPath) .withDelimiter("/") .withMaxKeys(1)); // If this didn't error out then the presumed us-east-1 region was correct return "s3.us-east-1.amazonaws.com"; } catch (AmazonS3Exception e) { if (e.getStatusCode() == 301 /* MOVED_PERMANENTLY */) { String endPoint = e.getAdditionalDetails().get("Endpoint"); // The end point is prefixed with the bucket name, so strip it return endPoint.substring(bucket.length() + 1); } throw e; } }
/** * Retrieves the region of the bucket by making a HeadBucket request to * us-west-1 region. Currently S3 doesn't return region in a HEAD Bucket * request if the bucket owner has enabled bucket to accept only SigV4 * requests via bucket policies. */ private String getBucketRegionViaHeadRequest(String bucketName) { String bucketRegion = null; try { final String endpoint = "https://s3-us-west-1.amazonaws.com"; final Request<HeadBucketRequest> request = createRequest(bucketName, null, new HeadBucketRequest(bucketName), HttpMethodName.HEAD, new URI(endpoint)); final HeadBucketResult result = invoke(request, new HeadBucketResultHandler(), bucketName, null); bucketRegion = result.getBucketRegion(); } catch (final AmazonS3Exception exception) { if (exception.getAdditionalDetails() != null) { bucketRegion = exception.getAdditionalDetails().get( Headers.S3_BUCKET_REGION); } } catch (final URISyntaxException e) { log.warn("Error while creating URI"); } if (bucketRegion == null && log.isDebugEnabled()) { log.debug("Not able to derive region of the " + bucketName + " from the HEAD Bucket requests."); } return bucketRegion; }
if (ase.getAdditionalDetails() != null) { final String region = ase.getAdditionalDetails().get(Headers.S3_BUCKET_REGION); bucketRegionCache.put(bucket, region); ase.setErrorMessage("The bucket is in this region: " + region +
if (ase.getAdditionalDetails() != null) { String region = ase.getAdditionalDetails().get(Headers.S3_BUCKET_REGION); bucketRegionCache.put(bucket, region); ase.setErrorMessage("The bucket is in this region: " + region +
case 307: if (s3Exception != null) { if (s3Exception.getAdditionalDetails() != null && s3Exception.getAdditionalDetails().containsKey(ENDPOINT_KEY)) { message = String.format("Received permanent redirect response to " + "endpoint %s. This likely indicates that the S3 endpoint " + "configured in %s does not match the AWS region containing " + "the bucket.", s3Exception.getAdditionalDetails().get(ENDPOINT_KEY), ENDPOINT);