From 80a44300352ec3951e96eacee8f91daebdde298e Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Tue, 22 Jul 2014 10:55:52 -0700 Subject: [PATCH] JCLOUDS-679: Use HEAD for S3 bucketExists This method costs 0.4 cents per 10,000 requests instead of 0.5 cents per 1,000 requests: http://aws.amazon.com/s3/pricing/ API reference: http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketHEAD.html --- .../java/org/jclouds/s3/S3AsyncClient.java | 5 +- .../s3/PathBasedS3ClientExpectTest.java | 6 +- .../org/jclouds/s3/S3AsyncClientTest.java | 2 +- .../org/jclouds/s3/S3ClientExpectTest.java | 6 +- .../S3RedirectionRetryHandlerExpectTest.java | 55 ------------------- 5 files changed, 9 insertions(+), 65 deletions(-) delete mode 100644 apis/s3/src/test/java/org/jclouds/s3/handlers/S3RedirectionRetryHandlerExpectTest.java diff --git a/apis/s3/src/main/java/org/jclouds/s3/S3AsyncClient.java b/apis/s3/src/main/java/org/jclouds/s3/S3AsyncClient.java index d0e1a6e78f..a7dff35d90 100644 --- a/apis/s3/src/main/java/org/jclouds/s3/S3AsyncClient.java +++ b/apis/s3/src/main/java/org/jclouds/s3/S3AsyncClient.java @@ -202,10 +202,9 @@ public interface S3AsyncClient extends Closeable { /** * @see S3Client#bucketExists */ - @Named("ListBucket") - @GET + @Named("BucketExists") + @HEAD @Path("/") - @QueryParams(keys = "max-keys", values = "0") @Fallback(FalseOnContainerNotFound.class) ListenableFuture bucketExists( @Bucket @EndpointParam(parser = AssignCorrectHostnameForBucket.class) @BinderParam(BindAsHostPrefixIfConfigured.class) @ParamValidators(BucketNameValidator.class) String bucketName); diff --git a/apis/s3/src/test/java/org/jclouds/s3/PathBasedS3ClientExpectTest.java b/apis/s3/src/test/java/org/jclouds/s3/PathBasedS3ClientExpectTest.java index 42af044101..14be66285e 100644 --- a/apis/s3/src/test/java/org/jclouds/s3/PathBasedS3ClientExpectTest.java +++ b/apis/s3/src/test/java/org/jclouds/s3/PathBasedS3ClientExpectTest.java @@ -42,10 +42,10 @@ public class PathBasedS3ClientExpectTest extends BaseS3ClientExpectTest { @Test public void testBucketExistsReturnsTrueOn200AndFalseOn404() { - HttpRequest bucketFooExists = HttpRequest.builder().method("GET") - .endpoint("https://s3.amazonaws.com/foo?max-keys=0") + HttpRequest bucketFooExists = HttpRequest.builder().method("HEAD") + .endpoint("https://s3.amazonaws.com/foo") .addHeader("Date", CONSTANT_DATE) - .addHeader("Authorization", "AWS identity:p32RsBr2inawMBeCkkiA228BT2w=") + .addHeader("Authorization", "AWS identity:lLD0mzo2bZPIWhxlFDZoT09MKUQ=") .build(); S3Client clientWhenBucketExists = requestSendsResponse(bucketFooExists, HttpResponse.builder().statusCode(200).build()); diff --git a/apis/s3/src/test/java/org/jclouds/s3/S3AsyncClientTest.java b/apis/s3/src/test/java/org/jclouds/s3/S3AsyncClientTest.java index a82ef9e9ab..c0561f40e1 100644 --- a/apis/s3/src/test/java/org/jclouds/s3/S3AsyncClientTest.java +++ b/apis/s3/src/test/java/org/jclouds/s3/S3AsyncClientTest.java @@ -178,7 +178,7 @@ public abstract class S3AsyncClientTest extends BaseS3A Invokable method = method(S3AsyncClient.class, "bucketExists", String.class); GeneratedHttpRequest request = processor.createRequest(method, ImmutableList. of("bucket")); - assertRequestLineEquals(request, "GET https://bucket." + url + "/?max-keys=0 HTTP/1.1"); + assertRequestLineEquals(request, "HEAD https://bucket." + url + "/ HTTP/1.1"); assertNonPayloadHeadersEqual(request, "Host: bucket." + url + "\n"); assertPayloadEquals(request, null, null, false); diff --git a/apis/s3/src/test/java/org/jclouds/s3/S3ClientExpectTest.java b/apis/s3/src/test/java/org/jclouds/s3/S3ClientExpectTest.java index 67baa7b68e..577c8412ff 100644 --- a/apis/s3/src/test/java/org/jclouds/s3/S3ClientExpectTest.java +++ b/apis/s3/src/test/java/org/jclouds/s3/S3ClientExpectTest.java @@ -31,11 +31,11 @@ public class S3ClientExpectTest extends BaseS3ClientExpectTest { @Test public void testBucketExistsReturnsTrueOn200AndFalseOn404() { - HttpRequest bucketFooExists = HttpRequest.builder().method("GET").endpoint( - URI.create("https://s3.amazonaws.com/foo?max-keys=0")).headers( + HttpRequest bucketFooExists = HttpRequest.builder().method("HEAD").endpoint( + URI.create("https://s3.amazonaws.com/foo")).headers( ImmutableMultimap. builder() .put("Date", CONSTANT_DATE) - .put("Authorization", "AWS identity:p32RsBr2inawMBeCkkiA228BT2w=") + .put("Authorization", "AWS identity:lLD0mzo2bZPIWhxlFDZoT09MKUQ=") .build()).build(); S3Client clientWhenBucketExists = requestSendsResponse(bucketFooExists, HttpResponse.builder().statusCode(200).build()); diff --git a/apis/s3/src/test/java/org/jclouds/s3/handlers/S3RedirectionRetryHandlerExpectTest.java b/apis/s3/src/test/java/org/jclouds/s3/handlers/S3RedirectionRetryHandlerExpectTest.java deleted file mode 100644 index 11360f9336..0000000000 --- a/apis/s3/src/test/java/org/jclouds/s3/handlers/S3RedirectionRetryHandlerExpectTest.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License 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 org.jclouds.s3.handlers; - -import java.net.URI; - -import org.jclouds.http.HttpRequest; -import org.jclouds.http.HttpResponse; -import org.jclouds.s3.S3Client; -import org.jclouds.s3.internal.BaseS3ClientExpectTest; -import org.testng.annotations.Test; - -import com.google.common.collect.ImmutableMultimap; - -@Test(groups = "unit", testName = "S3RedirectionRetryHandlerExpectTest") -public class S3RedirectionRetryHandlerExpectTest extends BaseS3ClientExpectTest { - - public void testRedirectOnHeadBucketChangesRequestToGetBucket() { - - HttpRequest bucketFooExists = HttpRequest.builder().method("HEAD").endpoint( - URI.create("https://s3.amazonaws.com/foo?max-keys=0")).headers( - ImmutableMultimap. builder().put("Date", - CONSTANT_DATE).put("Authorization", "AWS identity:p32RsBr2inawMBeCkkiA228BT2w=").build()) - .build(); - - HttpResponse redirectResponse = HttpResponse.builder().statusCode(301).build(); - - HttpRequest bucketFooExistsNowUsesGET = HttpRequest.builder().method("GET").endpoint( - URI.create("https://s3.amazonaws.com/foo?max-keys=0")).headers( - ImmutableMultimap. builder().put("Date", - CONSTANT_DATE).put("Authorization", "AWS identity:p32RsBr2inawMBeCkkiA228BT2w=").build()) - .build(); - - HttpResponse success = HttpResponse.builder().statusCode(200).build(); - - S3Client clientWhenBucketExists = requestsSendResponses(bucketFooExists, redirectResponse, bucketFooExistsNowUsesGET, success); - - assert clientWhenBucketExists.bucketExists("foo"); - - } -}