Example usage for com.amazonaws.auth BasicAWSCredentials BasicAWSCredentials

List of usage examples for com.amazonaws.auth BasicAWSCredentials BasicAWSCredentials

Introduction

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

Prototype

public BasicAWSCredentials(String accessKey, String secretKey) 

Source Link

Document

Constructs a new BasicAWSCredentials object, with the specified AWS access key and AWS secret key.

Usage

From source file:baldrickv.s3streamingtool.S3StreamingTool.java

License:Open Source License

public static void main(String args[]) throws Exception {
    BasicParser p = new BasicParser();

    Options o = getOptions();/*from ww  w. jav a2  s  .c o  m*/

    CommandLine cl = p.parse(o, args);

    if (cl.hasOption('h')) {
        HelpFormatter hf = new HelpFormatter();
        hf.setWidth(80);

        StringBuilder sb = new StringBuilder();

        sb.append("\n");
        sb.append("Upload:\n");
        sb.append("    -u -r creds -s 50M -b my_bucket -f hda1.dump -t 10\n");
        sb.append("Download:\n");
        sb.append("    -d -r creds -s 50M -b my_bucket -f hda1.dump -t 10\n");
        sb.append("Upload encrypted:\n");
        sb.append("    -u -r creds -z -k secret_key -s 50M -b my_bucket -f hda1.dump -t 10\n");
        sb.append("Download encrypted:\n");
        sb.append("    -d -r creds -z -k secret_key -s 50M -b my_bucket -f hda1.dump -t 10\n");
        sb.append("Cleanup in-progress multipart uploads\n");
        sb.append("    -c -r creds -b my_bucket\n");
        System.out.println(sb.toString());

        hf.printHelp("See above", o);

        return;
    }

    int n = 0;
    if (cl.hasOption('d'))
        n++;
    if (cl.hasOption('u'))
        n++;
    if (cl.hasOption('c'))
        n++;
    if (cl.hasOption('m'))
        n++;

    if (n != 1) {
        System.err.println("Must specify at exactly one of -d, -u, -c or -m");
        System.exit(-1);
    }

    if (cl.hasOption('m')) {
        //InputStream in = new java.io.BufferedInputStream(System.in,1024*1024*2);
        InputStream in = System.in;
        System.out.println(TreeHashGenerator.calculateTreeHash(in));
        return;
    }

    require(cl, 'b');

    if (cl.hasOption('d') || cl.hasOption('u')) {
        require(cl, 'f');
    }
    if (cl.hasOption('z')) {
        require(cl, 'k');
    }

    AWSCredentials creds = null;

    if (cl.hasOption('r')) {
        creds = Utils.loadAWSCredentails(cl.getOptionValue('r'));
    } else {
        if (cl.hasOption('i') && cl.hasOption('e')) {
            creds = new BasicAWSCredentials(cl.getOptionValue('i'), cl.getOptionValue('e'));
        } else {

            System.out.println("Must specify either credential file (-r) or AWS key ID and secret (-i and -e)");
            System.exit(-1);
        }
    }

    S3StreamConfig config = new S3StreamConfig();
    config.setEncryption(false);
    if (cl.hasOption('z')) {
        config.setEncryption(true);
        config.setSecretKey(Utils.loadSecretKey(cl.getOptionValue("k")));
    }

    if (cl.hasOption("encryption-mode")) {
        config.setEncryptionMode(cl.getOptionValue("encryption-mode"));
    }
    config.setS3Bucket(cl.getOptionValue("bucket"));
    if (cl.hasOption("file")) {
        config.setS3File(cl.getOptionValue("file"));
    }

    if (cl.hasOption("threads")) {
        config.setIOThreads(Integer.parseInt(cl.getOptionValue("threads")));
    }

    if (cl.hasOption("blocksize")) {
        String s = cl.getOptionValue("blocksize");
        s = s.toUpperCase();
        int multi = 1;

        int end = 0;
        while ((end < s.length()) && (s.charAt(end) >= '0') && (s.charAt(end) <= '9')) {
            end++;
        }
        int size = Integer.parseInt(s.substring(0, end));

        if (end < s.length()) {
            String m = s.substring(end);
            if (m.equals("K"))
                multi = 1024;
            else if (m.equals("M"))
                multi = 1048576;
            else if (m.equals("G"))
                multi = 1024 * 1024 * 1024;
            else if (m.equals("KB"))
                multi = 1024;
            else if (m.equals("MB"))
                multi = 1048576;
            else if (m.equals("GB"))
                multi = 1024 * 1024 * 1024;
            else {
                System.out.println("Unknown suffix on block size.  Only K,M and G understood.");
                System.exit(-1);
            }

        }
        size *= multi;
        config.setBlockSize(size);
    }

    Logger.getLogger("").setLevel(Level.FINE);

    S3StreamingDownload.log.setLevel(Level.FINE);
    S3StreamingUpload.log.setLevel(Level.FINE);

    config.setS3Client(new AmazonS3Client(creds));
    config.setGlacierClient(new AmazonGlacierClient(creds));
    config.getGlacierClient().setEndpoint("glacier.us-west-2.amazonaws.com");

    if (cl.hasOption("glacier")) {
        config.setGlacier(true);
        config.setStorageInterface(new StorageGlacier(config.getGlacierClient()));
    } else {
        config.setStorageInterface(new StorageS3(config.getS3Client()));
    }
    if (cl.hasOption("bwlimit")) {
        config.setMaxBytesPerSecond(Double.parseDouble(cl.getOptionValue("bwlimit")));

    }

    if (cl.hasOption('c')) {
        if (config.getGlacier()) {
            GlacierCleanupMultipart.cleanup(config);
        } else {
            S3CleanupMultipart.cleanup(config);
        }
        return;
    }
    if (cl.hasOption('d')) {
        config.setOutputStream(System.out);
        S3StreamingDownload.download(config);
        return;
    }
    if (cl.hasOption('u')) {
        config.setInputStream(System.in);
        S3StreamingUpload.upload(config);
        return;
    }

}

