Added putObject implementation to JetS3t service

git-svn-id: http://jclouds.googlecode.com/svn/trunk@879 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
jamurty 2009-05-29 23:31:42 +00:00
parent 4403887200
commit 7ac032015d
3 changed files with 205 additions and 48 deletions

View File

@ -35,6 +35,7 @@ import org.jclouds.aws.s3.S3Context;
import org.jclouds.aws.s3.S3ContextFactory;
import org.jclouds.aws.s3.commands.options.GetObjectOptions;
import org.jclouds.aws.s3.commands.options.ListBucketOptions;
import org.jclouds.aws.s3.commands.options.PutObjectOptions;
import org.jclouds.util.Utils;
import org.jets3t.service.S3ObjectsChunk;
import org.jets3t.service.S3Service;
@ -304,9 +305,17 @@ public class JCloudsS3Service extends S3Service {
}
@Override
protected S3Object putObjectImpl(String bucketName, S3Object object) throws S3ServiceException {
// TODO Unimplemented
return null;
protected S3Object putObjectImpl(String bucketName, S3Object jsObject) throws S3ServiceException {
try {
PutObjectOptions options = Util.convertPutObjectOptions(jsObject.getAcl());
org.jclouds.aws.s3.domain.S3Object jcObject = Util.convertObject(jsObject);
byte md5[] = connection.putObject(bucketName, jcObject, options)
.get(requestTimeoutMilliseconds, TimeUnit.MILLISECONDS);
return jsObject;
} catch (Exception e) {
Utils.<S3ServiceException> rethrowIfRuntimeOrSameType(e);
throw new S3ServiceException("error putting object", e);
}
}
@Override

View File

@ -28,17 +28,26 @@ import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;
import org.jclouds.aws.s3.commands.options.GetObjectOptions;
import org.jclouds.aws.s3.commands.options.ListBucketOptions;
import org.jclouds.aws.s3.commands.options.PutObjectOptions;
import org.jclouds.aws.s3.domain.CanonicalUser;
import org.jclouds.aws.s3.domain.acl.CannedAccessPolicy;
import org.jclouds.aws.s3.reference.S3Constants;
import org.jclouds.aws.s3.util.S3Utils;
import org.jclouds.aws.util.DateService;
import org.jclouds.http.ContentTypes;
import org.jets3t.service.S3ServiceException;
import org.jets3t.service.acl.AccessControlList;
import org.jets3t.service.model.S3Bucket;
import org.jets3t.service.model.S3Object;
import org.jets3t.service.model.S3Owner;
import org.jets3t.service.utils.RestUtils;
import org.joda.time.DateTime;
import com.google.common.collect.Iterables;
@ -128,6 +137,61 @@ public class Util {
return (S3Object[]) jsObjects.toArray(new S3Object[jsObjects.size()]);
}
public static org.jclouds.aws.s3.domain.S3Object convertObject(S3Object jsObject)
throws S3ServiceException
{
org.jclouds.aws.s3.domain.S3Object jcObject =
new org.jclouds.aws.s3.domain.S3Object(jsObject.getKey());
jcObject.getMetadata().setCacheControl(
(String) jsObject.getMetadata("Cache-Control"));
jcObject.getMetadata().setContentDisposition(jsObject.getContentDisposition());
jcObject.getMetadata().setContentEncoding(jsObject.getContentEncoding());
jcObject.getMetadata().setLastModified(
new DateTime(jsObject.getLastModifiedDate()));
jcObject.getMetadata().setSize(jsObject.getContentLength());
jcObject.getMetadata().setStorageClass(jsObject.getStorageClass());
if (jsObject.getMd5HashAsHex() != null) {
jcObject.getMetadata().setMd5(
S3Utils.fromHexString(jsObject.getMd5HashAsHex()));
}
if (jsObject.getOwner() != null) {
jcObject.getMetadata().setOwner(new CanonicalUser(jsObject.getOwner().getId()));
}
if (jsObject.getDataInputStream() != null) {
jcObject.setData(jsObject.getDataInputStream());
} else {
jcObject.setData(""); // Must explicitly set data for empty jClouds objects.
}
if (jsObject.getContentType() != null) {
jcObject.getMetadata().setContentType(jsObject.getContentType());
} else {
// Must always provide a content type for jClouds objects.
jcObject.getMetadata().setContentType(ContentTypes.BINARY);
jsObject.setContentType(ContentTypes.BINARY);
}
// User metadata
DateService dateService = new DateService();
for (Object maybeUserMetadataObj : jsObject.getMetadataMap().entrySet())
{
String name = ((Entry<String, Object>) maybeUserMetadataObj).getKey();
Object value = ((Entry<String, Object>) maybeUserMetadataObj).getValue();
if (!RestUtils.HTTP_HEADER_METADATA_NAMES.contains(name.toLowerCase())) {
if (value instanceof Date) {
value = dateService.rfc822DateFormat((Date)value);
}
jcObject.getMetadata().getUserMetadata().put(name, (String)value);
}
}
return jcObject;
}
public static GetObjectOptions convertGetObjectOptions(Calendar ifModifiedSince,
Calendar ifUnmodifiedSince, String[] ifMatchTags, String[] ifNoneMatchTags)
throws UnsupportedEncodingException
@ -168,5 +232,19 @@ public class Util {
}
return options;
}
public static PutObjectOptions convertPutObjectOptions(AccessControlList acl) {
PutObjectOptions options = new PutObjectOptions();
if (acl == AccessControlList.REST_CANNED_AUTHENTICATED_READ) {
options.withAcl(CannedAccessPolicy.AUTHENTICATED_READ);
} else if (acl == AccessControlList.REST_CANNED_PRIVATE) {
options.withAcl(CannedAccessPolicy.PRIVATE);
} else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ) {
options.withAcl(CannedAccessPolicy.PUBLIC_READ);
} else if (acl == AccessControlList.REST_CANNED_PUBLIC_READ_WRITE) {
options.withAcl(CannedAccessPolicy.PUBLIC_READ_WRITE);
}
return options;
}
}

