Search in sources :

Example 1 with HeadBucketResult

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;
        }
    }
}
Also used : DefaultAWSCredentialsProviderChain(com.amazonaws.auth.DefaultAWSCredentialsProviderChain) AmazonS3(com.amazonaws.services.s3.AmazonS3) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) AmazonServiceException(com.amazonaws.AmazonServiceException) AnonymousAWSCredentials(com.amazonaws.auth.AnonymousAWSCredentials) AwsClientBuilder(com.amazonaws.client.builder.AwsClientBuilder) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider)

Example 2 with HeadBucketResult

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;
}
Also used : HeadBucketResultHandler(com.amazonaws.services.s3.model.transform.HeadBucketResultHandler) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 3 with HeadBucketResult

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;
}
Also used : HeadBucketResult(com.amazonaws.services.s3.model.HeadBucketResult) AmazonWebServiceResponse(com.amazonaws.AmazonWebServiceResponse)

Example 4 with HeadBucketResult

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);
}
Also used : HeadBucketResultHandler(com.amazonaws.services.s3.model.transform.HeadBucketResultHandler)

Aggregations

HeadBucketResultHandler (com.amazonaws.services.s3.model.transform.HeadBucketResultHandler)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 AmazonWebServiceResponse (com.amazonaws.AmazonWebServiceResponse)1 AWSCredentialsProvider (com.amazonaws.auth.AWSCredentialsProvider)1 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)1 AnonymousAWSCredentials (com.amazonaws.auth.AnonymousAWSCredentials)1 DefaultAWSCredentialsProviderChain (com.amazonaws.auth.DefaultAWSCredentialsProviderChain)1 AwsClientBuilder (com.amazonaws.client.builder.AwsClientBuilder)1 AmazonS3 (com.amazonaws.services.s3.AmazonS3)1 HeadBucketResult (com.amazonaws.services.s3.model.HeadBucketResult)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1