From source file:baldrickv.s3streamingtool.Utils.java

License:Open Source License

public static AWSCredentials loadAWSCredentails(String file_path) throws java.io.IOException {
    FileInputStream fin = new FileInputStream(file_path);

    Scanner scan = new Scanner(fin);

    String id = scan.next();/*from   w  ww.  j  a  va  2s.  com*/
    String key = scan.next();

    fin.close();

    return new BasicAWSCredentials(id, key);

}

From source file:be.ugent.intec.halvade.uploader.AWSUploader.java

License:Open Source License

public AWSUploader(String existingBucketName, boolean sse, String profile) throws IOException {
    SSE = sse;//from www .j a  v  a2 s  .  c  o m
    this.profile = profile;
    this.existingBucketName = existingBucketName;
    AWSCredentials c;
    try {
        DefaultAWSCredentialsProviderChain prov = new DefaultAWSCredentialsProviderChain();
        c = prov.getCredentials();
    } catch (AmazonClientException ex) {
        // read from ~/.aws/credentials
        String access = null;
        String secret = null;
        try (BufferedReader br = new BufferedReader(
                new FileReader(System.getProperty("user.home") + "/.aws/credentials"))) {
            String line;
            while ((line = br.readLine()) != null && !line.contains("[" + this.profile + "]")) {
            }
            line = br.readLine();
            if (line != null)
                access = line.split(" = ")[1];
            line = br.readLine();
            if (line != null)
                secret = line.split(" = ")[1];
        }
        c = new BasicAWSCredentials(access, secret);
    }
    this.tm = new TransferManager(c);
}

From source file:be.ugent.intec.halvade.uploader.CopyOfAWSUploader.java

License:Open Source License

public CopyOfAWSUploader(String existingBucketName) throws IOException {
    this.existingBucketName = existingBucketName;
    AWSCredentials c;//from  www  .  j a  v a 2  s  .c o m
    try {
        DefaultAWSCredentialsProviderChain prov = new DefaultAWSCredentialsProviderChain();
        c = prov.getCredentials();
    } catch (AmazonClientException ex) {
        // read from ~/.aws/credentials
        String access = null;
        String secret = null;
        try (BufferedReader br = new BufferedReader(
                new FileReader(System.getProperty("user.home") + "/.aws/credentials"))) {
            String line;
            while ((line = br.readLine()) != null && !line.contains("[default]")) {
            }
            line = br.readLine();
            if (line != null)
                access = line.split(" = ")[1];
            line = br.readLine();
            if (line != null)
                secret = line.split(" = ")[1];
        }
        c = new BasicAWSCredentials(access, secret);
    }
    this.tm = new TransferManager(c);
}

From source file:beanstalk.BeansDatabase.java

License:Apache License

