Example usage for com.amazonaws.auth ClasspathPropertiesFileCredentialsProvider getCredentials

List of usage examples for com.amazonaws.auth ClasspathPropertiesFileCredentialsProvider getCredentials

Introduction

In this page you can find the example usage for com.amazonaws.auth ClasspathPropertiesFileCredentialsProvider getCredentials.

Prototype

public AWSCredentials getCredentials() 

Source Link

Usage

From source file:com.enitalk.configs.AwsConfig.java

@Bean
public AWSCredentials awsCredentials() {
    ClasspathPropertiesFileCredentialsProvider credentials = new ClasspathPropertiesFileCredentialsProvider(
            "AwsS3Credentials.properties");
    return credentials.getCredentials();
}

From source file:com.intuit.s3encrypt.S3Encrypt.java

License:Open Source License

public static void main(String[] args) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {

    // create Options object
    Options options = new Options();
    options.addOption(create_bucket);//www . ja  v a 2s  .  c o  m
    options.addOption(create_key);
    options.addOption(delete_bucket);
    options.addOption(get);
    options.addOption(help);
    options.addOption(inspect);
    options.addOption(keyfile);
    options.addOption(list_buckets);
    options.addOption(list_objects);
    options.addOption(put);
    options.addOption(remove);
    options.addOption(rotate);
    options.addOption(rotateall);
    options.addOption(rotateKey);

    //      CommandLineParser parser = new GnuParser();
    //       Changed from above GnuParser to below PosixParser because I found code which allows for multiple arguments 
    PosixParser parser = new PosixParser();
    CommandLine cmd;
    try {
        cmd = parser.parse(options, args);
        Logger.getRootLogger().setLevel(Level.OFF);

        if (cmd.hasOption("help")) {
            HelpFormatter help = new HelpFormatter();
            System.out.println();
            help.printHelp("S3Encrypt", options);
            System.out.println();
            System.exit(1);
        } else if (cmd.hasOption("create_key")) {
            keyname = cmd.getOptionValue("keyfile");
            createKeyFile(keyname);
            key = new File(keyname);
        } else {
            if (cmd.hasOption("keyfile")) {
                keyname = cmd.getOptionValue("keyfile");
            }
            key = new File(keyname);
        }

        if (!(key.exists())) {
            System.out.println("Key does not exist or not provided");
            System.exit(1);
        }

        //         AmazonS3 s3 = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider());
        ClasspathPropertiesFileCredentialsProvider credentials = new ClasspathPropertiesFileCredentialsProvider(
                ".s3encrypt");
        EncryptionMaterials encryptionMaterials = new EncryptionMaterials(getKeyFile(keyname));
        AmazonS3EncryptionClient s3 = new AmazonS3EncryptionClient(credentials.getCredentials(),
                encryptionMaterials);
        //          Region usWest2 = Region.getRegion(Regions.US_WEST_2);
        //          s3.setRegion(usWest2);

        if (cmd.hasOption("create_bucket")) {
            String bucket = cmd.getOptionValue("create_bucket");
            System.out.println("Creating bucket " + bucket + "\n");
            s3.createBucket(bucket);
        } else if (cmd.hasOption("delete_bucket")) {
            String bucket = cmd.getOptionValue("delete_bucket");
            System.out.println("Deleting bucket " + bucket + "\n");
            s3.deleteBucket(bucket);
        } else if (cmd.hasOption("get")) {
            String[] searchArgs = cmd.getOptionValues("get");
            String bucket = searchArgs[0];
            String filename = searchArgs[1];
            getS3Object(cmd, s3, bucket, filename);
        } else if (cmd.hasOption("inspect")) {
            String[] searchArgs = cmd.getOptionValues("inspect");
            String bucket = searchArgs[0];
            String filename = searchArgs[1];
            String keyname = "encryption_key";
            String metadata = inspectS3Object(cmd, s3, bucket, filename, keyname);
            System.out.println(metadata);
        } else if (cmd.hasOption("list_buckets")) {
            System.out.println("Listing buckets");
            for (Bucket bucket : s3.listBuckets()) {
                System.out.println(bucket.getName());
            }
            System.out.println();
        } else if (cmd.hasOption("list_objects")) {
            String bucket = cmd.getOptionValue("list_objects");
            System.out.println("Listing objects");
            ObjectListing objectListing = s3.listObjects(new ListObjectsRequest().withBucketName(bucket));
            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                System.out.println(objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");
            }
            System.out.println();
        } else if (cmd.hasOption("put")) {
            String[] searchArgs = cmd.getOptionValues("put");
            String bucket = searchArgs[0];
            String filename = searchArgs[1];
            String metadataKeyname = "encryption_key";
            String key = keyname;
            putS3Object(cmd, s3, bucket, filename, metadataKeyname, key);
        } else if (cmd.hasOption("remove")) {
            String[] searchArgs = cmd.getOptionValues("remove");
            String bucket = searchArgs[0];
            String filename = searchArgs[1];
            System.out.println("Removing object in S3 from BUCKET = " + bucket + " FILENAME = " + filename);
            s3.deleteObject(new DeleteObjectRequest(bucket, filename));
            System.out.println();
        } else if (cmd.hasOption("rotate")) {
            String[] searchArgs = cmd.getOptionValues("rotate");
            String bucket = searchArgs[0];
            String filename = searchArgs[1];
            String key1 = cmd.getOptionValue("keyfile");
            String key2 = cmd.getOptionValue("rotateKey");
            String metadataKeyname = "encryption_key";
            System.out.println("Supposed to get object from here OPTION VALUE = " + bucket + " FILENAME = "
                    + filename + " KEY1 = " + key1 + " KEY2 = " + key2);

            EncryptionMaterials rotateEncryptionMaterials = new EncryptionMaterials(getKeyFile(key2));
            AmazonS3EncryptionClient rotateS3 = new AmazonS3EncryptionClient(credentials.getCredentials(),
                    rotateEncryptionMaterials);

            getS3Object(cmd, s3, bucket, filename);
            putS3Object(cmd, rotateS3, bucket, filename, metadataKeyname, key2);
        } else if (cmd.hasOption("rotateall")) {
            String[] searchArgs = cmd.getOptionValues("rotateall");
            String bucket = searchArgs[0];
            String key1 = searchArgs[1];
            String key2 = searchArgs[2];
            System.out.println("Supposed to rotateall here for BUCKET NAME = " + bucket + " KEY1 = " + key1
                    + " KEY2 = " + key2);
        } else {
            System.out.println("Something went wrong... ");
            System.exit(1);
        }

    } catch (ParseException e) {
        e.printStackTrace();
    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which " + "means your request made it "
                + "to Amazon S3, but was rejected with an error response" + " for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, which " + "means the client encountered "
                + "an internal error while trying to " + "communicate with S3, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }

}