Issue 58 reverted as path style bucket access is deprecated

git-svn-id: http://jclouds.googlecode.com/svn/trunk@1087 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-06-07 21:39:23 +00:00
parent 445ca04c4e
commit 498c1dca20
15 changed files with 280 additions and 231 deletions

View File

@ -35,6 +35,7 @@ import org.jclouds.http.commands.callables.ReturnTrueIf2xx;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted; import com.google.inject.assistedinject.Assisted;
import com.google.inject.name.Named;
/** /**
* Issues a HEAD command to determine if the bucket exists or not. * Issues a HEAD command to determine if the bucket exists or not.
@ -44,38 +45,42 @@ import com.google.inject.assistedinject.Assisted;
*/ */
public class BucketExists extends S3FutureCommand<Boolean> { public class BucketExists extends S3FutureCommand<Boolean> {
@Inject @Inject
public BucketExists(ReturnTrueIf2xx callable, @Assisted String s3Bucket) { public BucketExists(@Named("jclouds.http.address") String amazonHost,
super("HEAD", "/" + maxResults(0).buildQueryString(), callable, s3Bucket); ReturnTrueIf2xx callable, @Assisted String s3Bucket) {
} super("HEAD", "/" + maxResults(0).buildQueryString(), callable,
amazonHost, s3Bucket);
}
@Override @Override
public Boolean get() throws InterruptedException, ExecutionException { public Boolean get() throws InterruptedException, ExecutionException {
try { try {
return super.get(); return super.get();
} catch (ExecutionException e) { } catch (ExecutionException e) {
return attemptNotFound(e); return attemptNotFound(e);
} }
} }
@VisibleForTesting @VisibleForTesting
Boolean attemptNotFound(ExecutionException e) throws ExecutionException { Boolean attemptNotFound(ExecutionException e) throws ExecutionException {
if (e.getCause() != null && e.getCause() instanceof HttpResponseException) { if (e.getCause() != null
HttpResponseException responseException = (HttpResponseException) e.getCause(); && e.getCause() instanceof HttpResponseException) {
if (responseException.getResponse().getStatusCode() == 404) { HttpResponseException responseException = (HttpResponseException) e
return false; .getCause();
} if (responseException.getResponse().getStatusCode() == 404) {
} return false;
throw e; }
} }
throw e;
}
@Override @Override
public Boolean get(long l, TimeUnit timeUnit) throws InterruptedException, ExecutionException, public Boolean get(long l, TimeUnit timeUnit) throws InterruptedException,
TimeoutException { ExecutionException, TimeoutException {
try { try {
return super.get(l, timeUnit); return super.get(l, timeUnit);
} catch (ExecutionException e) { } catch (ExecutionException e) {
return attemptNotFound(e); return attemptNotFound(e);
} }
} }
} }

View File