public DatabaseObject createDatabase(String AWSKeyId, String AWSSecretKey, String dbname, String dbType,
        String dbIdentifier, String dbClass, int dbSize, String dbUser, String dbPassword) {//throws Exception {
    DatabaseObject dbobj = new DatabaseObject();

    String endpoint_str = "";
    BasicAWSCredentials basic_credentials = new BasicAWSCredentials(AWSKeyId, AWSSecretKey);

    AmazonRDSClient rDSClient = new AmazonRDSClient(basic_credentials);

    //Minimum size per db type, as described in API Reference
    if (dbType != null && dbType.equalsIgnoreCase("MySQL") && dbSize < 5) {
        dbSize = 5;//from  w  w  w . j  a  v a2  s . c  o  m
    }

    if (dbType != null && (dbType.equalsIgnoreCase("oracle-se1") || dbType.equalsIgnoreCase("oracle-se")
            || dbType.equalsIgnoreCase("oracle-ee")) && dbSize < 10) {
        dbSize = 10;
    }

    CreateDBInstanceRequest create_dbinstance = new CreateDBInstanceRequest();
    create_dbinstance.setDBName(dbname);
    create_dbinstance.setEngine(dbType);
    create_dbinstance.setMasterUsername(dbUser);
    create_dbinstance.setMasterUserPassword(dbPassword);
    create_dbinstance.setDBInstanceIdentifier(dbIdentifier);
    create_dbinstance.setAllocatedStorage(dbSize);//min size =5GB for mysql,10gb for oracle
    create_dbinstance.setDBInstanceClass("db.m1.small");//db.m1.small//db.m1.large//db.m1.xlarge//db.m2.xlarge//db.m2.2xlarge//db.m2.4xlarge....
    String group = "c4sallowallipgroup";
    Vector sec_groups = new Vector();
    sec_groups.add(group);
    create_dbinstance.setDBSecurityGroups(sec_groups);
    DBInstance dbInstance_create = new DBInstance();
    System.out.println("will call createDBInstance");

    try {
        dbInstance_create = rDSClient.createDBInstance(create_dbinstance);
    } catch (AmazonClientException amazonClientException) {
        System.out.println("Error in Database Creation.");
    }
    System.out.println("called createDBInstance");

    String status = "";
    System.out.println("called createDBInstance");

    //wait 5 minutes for the first time
    int busyWaitingTime = 3000;//3sec
    //int busyWaitingTime=300000;
    //int busyWaitingTime=40000;  

    while (!status.equalsIgnoreCase("available")) {
        System.out.println("just got inside while");

        try {
            System.out.println("preparing for sleep");

            Thread.sleep(busyWaitingTime);
            System.out.println("woke up");

        } catch (InterruptedException ex) {
            Logger.getLogger(BeansDatabase.class.getName()).log(Level.SEVERE, null, ex);

        }

        DescribeDBInstancesRequest describeDBInstancesRequest = new DescribeDBInstancesRequest();

        describeDBInstancesRequest.setDBInstanceIdentifier(dbname + "cloud4soaid");
        //describeDBInstancesRequest.setDBInstanceIdentifier("c4sdb2cloud4soaid");
        DBInstance dbInstance = new DBInstance();

        System.out.println("will call describeDBInstances");
        DescribeDBInstancesResult describeDBInstances = rDSClient
                .describeDBInstances(describeDBInstancesRequest);
        System.out.println("called describeDBInstances");

        List<DBInstance> dbInstances = describeDBInstances.getDBInstances();

        System.out.println("size--->" + dbInstances.size());
        System.out.println("status--->" + dbInstances.get(0).getDBInstanceStatus());
        System.out.println("dbname--->" + dbInstances.get(0).getDBName());
        dbobj.setDbhost(dbInstances.get(0).getDBInstanceStatus());

        if (dbInstances.get(0).getEndpoint() != null) {

            System.out.println("endpoint--->" + dbInstances.get(0).getEndpoint().getAddress());
            System.out.println("port--->" + dbInstances.get(0).getEndpoint().getPort());
            System.out.println("all--->" + dbInstances.get(0).toString());

            dbobj.setDbhost(dbInstances.get(0).getEndpoint().getAddress());
            dbobj.setDbname(dbInstances.get(0).getDBName());
            dbobj.setPort(dbInstances.get(0).getEndpoint().getPort());

            status = dbInstances.get(0).getDBInstanceStatus();

            //after the first 5 minutes test every minute
            busyWaitingTime = 60000;
        }
    }
    System.out.println("just got outside while");
    System.out.println(
            "just got outside while and endpoint is :" + dbobj.getDbhost() + ", name is " + dbobj.getDbname());

    /*
     *
     * DescribeDBInstancesRequest describeDBInstancesRequest = new
     * DescribeDBInstancesRequest();
     *
     * describeDBInstancesRequest.setDBInstanceIdentifier(dbname +
     * "cloud4soaid");
     * //describeDBInstancesRequest.setDBInstanceIdentifier("c4sdb2cloud4soaid");
     * DBInstance dbInstance = new DBInstance();
     *
     * System.out.println("will call describeDBInstances");
     * DescribeDBInstancesResult describeDBInstances =
     * rDSClient.describeDBInstances(describeDBInstancesRequest);
     * System.out.println("called describeDBInstances");
     *
     * List<DBInstance> dbInstances = describeDBInstances.getDBInstances();
     *
     * System.out.println("size--->" + dbInstances.size());
     * System.out.println("status--->" +
     * dbInstances.get(0).getDBInstanceStatus());
     * System.out.println("dbname--->" + dbInstances.get(0).getDBName());
     * dbobj.setDbhost(dbInstances.get(0).getDBInstanceStatus());
     *
     *
     * if(dbInstances.get(0).getEndpoint()!=null){
     *
     *
     * System.out.println("endpoint--->" +
     * dbInstances.get(0).getEndpoint().getAddress());
     * System.out.println("port--->" +
     * dbInstances.get(0).getEndpoint().getPort());
     * System.out.println("all--->" + dbInstances.get(0).toString());
     *
     * dbobj.setDbhost(dbInstances.get(0).getEndpoint().getAddress());
     * dbobj.setDbname(dbInstances.get(0).getDBName());
     * dbobj.setPort(dbInstances.get(0).getEndpoint().getPort()); }
     */

    return dbobj;
}

