use of com.amazonaws.services.s3.model.HeadBucketResult in project imagej-utils by embl-cba.
the class S3Utils method getS3Client.
public static AmazonS3 getS3Client(String endpoint, String region, String bucket) {
final AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(endpoint, region);
// first we create a client with anon credentials and see if we can access the bucket like this
AWSCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(new AnonymousAWSCredentials());
AmazonS3 s3 = AmazonS3ClientBuilder.standard().withPathStyleAccessEnabled(true).withEndpointConfiguration(endpointConfiguration).withCredentials(credentialsProvider).build();
// check if we can access the access
HeadBucketRequest headBucketRequest = new HeadBucketRequest(bucket);
try {
HeadBucketResult headBucketResult = s3.headBucket(headBucketRequest);
return s3;
} catch (AmazonServiceException e) {
switch(e.getStatusCode()) {
// if we get a 403 response (access forbidden), we try again with credentials
case HttpStatusCodes.STATUS_CODE_FORBIDDEN:
credentialsProvider = new DefaultAWSCredentialsProviderChain();
checkCredentialsExistence(credentialsProvider);
s3 = AmazonS3ClientBuilder.standard().withPathStyleAccessEnabled(true).withEndpointConfiguration(endpointConfiguration).withCredentials(credentialsProvider).build();
// check if we have access permissions now
try {
HeadBucketResult headBucketResult = s3.headBucket(headBucketRequest);
} catch (AmazonServiceException e2) {
throw e2;
}
return s3;
// otherwise the bucket does not exist or has been permanently moved; throw the exception
default:
throw e;
}
}
}
use of com.amazonaws.services.s3.model.HeadBucketResult in project aws-sdk-android by aws-amplify.
the class AmazonS3Client method getBucketRegionViaHeadRequest.
/**
* 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;
}
use of com.amazonaws.services.s3.model.HeadBucketResult in project aws-sdk-android by aws-amplify.
the class HeadBucketResultHandler method handle.
@Override
public AmazonWebServiceResponse<HeadBucketResult> handle(HttpResponse response) throws Exception {
final AmazonWebServiceResponse<HeadBucketResult> awsResponse = new AmazonWebServiceResponse<HeadBucketResult>();
final HeadBucketResult result = new HeadBucketResult();
result.setBucketRegion(response.getHeaders().get(Headers.S3_BUCKET_REGION));
awsResponse.setResult(result);
return awsResponse;
}
use of com.amazonaws.services.s3.model.HeadBucketResult in project aws-sdk-android by aws-amplify.
the class AmazonS3Client method headBucket.
@Override
public HeadBucketResult headBucket(HeadBucketRequest headBucketRequest) throws AmazonClientException, AmazonServiceException {
final String bucketName = headBucketRequest.getBucketName();
assertParameterNotNull(bucketName, "The bucketName parameter must be specified.");
final Request<HeadBucketRequest> request = createRequest(bucketName, null, headBucketRequest, HttpMethodName.HEAD);
return invoke(request, new HeadBucketResultHandler(), bucketName, null);
}
Aggregations