@ -33,19 +33,20 @@ import org.jclouds.util.Utils;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted; import com.google.inject.assistedinject.Assisted;
import com.google.inject.name.Named;
/** /**
* The copy operation creates a copy of an object that is already storedin Amazon S3. * The copy operation creates a copy of an object that is already storedin
* Amazon S3.
* <p/> * <p/>
* When copying an object, you can preserve all metadata (default) or * When copying an object, you can preserve all metadata (default) or
* {@link CopyObjectOptions#overrideMetadataWith(com.google.common.collect.Multimap) specify new * {@link CopyObjectOptions#overrideMetadataWith(com.google.common.collect.Multimap)
* metadata}. However, the ACL is not preserved and is set to private for the user making the * specify new metadata}. However, the ACL is not preserved and is set to
* request. To override the default ACL setting, * private for the user making the request. To override the default ACL setting,
* {@link CopyObjectOptions#overrideAcl(org.jclouds.aws.s3.domain.acl.CannedAccessPolicy) specify a * {@link CopyObjectOptions#overrideAcl(org.jclouds.aws.s3.domain.acl.CannedAccessPolicy)
* new ACL} when generating a copy request. * specify a new ACL} when generating a copy request.
* *
* @see <a * @see <a href="http://docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?RESTObjectCOPY.html"
* href="http://docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?RESTObjectCOPY.html"
* /> * />
* @see CopyObjectOptions * @see CopyObjectOptions
* @see org.jclouds.aws.s3.domain.acl.CannedAccessPolicy * @see org.jclouds.aws.s3.domain.acl.CannedAccessPolicy
@ -55,20 +56,23 @@ import com.google.inject.assistedinject.Assisted;
public class CopyObject extends S3FutureCommand<S3Object.Metadata> { public class CopyObject extends S3FutureCommand<S3Object.Metadata> {
@Inject @Inject
public CopyObject(ParseSax<S3Object.Metadata> callable, public CopyObject(@Named("jclouds.http.address") String amazonHost,
@Assisted("sourceBucket") String sourceBucket, ParseSax<S3Object.Metadata> callable, @Assisted("sourceBucket") String sourceBucket,
@Assisted("sourceObject") String sourceObject, @Assisted("sourceObject") String sourceObject,
@Assisted("destinationBucket") String destinationBucket, @Assisted("destinationBucket") String destinationBucket,
@Assisted("destinationObject") String destinationObject, @Assisted("destinationObject") String destinationObject,
@Assisted CopyObjectOptions options) { @Assisted CopyObjectOptions options)
super("PUT", "/" + checkNotNull(destinationObject, "destinationObject"), callable, {
destinationBucket); super("PUT",
"/" + checkNotNull(destinationObject, "destinationObject"),
callable, amazonHost, destinationBucket);
CopyObjectHandler handler = (CopyObjectHandler) callable.getHandler(); CopyObjectHandler handler = (CopyObjectHandler) callable.getHandler();
handler.setKey(destinationObject); handler.setKey(destinationObject);
getRequest().getHeaders().put( getRequest().getHeaders().put(
"x-amz-copy-source", "x-amz-copy-source",
String.format("/%1$s/%2$s", checkNotNull(sourceBucket, "sourceBucket"), Utils String.format("/%1$s/%2$s",
.encodeUriPath(checkNotNull(sourceObject, "sourceObject")))); checkNotNull(sourceBucket, "sourceBucket"),
Utils.encodeUriPath(checkNotNull(sourceObject, "sourceObject"))));
getRequest().getHeaders().putAll(options.buildRequestHeaders()); getRequest().getHeaders().putAll(options.buildRequestHeaders());
} }
} }

View File

@ -34,54 +34,59 @@ import org.jclouds.http.commands.callables.ReturnTrueIf2xx;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted; import com.google.inject.assistedinject.Assisted;
import com.google.inject.name.Named;
/** /**
* The DELETE request operation deletes the bucket named in the URI. All objects in the bucket must * The DELETE request operation deletes the bucket named in the URI. All objects
* be deleted before the bucket itself can be deleted. * in the bucket must be deleted before the bucket itself can be deleted.
* <p /> * <p />
* Only the owner of a bucket can delete it, regardless of the bucket's access control policy. * Only the owner of a bucket can delete it, regardless of the bucket's access
* control policy.
* *
* @see <a * @see <a href="http://docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?RESTBucketDELETE.html"
* href="http://docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?RESTBucketDELETE.html"
* /> * />
* @author Adrian Cole * @author Adrian Cole
*/ */
public class DeleteBucket extends S3FutureCommand<Boolean> { public class DeleteBucket extends S3FutureCommand<Boolean> {
@Inject @Inject
public DeleteBucket(ReturnTrueIf2xx callable, @Assisted String s3Bucket) { public DeleteBucket(@Named("jclouds.http.address") String amazonHost,
super("DELETE", "/", callable, s3Bucket); ReturnTrueIf2xx callable, @Assisted String s3Bucket) {
} super("DELETE", "/", callable, amazonHost, s3Bucket);
}
@Override @Override
public Boolean get() throws InterruptedException, ExecutionException { public Boolean get() throws InterruptedException, ExecutionException {
try { try {
return super.get(); return super.get();
} catch (ExecutionException e) { } catch (ExecutionException e) {
return attemptNotFound(e); return attemptNotFound(e);
} }
} }
@VisibleForTesting @VisibleForTesting
Boolean attemptNotFound(ExecutionException e) throws ExecutionException { Boolean attemptNotFound(ExecutionException e) throws ExecutionException {
if (e.getCause() != null && e.getCause() instanceof HttpResponseException) { if (e.getCause() != null
AWSResponseException responseException = (AWSResponseException) e.getCause(); && e.getCause() instanceof HttpResponseException) {
if (responseException.getResponse().getStatusCode() == 404) { AWSResponseException responseException = (AWSResponseException) e
return true; .getCause();
} else if ("BucketNotEmpty".equals(responseException.getError().getCode())) { if (responseException.getResponse().getStatusCode() == 404) {
return false; return true;
} } else if ("BucketNotEmpty".equals(responseException.getError()
} .getCode())) {
throw e; return false;
} }
}
throw e;
}
@Override @Override
public Boolean get(long l, TimeUnit timeUnit) throws InterruptedException, ExecutionException, public Boolean get(long l, TimeUnit timeUnit) throws InterruptedException,
TimeoutException { ExecutionException, TimeoutException {
try { try {
return super.get(l, timeUnit); return super.get(l, timeUnit);
} catch (ExecutionException e) { } catch (ExecutionException e) {
return attemptNotFound(e); return attemptNotFound(e);
} }
} }
} }

