Package org.jets3t.service

Examples of org.jets3t.service.S3Service


//        s3Service.deleteBucket(bucket.getName());
    }

    public void testUrlSigning() throws Exception {
        S3Service s3Service = getS3Service(awsCredentials);

        String bucketName = awsCredentials.getAccessKey() + ".jets3t_TestCases";

        S3Bucket bucket = s3Service.createBucket(bucketName);
       
        // Create test object, with private ACL
        String dataString = "Text for the URL Signing test object...";
        S3Object object = new S3Object(bucket, "Testing URL Signing", dataString);
        object.setContentType("text/html");
        object.addMetadata("x-amz-example-header", "example-value");
        object.setAcl(AccessControlList.REST_CANNED_PRIVATE);

        // Determine what the time will be in 5 minutes.
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MINUTE, 5);
        Date expiryDate = cal.getTime();

        // Create a signed HTTP PUT URL.
        String signedPutUrl = S3Service.createSignedPutUrl(bucket.getName(), object.getKey(),
            object.getMetadataMap(), awsCredentials, expiryDate, false);

        // Put the object in S3 using the signed URL (no AWS credentials required)
        RestS3Service restS3Service = new RestS3Service(null);
        restS3Service.putObjectWithSignedUrl(signedPutUrl, object);
       
        // Ensure the object was created.
        S3Object objects[] = s3Service.listObjects(bucket, object.getKey(), null);
        assertEquals("Signed PUT URL failed to put/create object", objects.length, 1);

        // Change the object's content-type and ensure the signed PUT URL disallows the put.
        object.setContentType("application/octet-stream");
        try {
            restS3Service.putObjectWithSignedUrl(signedPutUrl, object);
            fail("Should not be able to use a signed URL for an object with a changed content-type");
        } catch (S3ServiceException e) {           
            object.setContentType("text/html");
        }
       
        // Add an object header and ensure the signed PUT URL disallows the put.
        object.addMetadata("x-amz-example-header-2", "example-value");
        try {
            restS3Service.putObjectWithSignedUrl(signedPutUrl, object);
            fail("Should not be able to use a signed URL for an object with changed metadata");
        } catch (S3ServiceException e) {
            object.removeMetadata("x-amz-example-header-2");
        }

        // Change the object's name and ensure the signed PUT URL uses the signed name, not the object name.
        String originalName = object.getKey();
        object.setKey("Testing URL Signing 2");
        object.setDataInputStream(new ByteArrayInputStream(dataString.getBytes()));
        S3Object renamedObject = restS3Service.putObjectWithSignedUrl(signedPutUrl, object);
        assertEquals("Ensure returned object key is renamed based on signed PUT URL",
            originalName, renamedObject.getKey());

        // Ensure we can't get the object with a normal URL.
        String s3Url = "https://s3.amazonaws.com";
        URL url = new URL(s3Url + "/" + bucketName + "/" + RestUtils.encodeUrlString(object.getKey()));
        assertEquals("Expected denied access (403) error", 403, ((HttpURLConnection) url
            .openConnection()).getResponseCode());

        // Create a signed HTTP GET URL.
        String signedGetUrl = S3Service.createSignedGetUrl(bucket.getName(), object.getKey(),
            awsCredentials, expiryDate, false);

        // Ensure the signed URL can retrieve the object.
        url = new URL(signedGetUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        assertEquals("Expected signed GET URL ("+ signedGetUrl +") to retrieve object with response code 200",
            200, conn.getResponseCode());
       
        // Sanity check the data in the S3 object.
        String objectData = (new BufferedReader(
            new InputStreamReader(conn.getInputStream())))
            .readLine();
        assertEquals("Unexpected data content in S3 object", dataString, objectData);
       
        // Clean up.
        s3Service.deleteObject(bucket, object.getKey());
       
//        s3Service.deleteBucket(bucket.getName());
    }
