Example usage for com.amazonaws.services.s3 AmazonS3ClientBuilder defaultClient

List of usage examples for com.amazonaws.services.s3 AmazonS3ClientBuilder defaultClient

Introduction

In this page you can find the example usage for com.amazonaws.services.s3 AmazonS3ClientBuilder defaultClient.

Prototype

public static AmazonS3 defaultClient() 

Source Link

Usage

From source file:GetS3Object.java

License:Open Source License

public static void main(String[] args) throws IOException {
    AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();

    try {/*from   w  ww.  j a  va 2  s . co m*/
        System.out.println("Downloading an object");
        S3Object s3object = s3Client.getObject(new GetObjectRequest(bucketName, key));
        displayTextInputStream(s3object.getObjectContent());
    } catch (AmazonServiceException ase) {
        System.err.println("Exception was thrown by the service");
    } catch (AmazonClientException ace) {
        System.err.println("Exception was thrown by the client");
    }
}

From source file:aws.example.s3.DeleteBucketPolicy.java

License:Open Source License

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    DeleteBucketPolicy <bucket>\n\n" + "Where:\n"
            + "    bucket - the bucket to delete the policy from.\n\n" + "Example:\n"
            + "    DeleteBucketPolicy testbucket\n\n";

    if (args.length < 1) {
        System.out.println(USAGE);
        System.exit(1);/*from  ww  w .ja v a2s  .c  om*/
    }

    String bucket_name = args[0];
    String policy_text = null;

    System.out.format("Deleting policy from bucket: \"%s\"\n\n", bucket_name);

    final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
    try {
        s3.deleteBucketPolicy(bucket_name);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }

    System.out.println("Done!");
}

From source file:aws.example.s3.DeleteWebsiteConfiguration.java

License:Open Source License

public static void deleteWebsiteConfig(String bucket_name) {
    final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
    try {/*from w  w  w  .j  a v a 2s.co  m*/
        s3.deleteBucketWebsiteConfiguration(bucket_name);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.out.println("Failed to delete website configuration!");
        System.exit(1);
    }
}

From source file:aws.example.s3.GetAcl.java

License:Open Source License

public static void getBucketAcl(String bucket_name) {
    System.out.println("Retrieving ACL for bucket: " + bucket_name);

    final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
    try {//from w w w .j a  v a 2s . c  o  m
        AccessControlList acl = s3.getBucketAcl(bucket_name);
        List<Grant> grants = acl.getGrantsAsList();
        for (Grant grant : grants) {
            System.out.format("  %s: %s\n", grant.getGrantee().getIdentifier(),
                    grant.getPermission().toString());
        }
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
}

From source file:aws.example.s3.GetAcl.java

License:Open Source License

public static void getObjectAcl(String bucket_name, String object_key) {
    System.out.println("Retrieving ACL for object: " + object_key);
    System.out.println("                in bucket: " + bucket_name);

    final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
    try {/*from  ww w  .j a va2s.  co  m*/
        AccessControlList acl = s3.getObjectAcl(bucket_name, object_key);
        List<Grant> grants = acl.getGrantsAsList();
        for (Grant grant : grants) {
            System.out.format("  %s: %s\n", grant.getGrantee().getIdentifier(),
                    grant.getPermission().toString());
        }
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
}

From source file:aws.example.s3.GetBucketPolicy.java

License:Open Source License

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    GetBucketPolicy <bucket>\n\n" + "Where:\n"
            + "    bucket - the bucket to get the policy from.\n\n" + "Example:\n"
            + "    GetBucketPolicy testbucket\n\n";

    if (args.length < 1) {
        System.out.println(USAGE);
        System.exit(1);//from  w w  w  .  j  a  v  a  2s . c  o m
    }

    String bucket_name = args[0];
    String policy_text = null;

    System.out.format("Getting policy for bucket: \"%s\"\n\n", bucket_name);

    final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
    try {
        BucketPolicy bucket_policy = s3.getBucketPolicy(bucket_name);
        policy_text = bucket_policy.getPolicyText();
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }

    if (policy_text == null) {
        System.out.println("The specified bucket has no bucket policy.");
    } else {
        System.out.println("Returned policy:");
        System.out.println("----");
        System.out.println(policy_text);
        System.out.println("----\n");
    }

    System.out.println("Done!");
}

From source file:aws.example.s3.GetWebsiteConfiguration.java

License:Open Source License

public static void getWebsiteConfig(String bucket_name) {
    final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
    try {/*from w  ww  .  ja  v a2s.  c  o m*/
        BucketWebsiteConfiguration config = s3.getBucketWebsiteConfiguration(bucket_name);
        if (config == null) {
            System.out.println("No website configuration found!");
        } else {
            System.out.format("Index document: %s\n", config.getIndexDocumentSuffix());
            System.out.format("Error document: %s\n", config.getErrorDocument());
        }
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.out.println("Failed to get website configuration!");
        System.exit(1);
    }
}

From source file:aws.example.s3.SetAcl.java

License:Open Source License

public static void setBucketAcl(String bucket_name, String email, String access) {
    System.out.format("Setting %s access for %s\n", access, email);
    System.out.println("on bucket: " + bucket_name);

    final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
    try {/*ww  w  .  ja va2s  .  co  m*/
        // get the current ACL
        AccessControlList acl = s3.getBucketAcl(bucket_name);
        // set access for the grantee
        EmailAddressGrantee grantee = new EmailAddressGrantee(email);
        Permission permission = Permission.valueOf(access);
        acl.grantPermission(grantee, permission);
        s3.setBucketAcl(bucket_name, acl);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
}

From source file:aws.example.s3.SetAcl.java

License:Open Source License

public static void setObjectAcl(String bucket_name, String object_key, String email, String access) {
    System.out.format("Setting %s access for %s\n", access, email);
    System.out.println("for object: " + object_key);
    System.out.println(" in bucket: " + bucket_name);

    final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
    try {/*from  ww  w .java  2s  .c  o m*/
        // get the current ACL
        AccessControlList acl = s3.getObjectAcl(bucket_name, object_key);
        // set access for the grantee
        EmailAddressGrantee grantee = new EmailAddressGrantee(email);
        Permission permission = Permission.valueOf(access);
        acl.grantPermission(grantee, permission);
        s3.setObjectAcl(bucket_name, object_key, acl);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
}

From source file:aws.example.s3.SetBucketPolicy.java

License:Open Source License

public static void setBucketPolicy(String bucket_name, String policy_text) {
    final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
    try {/*from   w ww.j  a v  a 2s  .  c  o  m*/
        s3.setBucketPolicy(bucket_name, policy_text);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
}