View File

@ -29,10 +29,11 @@ import org.jclouds.http.commands.callables.ReturnTrueIf2xx;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted; import com.google.inject.assistedinject.Assisted;
import com.google.inject.name.Named;
/** /**
* The DELETE request operation removes the specified object from Amazon S3. Once deleted, there is * The DELETE request operation removes the specified object from Amazon S3.
* no method to restore or undelete an object. * Once deleted, there is no method to restore or undelete an object.
* *
* @see <a href="http://docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html? * @see <a href="http://docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?
* RESTObjectDELETE.html" /> * RESTObjectDELETE.html" />
@ -40,9 +41,10 @@ import com.google.inject.assistedinject.Assisted;
*/ */
public class DeleteObject extends S3FutureCommand<Boolean> { public class DeleteObject extends S3FutureCommand<Boolean> {
@Inject @Inject
public DeleteObject(ReturnTrueIf2xx callable, @Assisted("bucketName") String bucket, public DeleteObject(@Named("jclouds.http.address") String amazonHost,
@Assisted("key") String key) { ReturnTrueIf2xx callable, @Assisted("bucketName") String bucket,
super("DELETE", "/" + checkNotNull(key), callable, bucket); @Assisted("key") String key) {
} super("DELETE", "/" + checkNotNull(key), callable, amazonHost, bucket);
}
} }

View File