View Full Code Here


       
//        s3Service.deleteBucket(bucket.getName());
    }
   
    public void testHashVerifiedUploads() throws Exception {
        S3Service s3Service = getS3Service(awsCredentials);

        String bucketName = awsCredentials.getAccessKey() + ".jets3t_TestCases";

        S3Bucket bucket = s3Service.createBucket(bucketName);
       
        // Create test object with an MD5 hash of the data.
        String dataString = "Text for MD5 hashing...";
        S3Object object = new S3Object(bucket, "Testing MD5 Hashing", dataString);       
        object.setContentType("text/plain");
       
        // Calculate hash data for object.
        byte[] md5Hash = ServiceUtils.computeMD5Hash(dataString.getBytes());

        // Ensure that using an invalid hash value fails.
        try {
            object.addMetadata("Content-MD5", "123");
            s3Service.putObject(bucket, object);
            fail("Should have failed due to invalid hash value");
        } catch (S3ServiceException e) {
            // This error check would be nice to have, but it only works for the REST interface, not SOAP.
            // assertEquals("Expected error code indicating invalid md5 hash", "InvalidDigest", e.getErrorCode());
        }
        object = new S3Object(bucket, "Testing MD5 Hashing", dataString);       
       
        // Ensure that using the wrong hash value fails.
        try {
            byte[] incorrectHash = new byte[md5Hash.length];
            System.arraycopy(md5Hash, 0, incorrectHash, 0, incorrectHash.length);
            incorrectHash[0] = incorrectHash[1];
            object.setMd5Hash(incorrectHash);
            s3Service.putObject(bucket, object);
            fail("Should have failed due to incorrect hash value");
        } catch (S3ServiceException e) {
            // This error checks would be nice to have, but it only works for the REST interface, not SOAP.
            // assertEquals("Expected error code indicating invalid md5 hash", "BadDigest", e.getErrorCode());
        }
        object = new S3Object(bucket, "Testing MD5 Hashing", dataString);       

        // Ensure that correct hash value succeeds.
        object.setMd5Hash(md5Hash);
        S3Object resultObject = s3Service.putObject(bucket, object);
       
        // Ensure the ETag result matches the hex-encoded MD5 hash.
        assertEquals("Hex-encoded MD5 hash should match ETag", resultObject.getETag(),
            ServiceUtils.toHex(md5Hash));

        // Ensure we can convert the hex-encoded ETag to Base64 that matches the Base64 md5 hash.
        String md5HashBase64 = ServiceUtils.toBase64(md5Hash);
        String eTagBase64 = ServiceUtils.toBase64(ServiceUtils.fromHex(resultObject.getETag()));
        assertEquals("Could not convert ETag and MD5 hash to matching Base64-encoded strings",
            md5HashBase64, eTagBase64);

        // Clean up.
        s3Service.deleteObject(bucket, object.getKey());
       
//        s3Service.deleteBucket(bucket.getName());
    }
View Full Code Here

    protected S3Service getS3Service(AWSCredentials awsCredentials) throws S3ServiceException {
        return new RestS3Service(awsCredentials);
    }
   
    public void testRestCannedACL() throws Exception {
        S3Service s3Service = getS3Service(awsCredentials);
       
        // Create test bucket.
        String bucketName = awsCredentials.getAccessKey() + ".jets3t_TestCases";
        S3Bucket bucket = s3Service.createBucket(bucketName);
       
        // Try to create REST canned public object.       
        String publicKey = "PublicObject";
        S3Object object = new S3Object(publicKey);
        object.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
        object.setOwner(bucket.getOwner());
       
        try {
            s3Service.putObject(bucket, object);
            URL url = new URL("https://s3.amazonaws.com/" + bucketName + "/" + publicKey);     
            assertEquals("Expected public access (200)",
                    200, ((HttpURLConnection)url.openConnection()).getResponseCode());
        } finally {
            s3Service.deleteObject(bucket, object.getKey());
//            s3Service.deleteBucket(bucket.getName());
        }
    }