From source file:beanstalk.BeansDatabase.java

License:Apache License

public DatabaseObject getDBInstanceInfo(String AWSKeyId, String AWSSecretKey, String dbname) {//throws Exception {
    BasicAWSCredentials basic_credentials = new BasicAWSCredentials(AWSKeyId, AWSSecretKey);
    AmazonRDSClient rDSClient = new AmazonRDSClient(basic_credentials);
    DescribeDBInstancesRequest describeDBInstancesRequest = new DescribeDBInstancesRequest();
    describeDBInstancesRequest.setDBInstanceIdentifier(dbname + "cloud4soaid");
    DBInstance dbInstance = new DBInstance();
    DescribeDBInstancesResult describeDBInstances = rDSClient.describeDBInstances(describeDBInstancesRequest);
    List<DBInstance> dbInstances = describeDBInstances.getDBInstances();

    System.out.println("size--->" + dbInstances.size());
    System.out.println("dbname--->" + dbInstances.get(0).getDBName());
    System.out.println("endpoint--->" + dbInstances.get(0).getEndpoint().getAddress());
    System.out.println("port--->" + dbInstances.get(0).getEndpoint().getPort());
    System.out.println("all--->" + dbInstances.get(0).toString());
    DatabaseObject dbobj = new DatabaseObject();
    dbobj.setDbhost(dbInstances.get(0).getEndpoint().getAddress());
    dbobj.setDbname(dbInstances.get(0).getDBName());
    dbobj.setPort(dbInstances.get(0).getEndpoint().getPort());

    return dbobj;

}

From source file:beanstalk.BeansDatabase.java

License:Apache License

public String getDBEndpoint(String AWSKeyId, String AWSSecretKey, String dbname) {//throws Exception {

    String endpoint_str = "";
    BasicAWSCredentials basic_credentials = new BasicAWSCredentials(AWSKeyId, AWSSecretKey);

    AmazonRDSClient rDSClient = new AmazonRDSClient(basic_credentials);

    ModifyDBInstanceRequest mod_db = new ModifyDBInstanceRequest(dbname + "cloud4soaid");
    DBInstance dbInstance = new DBInstance();
    ////CHECK THIS. in order to make correct ModifyDBInstanceRequest an attribute must be sent for modification.
    ///5 (GB) is the minimum
    mod_db.setAllocatedStorage(6);/*from   w w w . j a  v  a 2s  .  c  o m*/

    dbInstance = rDSClient.modifyDBInstance(mod_db);
    Endpoint endpoint = new Endpoint();
    endpoint = dbInstance.getEndpoint();
    if (endpoint != null) {
        endpoint_str = endpoint.getAddress();
        System.out.println("endpoint to string:" + endpoint_str);/////{Address: cloud4soadbid.coaodqyxxykq.us-east-1.rds.amazonaws.com, Port: 3306, }
        System.out.println("endpoint get address:" + endpoint.getAddress());/////cloud4soadbid.coaodqyxxykq.us-east-1.rds.amazonaws.com
    }

    return endpoint_str;
}

From source file:beanstalk.BeansDatabase.java

License:Apache License