@ -35,6 +35,7 @@ import org.jclouds.http.commands.callables.xml.ParseSax;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted; import com.google.inject.assistedinject.Assisted;
import com.google.inject.name.Named;
/** /**
* A GET request operation directed at an object or bucket URI with the "acl" parameter retrieves * A GET request operation directed at an object or bucket URI with the "acl" parameter retrieves
@ -48,15 +49,20 @@ import com.google.inject.assistedinject.Assisted;
public class GetAccessControlList extends S3FutureCommand<AccessControlList> { public class GetAccessControlList extends S3FutureCommand<AccessControlList> {
@Inject @Inject
public GetAccessControlList(ParseSax<AccessControlList> accessControlListParser, public GetAccessControlList(@Named("jclouds.http.address") String amazonHost,
@Assisted("bucketName") String bucket) { ParseSax<AccessControlList> accessControlListParser,
super("GET", "/?acl", accessControlListParser, bucket); @Assisted("bucketName") String bucket)
{
super("GET", "/?acl", accessControlListParser, amazonHost, bucket);
} }
@Inject @Inject
public GetAccessControlList(ParseSax<AccessControlList> accessControlListParser, public GetAccessControlList(@Named("jclouds.http.address") String amazonHost,
@Assisted("bucketName") String bucket, @Assisted("objectKey") String objectKey) { ParseSax<AccessControlList> accessControlListParser,
super("GET", "/" + objectKey + "?acl", accessControlListParser, bucket); @Assisted("bucketName") String bucket,
@Assisted("objectKey") String objectKey)
{
super("GET", "/" + objectKey + "?acl", accessControlListParser, amazonHost, bucket);
} }
@Override @Override
@ -73,7 +79,8 @@ public class GetAccessControlList extends S3FutureCommand<AccessControlList> {
if (e.getCause() != null && e.getCause() instanceof HttpResponseException) { if (e.getCause() != null && e.getCause() instanceof HttpResponseException) {
AWSResponseException responseException = (AWSResponseException) e.getCause(); AWSResponseException responseException = (AWSResponseException) e.getCause();
if ("NoSuchBucket".equals(responseException.getError().getCode()) if ("NoSuchBucket".equals(responseException.getError().getCode())
|| "NoSuchObject".equals(responseException.getError().getCode())) { || "NoSuchObject".equals(responseException.getError().getCode()))
{
return AccessControlList.NOT_FOUND; return AccessControlList.NOT_FOUND;
} }
} }
@ -81,8 +88,8 @@ public class GetAccessControlList extends S3FutureCommand<AccessControlList> {
} }
@Override @Override
public AccessControlList get(long l, TimeUnit timeUnit) throws InterruptedException, public AccessControlList get(long l, TimeUnit timeUnit) throws InterruptedException, ExecutionException,
ExecutionException, TimeoutException { TimeoutException {
try { try {
return super.get(l, timeUnit); return super.get(l, timeUnit);
} catch (ExecutionException e) { } catch (ExecutionException e) {

View File

@ -37,6 +37,7 @@ import org.jclouds.aws.s3.domain.S3Object;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted; import com.google.inject.assistedinject.Assisted;
import com.google.inject.name.Named;
/** /**
* Retrieves the S3Object associated with the Key or {@link S3Object#NOT_FOUND} if not available; * Retrieves the S3Object associated with the Key or {@link S3Object#NOT_FOUND} if not available;
@ -64,10 +65,10 @@ import com.google.inject.assistedinject.Assisted;
public class GetObject extends S3FutureCommand<S3Object> { public class GetObject extends S3FutureCommand<S3Object> {
@Inject @Inject
public GetObject(ParseObjectFromHeadersAndHttpContent callable, public GetObject(@Named("jclouds.http.address") String amazonHost,
@Assisted("bucketName") String s3Bucket, @Assisted("key") String key, ParseObjectFromHeadersAndHttpContent callable, @Assisted("bucketName") String s3Bucket,
@Assisted GetObjectOptions options) { @Assisted("key") String key, @Assisted GetObjectOptions options) {
super("GET", "/" + checkNotNull(key), callable, s3Bucket); super("GET", "/" + checkNotNull(key), callable, amazonHost, s3Bucket);
this.getRequest().getHeaders().putAll(options.buildRequestHeaders()); this.getRequest().getHeaders().putAll(options.buildRequestHeaders());
callable.setKey(key); callable.setKey(key);
} }

View File

@ -36,59 +36,65 @@ import org.jclouds.http.HttpResponseException;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted; import com.google.inject.assistedinject.Assisted;
import com.google.inject.name.Named;
/** /**
* Retrieves the metadata associated with the Key or * Retrieves the metadata associated with the Key or
* {@link org.jclouds.aws.s3.domain.S3Object.Metadata#NOT_FOUND} if not available. * {@link org.jclouds.aws.s3.domain.S3Object.Metadata#NOT_FOUND} if not available.
* *
* <p/> * <p/>
* The HEAD operation is used to retrieve information about a specific object or object size, * The HEAD operation is used to retrieve information about a specific object or
* without actually fetching the object itself. This is useful if you're only interested in the * object size, without actually fetching the object itself. This is useful if
* object metadata, and don't want to waste bandwidth on the object data. * you're only interested in the object metadata, and don't want to waste
* bandwidth on the object data.
* *
* @see GetObject * @see GetObject
* @see <a * @see <a href="http://docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?RESTObjectHEAD.html"
* href="http://docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?RESTObjectHEAD.html"
* /> * />
* @author Adrian Cole * @author Adrian Cole
* *
*/ */
public class HeadObject extends S3FutureCommand<S3Object.Metadata> { public class HeadObject extends S3FutureCommand<S3Object.Metadata> {
@Inject @Inject
public HeadObject(ParseMetadataFromHeaders callable, @Assisted("bucketName") String bucket, public HeadObject(@Named("jclouds.http.address") String amazonHost,
@Assisted("key") String key) { ParseMetadataFromHeaders callable,
super("HEAD", "/" + checkNotNull(key), callable, bucket); @Assisted("bucketName") String bucket, @Assisted("key") String key) {
callable.setKey(key); super("HEAD", "/" + checkNotNull(key), callable, amazonHost, bucket);
} callable.setKey(key);
}
@Override @Override
public S3Object.Metadata get() throws InterruptedException, ExecutionException { public S3Object.Metadata get() throws InterruptedException,
try { ExecutionException {
return super.get(); try {
} catch (ExecutionException e) { return super.get();
return attemptNotFound(e); } catch (ExecutionException e) {
} return attemptNotFound(e);
} }
}
@VisibleForTesting @VisibleForTesting
S3Object.Metadata attemptNotFound(ExecutionException e) throws ExecutionException { S3Object.Metadata attemptNotFound(ExecutionException e)
if (e.getCause() != null && e.getCause() instanceof HttpResponseException) { throws ExecutionException {
HttpResponseException responseException = (HttpResponseException) e.getCause(); if (e.getCause() != null
if (responseException.getResponse().getStatusCode() == 404) { && e.getCause() instanceof HttpResponseException) {
return S3Object.Metadata.NOT_FOUND; HttpResponseException responseException = (HttpResponseException) e
} .getCause();
} if (responseException.getResponse().getStatusCode() == 404) {
throw e; return S3Object.Metadata.NOT_FOUND;
} }
}
throw e;
}
@Override @Override
public S3Object.Metadata get(long l, TimeUnit timeUnit) throws InterruptedException, public S3Object.Metadata get(long l, TimeUnit timeUnit)
ExecutionException, TimeoutException { throws InterruptedException, ExecutionException, TimeoutException {
try { try {
return super.get(l, timeUnit); return super.get(l, timeUnit);
} catch (ExecutionException e) { } catch (ExecutionException e) {
return attemptNotFound(e); return attemptNotFound(e);
} }
} }
} }

View File