View Full Code Here

         */
        final String bucketName = "jets3t";
        final String delimiter = "/";
       
        AWSCredentials awsCredentials = SamplesUtils.loadAWSCredentials();
        S3Service restService = new RestS3Service(awsCredentials);
       
        final List allObjects = Collections.synchronizedList(new ArrayList());
        final Exception s3ServiceExceptions[] = new Exception[1];       

        /*
         * Identify top-level "subdirectory" names in a bucket by performing a
         * standard object listing with a delimiter string. 
         */
        long startTime = System.currentTimeMillis();
       
        // Find all the objects and common prefixes at the top level.
        S3ObjectsChunk initialChunk = restService.listObjectsChunked(bucketName, null, delimiter, 1000, null, true);
       
        long totalElapsedTime = System.currentTimeMillis() - startTime;
               
        // We will use the common prefix strings, if any, to perform sub-listings
        final String[] commonPrefixes = initialChunk.getCommonPrefixes();
View Full Code Here

    protected S3Service getS3Service(AWSCredentials awsCredentials) throws S3ServiceException {
        return new SoapS3Service(awsCredentials);
    }
   
    public void testIllegalACL() throws Exception {
        S3Service s3Service = getS3Service(awsCredentials);
       
        // Create test bucket.
        String bucketName = awsCredentials.getAccessKey() + ".jets3t_TestCases";
        S3Bucket bucket = s3Service.createBucket(bucketName);
       
        // Try to create illegal REST canned public object.       
        String publicKey = "PublicObject";
        S3Object object = new S3Object(publicKey);
        object.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
       
        try {
            s3Service.putObject(bucket, object);
            fail("Shouldn't be able to use REST canned ACL setting with SOAP service");
        } catch (S3ServiceException e) {
        } finally {
//            s3Service.deleteBucket(bucket.getName());
        }
View Full Code Here

     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        JFrame testFrame = new JFrame("Test");
        CloudFrontService cloudFrontService = new CloudFrontService(SamplesUtils.loadAWSCredentials());
        S3Service s3Service = new RestS3Service(SamplesUtils.loadAWSCredentials());
       
        S3Bucket[] buckets = s3Service.listAllBuckets();
        String[] bucketNames = new String[buckets.length];
        for (int i = 0; i < buckets.length; i++) {
            bucketNames[i] = buckets[i].getName();
        }
       
View Full Code Here

     * Get S3 Service
     *
     * @return S3Service
     */
    private S3Service getS3Service() {
        S3Service s3Service = null;
        try {
            s3Service = new RestS3Service(new AWSCredentials(AmazonCredential.getInstance().getAwsAccessKeyId(),
                    AmazonCredential.getInstance().getAwsSecretAccessKey()));
        } catch (S3ServiceException s3ex) {
            xBayaEngine.getErrorWindow().error(s3ex);
View Full Code Here

                S3FileChooser.this.s3Tree.clean();

                try {

                    // create S3Service
                    S3Service s3Service = new RestS3Service(new AWSCredentials(AmazonCredential.getInstance()
                            .getAwsAccessKeyId(), AmazonCredential.getInstance().getAwsSecretAccessKey()));

                    BucketsLoader bucketsLoader = new BucketsLoader(S3FileChooser.this.engine,
                            S3FileChooser.this.dialog.getDialog());
                    bucketsLoader.load(s3Service, S3FileChooser.this.s3Tree);
View Full Code Here

            amazonCloudFormationClient = new AmazonCloudFormationClient(awsCredentials);
        }
    }

    private boolean hasExistingProperties() {
        S3Service s3Service = getS3Service();
        boolean isObjectInBucket;
        try {
            isObjectInBucket = s3Service.isObjectInBucket(bucketName, fileKey);
        } catch (ServiceException e) {
            log.error("Error caught attempting to check if " + fileKey + " exists in bucket " + bucketName);
            return true;
        }
View Full Code Here

        } catch (IOException e) {
            log.error("Error attempting to create S3 object " + fileKey + " for bucket " + bucketName, e);
            return;
        }

        S3Service s3Service = getS3Service();
        try {
            s3Service.putObject(bucketName, objectToSave);
        } catch (S3ServiceException e) {
            log.error("Error attempting to put S3 object " + fileKey + " in bucket " + bucketName, e);
        }

    }
View Full Code Here

TOP

Related Classes of org.jets3t.service.S3Service

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.