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
This commit is contained in:
Andrew Gaul 2014-07-22 10:55:52 -07:00
parent e711275fb1
commit 80a4430035
5 changed files with 9 additions and 65 deletions

View File

@ -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<Boolean> bucketExists(
@Bucket @EndpointParam(parser = AssignCorrectHostnameForBucket.class) @BinderParam(BindAsHostPrefixIfConfigured.class) @ParamValidators(BucketNameValidator.class) String bucketName);

View File

@ -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());

View File

@ -178,7 +178,7 @@ public abstract class S3AsyncClientTest<T extends S3AsyncClient> extends BaseS3A
Invokable<?, ?> method = method(S3AsyncClient.class, "bucketExists", String.class);
GeneratedHttpRequest request = processor.createRequest(method, ImmutableList.<Object> 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);

View File

@ -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.<String, String> 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());

View File

@ -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.<String, String> 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.<String, String> 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");
}
}