@ -31,11 +31,13 @@ import org.jclouds.aws.AWSResponseException;
import org.jclouds.aws.s3.commands.options.ListBucketOptions; import org.jclouds.aws.s3.commands.options.ListBucketOptions;
import org.jclouds.aws.s3.domain.S3Bucket; import org.jclouds.aws.s3.domain.S3Bucket;
import org.jclouds.aws.s3.xml.ListBucketHandler; import org.jclouds.aws.s3.xml.ListBucketHandler;
import org.jclouds.http.HttpResponseException;
import org.jclouds.http.commands.callables.xml.ParseSax; import org.jclouds.http.commands.callables.xml.ParseSax;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted; import com.google.inject.assistedinject.Assisted;
import com.google.inject.name.Named;
/** /**
* A GET request operation using a bucket URI lists information about the objects in the bucket. * A GET request operation using a bucket URI lists information about the objects in the bucket.
@ -54,9 +56,10 @@ import com.google.inject.assistedinject.Assisted;
public class ListBucket extends S3FutureCommand<S3Bucket> { public class ListBucket extends S3FutureCommand<S3Bucket> {
@Inject @Inject
public ListBucket(ParseSax<S3Bucket> bucketParser, @Assisted String bucket, public ListBucket(@Named("jclouds.http.address") String amazonHost,
ParseSax<S3Bucket> bucketParser, @Assisted String bucket,
@Assisted ListBucketOptions options) { @Assisted ListBucketOptions options) {
super("GET", "/" + options.buildQueryString(), bucketParser, bucket); super("GET", "/" + options.buildQueryString(), bucketParser, amazonHost, bucket);
ListBucketHandler handler = (ListBucketHandler) bucketParser.getHandler(); ListBucketHandler handler = (ListBucketHandler) bucketParser.getHandler();
handler.setBucketName(bucket); handler.setBucketName(bucket);
} }
@ -72,7 +75,7 @@ public class ListBucket extends S3FutureCommand<S3Bucket> {
@VisibleForTesting @VisibleForTesting
S3Bucket attemptNotFound(ExecutionException e) throws ExecutionException { S3Bucket attemptNotFound(ExecutionException e) throws ExecutionException {
if (e.getCause() != null && e.getCause() instanceof AWSResponseException) { if (e.getCause() != null && e.getCause() instanceof HttpResponseException) {
AWSResponseException responseException = (AWSResponseException) e.getCause(); AWSResponseException responseException = (AWSResponseException) e.getCause();
if ("NoSuchBucket".equals(responseException.getError().getCode())) { if ("NoSuchBucket".equals(responseException.getError().getCode())) {
return S3Bucket.NOT_FOUND; return S3Bucket.NOT_FOUND;

View File

@ -29,21 +29,23 @@ import org.jclouds.aws.s3.domain.S3Bucket;
import org.jclouds.http.commands.callables.xml.ParseSax; import org.jclouds.http.commands.callables.xml.ParseSax;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.name.Named;
/** /**
* Returns a list of all of the buckets owned by the authenticated sender of the request. * Returns a list of all of the buckets owned by the authenticated sender of the
* request.
* *
* @see <a * @see <a href="http://docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?RESTServiceGET.html"
* href="http://docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?RESTServiceGET.html"
* /> * />
* @author Adrian Cole * @author Adrian Cole
* *
*/ */
public class ListOwnedBuckets extends S3FutureCommand<List<S3Bucket.Metadata>> { public class ListOwnedBuckets extends S3FutureCommand<List<S3Bucket.Metadata>> {
@Inject @Inject
public ListOwnedBuckets(ParseSax<List<S3Bucket.Metadata>> callable) { public ListOwnedBuckets(@Named("jclouds.http.address") String amazonHost,
super("GET", "/", callable); ParseSax<List<S3Bucket.Metadata>> callable) {
} super("GET", "/", callable, amazonHost);
}
} }

View File