public boolean deleteDatabase(String AWSKeyId, String AWSSecretKey, String dbIdentifier) {//throws Exception {

    boolean ret = false;
    //  credentials = new PropertiesCredentials(BeanstalkDeployNoGUI.class.getResourceAsStream("AwsCredentials.properties"))
    BasicAWSCredentials basic_credentials = new BasicAWSCredentials(AWSKeyId, AWSSecretKey);

    //  SimpleDBUtils simpledb= new SimpleDBUtils();
    AmazonRDSClient rDSClient = new AmazonRDSClient(basic_credentials);
    // RestoreDBInstanceFromDBSnapshotRequest req= new RestoreDBInstanceFromDBSnapshotRequest();
    //  req.setDBName("dbname");
    //  req.setPort(3306);

    DescribeDBInstancesRequest ddbi = new DescribeDBInstancesRequest();

    DBInstance dbins = new DBInstance();
    dbins.getEndpoint();//  ww  w  .  j a  v  a 2 s  . c  o  m
    DeleteDBInstanceRequest delRequest = new DeleteDBInstanceRequest(dbIdentifier);
    // rDSClient.restoreDBInstanceFromDBSnapshot(req);
    delRequest.setSkipFinalSnapshot(true);
    rDSClient.deleteDBInstance(delRequest);

    return ret;
}

From source file:beanstalk.BeansDatabase.java

License:Apache License

public boolean allowIPConnectionWithDB(String AWSKeyId, String AWSSecretKey, String dbIdentifier) {//throws Exception {

    boolean ret = false;
    String security_group = "Cloud4SoaSecGroup";
    BasicAWSCredentials basic_credentials = new BasicAWSCredentials(AWSKeyId, AWSSecretKey);

    AmazonRDSClient rDSClient = new AmazonRDSClient(basic_credentials);
    //1st step-->add group cloud4soa if not exist

    CreateDBSecurityGroupRequest create_secGroupRequest = new CreateDBSecurityGroupRequest(security_group,
            "GroupGeneratedByCloud4SoaAdapter");
    DBSecurityGroup securityGroup = new DBSecurityGroup();

    try {// www  . j  av a2 s. com
        securityGroup = rDSClient.createDBSecurityGroup(create_secGroupRequest);
    } catch (AmazonClientException amazonClientException) {
        System.out.print("Error when trying to add Security Group.Security Group might exist already!");
    }

    //2nd step--> add IP to list of specific Security Group

    AuthorizeDBSecurityGroupIngressRequest ip2SecGroup = new AuthorizeDBSecurityGroupIngressRequest(
            security_group);
    //allow specific ip
    //ip2SecGroup.setCIDRIP("91.132.244.150/5");
    //allow everyone
    ip2SecGroup.setCIDRIP("0.0.0.0/0");
    try {
        rDSClient.authorizeDBSecurityGroupIngress(ip2SecGroup);
    } catch (AmazonClientException amazonClientException) {
        System.out.print(
                "Error when trying to add the specific IP address to the security group.IP might be already entered!");

    }

    return ret;
}

From source file:beanstalk.BeanstalkDeploy.java

License:Apache License

public boolean deploy(String war, String AWSKeyId, String AWSSecretKey, String applicationname,
        String applicationversion, String environment, String bucket, String host) {//throws Exception {

    boolean ret = false;
    //  credentials = new PropertiesCredentials(BeanstalkDeploy.class.getResourceAsStream("AwsCredentials.properties"))
    BasicAWSCredentials basic_credentials = new BasicAWSCredentials(AWSKeyId, AWSSecretKey);

    // TransferManager manages a pool of threads, so we create a
    // single instance and share it throughout our application.
    tx = new TransferManager(basic_credentials);

    appname = applicationname;//from w w w.j  a  v  a  2 s  .c  om
    appversion = applicationversion;
    accessKeyId = AWSKeyId;
    secretAccessKey = AWSSecretKey;
    bucketName = bucket;
    environment_name = environment;
    host_name = host;

    //STEP 1: UPLOAD

    //STEP 2: CREATE APP VERSION

    //STEP 3: DEPLOY

    //    if(war.equalsIgnoreCase("")){
    //    NoGUI ngui= new NoGUI();

    //    ngui.performDeploydeploy("/home/jled/NetBeansProjects/WebApplication1/dist/WebApplication1.war",accessKeyId , secretAccessKey, appname, appversion,
    // environment_name,bucketName, host_name);

    //   }

    //TODO
    //take war file path from args
    //local_filename_and_path ="../sdsdsds/sts.war";

    /// new BeanstalkDeploy(args);

    return ret;
}