View File

@ -31,6 +31,7 @@ import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutionException;
@ -41,9 +42,11 @@ import org.apache.commons.io.IOUtils;
import org.jclouds.aws.s3.S3IntegrationTest;
import org.jclouds.aws.s3.config.StubS3ConnectionModule;
import org.jclouds.aws.s3.reference.S3Constants;
import org.jclouds.http.ContentTypes;
import org.jets3t.service.S3ObjectsChunk;
import org.jets3t.service.S3Service;
import org.jets3t.service.S3ServiceException;
import org.jets3t.service.acl.AccessControlList;
import org.jets3t.service.model.S3Bucket;
import org.jets3t.service.model.S3Object;
import org.jets3t.service.security.AWSCredentials;
@ -51,6 +54,8 @@ import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.google.common.collect.Iterables;
/**
* Tests to cover JCloudsS3Service
*
@ -95,16 +100,6 @@ public class JCloudsS3ServiceIntegrationTest extends S3IntegrationTest {
new JCloudsS3Service(new AWSCredentials("foo", "bar"), new StubS3ConnectionModule());
}
@Test(enabled = false)
public void testCheckBucketStatusString() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testCopyObjectImplStringStringStringStringAccessControlListMapCalendarCalendarStringArrayStringArray() {
fail("Not yet implemented");
}
@Test
public void testCreateBucketImpl()
throws S3ServiceException, InterruptedException, ExecutionException
@ -143,26 +138,6 @@ public class JCloudsS3ServiceIntegrationTest extends S3IntegrationTest {
org.jclouds.aws.s3.domain.S3Object.Metadata.NOT_FOUND);
}
@Test(enabled = false)
public void testGetBucketAclImplString() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testGetBucketLocationImplString() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testGetBucketLoggingStatusImplString() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testGetObjectAclImplStringString() {
fail("Not yet implemented");
}
@Test(dependsOnMethods = "testCreateBucketImpl")
public void testGetObjectDetailsImpl()
throws InterruptedException, ExecutionException, TimeoutException,
@ -218,16 +193,6 @@ public class JCloudsS3ServiceIntegrationTest extends S3IntegrationTest {
emptyBucket(commonTestingBucketName);
}
@Test(enabled = false)
public void testIsBucketAccessibleString() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testIsRequesterPaysBucketImplString() {
fail("Not yet implemented");
}
@Test(dependsOnMethods = "testCreateBucketImpl")
public void testListAllBucketsImpl() throws InterruptedException,
ExecutionException, TimeoutException, S3ServiceException {
@ -369,28 +334,133 @@ public class JCloudsS3ServiceIntegrationTest extends S3IntegrationTest {
emptyBucket(commonTestingBucketName);
}
@Test(dependsOnMethods = "testCreateBucketImpl")
public void testPutObjectImpl() throws S3ServiceException, InterruptedException,
ExecutionException, TimeoutException, NoSuchAlgorithmException, IOException
{
String objectKey = "putObject";
S3Object requestObject, jsResultObject;
org.jclouds.aws.s3.domain.S3Object jcObject;
// Upload empty object
requestObject = new S3Object(objectKey);
jsResultObject = service.putObject(new S3Bucket(commonTestingBucketName), requestObject);
jcObject = client.getObject(commonTestingBucketName, objectKey).get(10, TimeUnit.SECONDS);
assertEquals(jcObject.getKey(), objectKey);
assertEquals(jcObject.getMetadata().getSize(), 0);
assertEquals(jcObject.getMetadata().getContentType(), ContentTypes.BINARY);
assertEquals(jsResultObject.getKey(), requestObject.getKey());
assertEquals(jsResultObject.getContentLength(), 0);
assertEquals(jsResultObject.getContentType(), ContentTypes.BINARY);
// Upload unicode-named object
requestObject = new S3Object("Ÿn<EFBFBD>˜dŽ-object");
jsResultObject = service.putObject(new S3Bucket(commonTestingBucketName), requestObject);
jcObject = client.getObject(commonTestingBucketName, requestObject.getKey())
.get(10, TimeUnit.SECONDS);
assertEquals(jcObject.getKey(), requestObject.getKey());
assertEquals(jcObject.getMetadata().getSize(), 0);
assertEquals(jcObject.getMetadata().getContentType(), ContentTypes.BINARY);
assertEquals(jsResultObject.getKey(), requestObject.getKey());
assertEquals(jsResultObject.getContentLength(), 0);
assertEquals(jsResultObject.getContentType(), ContentTypes.BINARY);
// Upload string object
String data = "This is my Ÿn<6E>˜dŽ data";
requestObject = new S3Object(objectKey, data);
jsResultObject = service.putObject(new S3Bucket(commonTestingBucketName), requestObject);
jcObject = client.getObject(commonTestingBucketName, objectKey).get(10, TimeUnit.SECONDS);
assertEquals(jcObject.getMetadata().getSize(), data.getBytes("UTF-8").length);
assertTrue(jcObject.getMetadata().getContentType().startsWith("text/plain"));
assertEquals(jsResultObject.getContentLength(), data.getBytes("UTF-8").length);
assertTrue(jsResultObject.getContentType().startsWith("text/plain"));
// Upload object with metadata
requestObject = new S3Object(objectKey);
requestObject.addMetadata(S3Constants.USER_METADATA_PREFIX + "my-metadata-1", "value-1");
jsResultObject = service.putObject(new S3Bucket(commonTestingBucketName), requestObject);
jcObject = client.getObject(commonTestingBucketName, objectKey).get(10, TimeUnit.SECONDS);
assertEquals(Iterables.getLast(jcObject.getMetadata().getUserMetadata()
.get(S3Constants.USER_METADATA_PREFIX + "my-metadata-1")), "value-1");
assertEquals(
jsResultObject.getMetadata(S3Constants.USER_METADATA_PREFIX + "my-metadata-1"),
"value-1");
// Upload object with public-read ACL
requestObject = new S3Object(objectKey);
requestObject.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
jsResultObject = service.putObject(new S3Bucket(commonTestingBucketName), requestObject);
jcObject = client.getObject(commonTestingBucketName, objectKey).get(10, TimeUnit.SECONDS);
// TODO: No way yet to get/lookup ACL from jClouds object
// assertEquals(jcObject.getAcl(), CannedAccessPolicy.PUBLIC_READ);
assertEquals(jsResultObject.getAcl(), AccessControlList.REST_CANNED_PUBLIC_READ);
// TODO : Any way to test a URL lookup that works for live and stub testing?
// URL publicUrl = new URL(
// "http://" + commonTestingBucketName + ".s3.amazonaws.com:80/" + requestObject.getKey());
// assertEquals(((HttpURLConnection) publicUrl.openConnection()).getResponseCode(), 200);
emptyBucket(commonTestingBucketName);
}
@Test(enabled = false)
public void testPutBucketAclImplStringAccessControlList() {
public void testCopyObjectImpl() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testPutObjectAclImplStringStringAccessControlList() {
public void testCheckBucketStatus() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testPutObjectImplStringS3Object() {
public void testGetBucketAclImpl() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testSetBucketLoggingStatusImplStringS3BucketLoggingStatus() {
public void testGetBucketLocationImpl() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testSetRequesterPaysBucketImplStringBoolean() {
public void testGetBucketLoggingStatus() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testGetObjectAclImpl() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testPutBucketAclImpl() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testPutObjectAclImpl() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testSetBucketLoggingStatusImpl() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testSetRequesterPaysBucketImpl() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testIsBucketAccessible() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testIsRequesterPaysBucketImpl() {
fail("Not yet implemented");
}