@ -30,35 +30,36 @@ import org.jclouds.http.commands.callables.ReturnTrueIf2xx;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted; import com.google.inject.assistedinject.Assisted;
import com.google.inject.name.Named;
/** /**
* Create and name your own bucket in which to store your objects. * Create and name your own bucket in which to store your objects.
* <p/> * <p/>
* The PUT request operation with a bucket URI creates a new bucket. Depending on your latency and * The PUT request operation with a bucket URI creates a new bucket. Depending
* legal requirements, you can specify a location constraint that will affect where your data * on your latency and legal requirements, you can specify a location constraint
* physically resides. You can currently specify a Europe (EU) location constraint via * that will affect where your data physically resides. You can currently
* {@link PutBucketOptions}. * specify a Europe (EU) location constraint via {@link PutBucketOptions}.
* *
* @see PutBucketOptions * @see PutBucketOptions
* @see <a * @see <a href="http://docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?RESTBucketPUT.html"
* href="http://docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?RESTBucketPUT.html"
* /> * />
* @author Adrian Cole * @author Adrian Cole
* *
*/ */
public class PutBucket extends S3FutureCommand<Boolean> { public class PutBucket extends S3FutureCommand<Boolean> {
@Inject @Inject
public PutBucket(ReturnTrueIf2xx callable, @Assisted String bucketName, public PutBucket(@Named("jclouds.http.address") String amazonHost,
@Assisted PutBucketOptions options) { ReturnTrueIf2xx callable, @Assisted String bucketName,
super("PUT", "/", callable, S3Utils.validateBucketName(bucketName)); @Assisted PutBucketOptions options) {
getRequest().getHeaders().putAll(options.buildRequestHeaders()); super("PUT", "/", callable, amazonHost, S3Utils
String payload = options.buildPayload(); .validateBucketName(bucketName));
if (payload != null) { getRequest().getHeaders().putAll(options.buildRequestHeaders());
getRequest().setPayload(payload); String payload = options.buildPayload();
getRequest().getHeaders().put(HttpHeaders.CONTENT_LENGTH, payload.getBytes().length + ""); if (payload != null) {
} else { getRequest().setPayload(payload);
getRequest().getHeaders().put(HttpHeaders.CONTENT_LENGTH, "0"); getRequest().getHeaders().put(HttpHeaders.CONTENT_LENGTH,
} payload.getBytes().length + "");
} }
}
} }

View File

@ -34,6 +34,7 @@ import org.jclouds.http.HttpHeaders;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted; import com.google.inject.assistedinject.Assisted;
import com.google.inject.name.Named;
/** /**
* Store data by creating or overwriting an object. * Store data by creating or overwriting an object.
@ -42,53 +43,56 @@ import com.google.inject.assistedinject.Assisted;
* This returns a byte[] of the md5 hash of what Amazon S3 received * This returns a byte[] of the md5 hash of what Amazon S3 received
* <p /> * <p />
* <p/> * <p/>
* This command allows you to specify {@link PutObjectOptions} to control delivery of content. * This command allows you to specify {@link PutObjectOptions} to control
* delivery of content.
* *
* *
* @see PutObjectOptions * @see PutObjectOptions
* @see <a * @see <a href="http://docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?RESTObjectPUT.html"
* href="http://docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?RESTObjectPUT.html"
* /> * />
* @author Adrian Cole * @author Adrian Cole
*/ */
public class PutObject extends S3FutureCommand<byte[]> { public class PutObject extends S3FutureCommand<byte[]> {
@Inject @Inject
public PutObject(ParseMd5FromETagHeader callable, @Assisted String s3Bucket, public PutObject(@Named("jclouds.http.address") String amazonHost,
@Assisted S3Object object, @Assisted PutObjectOptions options) { ParseMd5FromETagHeader callable, @Assisted String s3Bucket,
super("PUT", "/" + checkNotNull(object.getKey()), callable, s3Bucket); @Assisted S3Object object, @Assisted PutObjectOptions options) {
checkArgument(object.getMetadata().getSize() >= 0, "size must be set"); super("PUT", "/" + checkNotNull(object.getKey()), callable, amazonHost,
s3Bucket);
checkArgument(object.getMetadata().getSize() >= 0, "size must be set");
getRequest().setPayload(checkNotNull(object.getData(), "object.getContent()")); getRequest().setPayload(
checkNotNull(object.getData(), "object.getContent()"));
getRequest().getHeaders() getRequest().getHeaders().put(
.put( HttpHeaders.CONTENT_TYPE,
HttpHeaders.CONTENT_TYPE, checkNotNull(object.getMetadata().getContentType(),
checkNotNull(object.getMetadata().getContentType(), "object.metadata.contentType()"));
"object.metadata.contentType()"));
getRequest().getHeaders() getRequest().getHeaders().put(HttpHeaders.CONTENT_LENGTH,
.put(HttpHeaders.CONTENT_LENGTH, object.getMetadata().getSize() + ""); object.getMetadata().getSize() + "");
if (object.getMetadata().getCacheControl() != null) { if (object.getMetadata().getCacheControl() != null) {
getRequest().getHeaders().put(HttpHeaders.CACHE_CONTROL, getRequest().getHeaders().put(HttpHeaders.CACHE_CONTROL,
object.getMetadata().getCacheControl()); object.getMetadata().getCacheControl());
} }
if (object.getMetadata().getContentDisposition() != null) { if (object.getMetadata().getContentDisposition() != null) {
getRequest().getHeaders().put(HttpHeaders.CONTENT_DISPOSITION, getRequest().getHeaders().put(HttpHeaders.CONTENT_DISPOSITION,
object.getMetadata().getContentDisposition()); object.getMetadata().getContentDisposition());
} }
if (object.getMetadata().getContentEncoding() != null) { if (object.getMetadata().getContentEncoding() != null) {
getRequest().getHeaders().put(HttpHeaders.CONTENT_ENCODING, getRequest().getHeaders().put(HttpHeaders.CONTENT_ENCODING,
object.getMetadata().getContentEncoding()); object.getMetadata().getContentEncoding());
} }
if (object.getMetadata().getMd5() != null) if (object.getMetadata().getMd5() != null)
getRequest().getHeaders().put(HttpHeaders.CONTENT_MD5, getRequest().getHeaders().put(HttpHeaders.CONTENT_MD5,
S3Utils.toBase64String(object.getMetadata().getMd5())); S3Utils.toBase64String(object.getMetadata().getMd5()));
getRequest().getHeaders().putAll(object.getMetadata().getUserMetadata()); getRequest().getHeaders()
getRequest().getHeaders().putAll(options.buildRequestHeaders()); .putAll(object.getMetadata().getUserMetadata());
getRequest().getHeaders().putAll(options.buildRequestHeaders());
} }
} }

View File

@ -33,6 +33,7 @@ import org.jclouds.aws.s3.xml.S3ParserFactory;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted; import com.google.inject.assistedinject.Assisted;
import com.google.inject.name.Named;
/** /**
* Assembles the command objects for S3. * Assembles the command objects for S3.
@ -121,29 +122,32 @@ public class S3CommandFactory {
return headMetadataFactory.create(bucket, key); return headMetadataFactory.create(bucket, key);
} }
@Inject
@Named("jclouds.http.address")
String amazonHost;
public ListOwnedBuckets createGetMetadataForOwnedBuckets() { public ListOwnedBuckets createGetMetadataForOwnedBuckets() {
return new ListOwnedBuckets(parserFactory.createListBucketsParser()); return new ListOwnedBuckets(amazonHost, parserFactory.createListBucketsParser());
} }
public ListBucket createListBucket(String bucket, ListBucketOptions options) { public ListBucket createListBucket(String bucket, ListBucketOptions options) {
return new ListBucket(parserFactory.createListBucketParser(), bucket, options); return new ListBucket(amazonHost, parserFactory.createListBucketParser(), bucket, options);
} }
public CopyObject createCopyObject(String sourceBucket, String sourceObject, public CopyObject createCopyObject(String sourceBucket, String sourceObject,
String destinationBucket, String destinationObject, CopyObjectOptions options) { String destinationBucket, String destinationObject, CopyObjectOptions options) {
return new CopyObject(parserFactory.createCopyObjectParser(), sourceBucket, return new CopyObject(amazonHost, parserFactory.createCopyObjectParser(), sourceBucket,
sourceObject, destinationBucket, destinationObject, options); sourceObject, destinationBucket, destinationObject, options);
} }
public GetAccessControlList createGetBucketACL(String bucket) { public GetAccessControlList createGetBucketACL(String bucket) {
return new GetAccessControlList( return new GetAccessControlList(
parserFactory.createAccessControlListParser(), bucket); amazonHost, parserFactory.createAccessControlListParser(), bucket);
} }
public GetAccessControlList createGetObjectACL(String bucket, String objectKey) { public GetAccessControlList createGetObjectACL(String bucket, String objectKey) {
return new GetAccessControlList( return new GetAccessControlList(
parserFactory.createAccessControlListParser(), bucket, objectKey); amazonHost, parserFactory.createAccessControlListParser(), bucket, objectKey);
} }
} }

View File

@ -37,13 +37,19 @@ import org.jclouds.http.HttpFutureCommand;
public class S3FutureCommand<T> extends HttpFutureCommand<T> { public class S3FutureCommand<T> extends HttpFutureCommand<T> {
public S3FutureCommand(String method, String uri, ResponseCallable<T> responseCallable, public S3FutureCommand(String method, String uri, ResponseCallable<T> responseCallable,
String bucketName) { String amazonHost, String bucketName) {
super(method, String.format("/%1$s%2$s", checkNotNull(bucketName, "bucketName"), super(method, uri, responseCallable);
checkNotNull(uri, "uri")), responseCallable); addHostHeader(checkNotNull(amazonHost, "amazonHost"), checkNotNull(bucketName, "bucketName"));
} }
public S3FutureCommand(String method, String uri, ResponseCallable<T> responseCallable) { public S3FutureCommand(String method, String uri, ResponseCallable<T> responseCallable,
String amazonHost) {
super(method, uri, responseCallable); super(method, uri, responseCallable);
addHostHeader(checkNotNull(amazonHost, "amazonHost"));
}
protected void addHostHeader(String amazonHost, String bucketName) {
addHostHeader(checkNotNull(bucketName) + "." + amazonHost);
} }
} }

View File

@ -158,10 +158,9 @@ public class RequestAuthorizeSignature implements HttpRequestFilter {
} }
private static void appendBucketName(HttpRequest request, StringBuilder toSign) { private static void appendBucketName(HttpRequest request, StringBuilder toSign) {
String hostHeader = request.getFirstHeaderOrNull(HttpHeaders.HOST); String hostHeader = request.getHeaders().get(HttpHeaders.HOST).iterator().next();
if (hostHeader != null && hostHeader.endsWith(".s3.amazonaws.com")) { if (hostHeader.endsWith(".s3.amazonaws.com"))
toSign.append("/").append(hostHeader.substring(0, hostHeader.length() - 17)); toSign.append("/").append(hostHeader.substring(0, hostHeader.length() - 17));
}
} }
private static void appendUriPath(HttpRequest request, StringBuilder toSign) { private static void appendUriPath(HttpRequest request, StringBuilder toSign) {
@ -174,21 +173,21 @@ public class RequestAuthorizeSignature implements HttpRequestFilter {
} else { } else {
toSign.append(request.getUri()); toSign.append(request.getUri());
} }
// ...however, there are a few exceptions that must be included in the signed URI. // ...however, there are a few exceptions that must be included in the signed URI.
if (paramsString != null) { if (paramsString != null) {
StringBuilder paramsToSign = new StringBuilder("?"); StringBuilder paramsToSign = new StringBuilder("?");
String[] params = paramsString.split("&"); String[] params = paramsString.split("&");
for (String param : params) { for (String param : params) {
String[] paramNameAndValue = param.split("="); String[] paramNameAndValue = param.split("=");
if ("acl".equals(paramNameAndValue[0])) { if ("acl".equals(paramNameAndValue[0])) {
paramsToSign.append("acl"); paramsToSign.append("acl");
} }
// TODO: Other special cases not yet handled: torrent, logging, location, requestPayment // TODO: Other special cases not yet handled: torrent, logging, location, requestPayment
} }
if (paramsToSign.length() > 1) { if (paramsToSign.length() > 1) {
toSign.append(paramsToSign); toSign.append(paramsToSign);
} }

View File

@ -41,13 +41,12 @@ import org.testng.annotations.Test;
*/ */
@Test(groups = "unit", testName = "s3.ParseObjectFromHeadersAndHttpContentTest") @Test(groups = "unit", testName = "s3.ParseObjectFromHeadersAndHttpContentTest")
public class ParseObjectFromHeadersAndHttpContentTest { public class ParseObjectFromHeadersAndHttpContentTest {
ParseObjectFromHeadersAndHttpContent callable;
ParseMetadataFromHeaders metadataParser;
@Test(expectedExceptions = IllegalStateException.class) @Test(expectedExceptions = IllegalStateException.class)
public void testCall() throws HttpException { public void testCall() throws HttpException {
metadataParser = createMock(ParseMetadataFromHeaders.class); ParseMetadataFromHeaders metadataParser = createMock(ParseMetadataFromHeaders.class);
callable = new ParseObjectFromHeadersAndHttpContent(metadataParser); ParseObjectFromHeadersAndHttpContent callable = new ParseObjectFromHeadersAndHttpContent(
metadataParser);
HttpResponse response = createMock(HttpResponse.class); HttpResponse response = createMock(HttpResponse.class);
expect(response.getStatusCode()).andReturn(409).atLeastOnce(); expect(response.getStatusCode()).andReturn(409).atLeastOnce();
expect(response.getContent()).andReturn(null); expect(response.getContent()).andReturn(null);
@ -58,8 +57,9 @@ public class ParseObjectFromHeadersAndHttpContentTest {
@Test @Test
public void testParseContentLengthWhenContentRangeSet() throws HttpException { public void testParseContentLengthWhenContentRangeSet() throws HttpException {
metadataParser = createMock(ParseMetadataFromHeaders.class); ParseMetadataFromHeaders metadataParser = createMock(ParseMetadataFromHeaders.class);
callable = new ParseObjectFromHeadersAndHttpContent(metadataParser); ParseObjectFromHeadersAndHttpContent callable = new ParseObjectFromHeadersAndHttpContent(
metadataParser);
HttpResponse response = createMock(HttpResponse.class); HttpResponse response = createMock(HttpResponse.class);
metadataParser.setResponse(response); metadataParser.setResponse(response);
Metadata meta = createMock(Metadata.class); Metadata meta = createMock(Metadata.class);