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:imperial.modaclouds.monitoring.datacollectors.monitors.DetailedCostMonitor.java

License:BSD License

@Override
public void run() {

    String accessKeyId = null;/*from   w w w .ja  v  a2  s. c o  m*/

    String secretKey = null;

    ObjectListing objects = null;

    AmazonS3Client s3Client = null;

    String key = null;

    long startTime = 0;

    while (!dcmt.isInterrupted()) {

        if (System.currentTimeMillis() - startTime > 10000) {

            cost_nonspot = new HashMap<String, Double>();

            cost_spot = new HashMap<String, Double>();

            for (String metric : getProvidedMetrics()) {
                try {
                    VM resource = new VM(Config.getInstance().getVmType(), Config.getInstance().getVmId());
                    if (dcAgent.shouldMonitor(resource, metric)) {
                        Map<String, String> parameters = dcAgent.getParameters(resource, metric);

                        accessKeyId = parameters.get("accessKey");
                        secretKey = parameters.get("secretKey");
                        bucketName = parameters.get("bucketName");
                        filePath = parameters.get("filePath");
                        period = Integer.valueOf(parameters.get("samplingTime")) * 1000;
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                } catch (ConfigurationException e) {
                    e.printStackTrace();
                }
            }

            startTime = System.currentTimeMillis();

            AWSCredentials credentials = new BasicAWSCredentials(accessKeyId, secretKey);
            s3Client = new AmazonS3Client(credentials);

            objects = s3Client.listObjects(bucketName);

            key = "aws-billing-detailed-line-items-with-resources-and-tags-";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            String date = sdf.format(new Date());
            key = key + date + ".csv.zip";

        }

        String fileName = null;
        do {
            for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
                System.out.println(objectSummary.getKey() + "\t" + objectSummary.getSize() + "\t"
                        + StringUtils.fromDate(objectSummary.getLastModified()));
                if (objectSummary.getKey().contains(key)) {
                    fileName = objectSummary.getKey();
                    s3Client.getObject(new GetObjectRequest(bucketName, fileName),
                            new File(filePath + fileName));
                    break;
                }
            }
            objects = s3Client.listNextBatchOfObjects(objects);
        } while (objects.isTruncated());

        try {
            ZipFile zipFile = new ZipFile(filePath + fileName);
            zipFile.extractAll(filePath);
        } catch (ZipException e) {
            e.printStackTrace();
        }

        String csvFileName = fileName.replace(".zip", "");

        AnalyseFile(filePath + csvFileName);

        try {
            Thread.sleep(period);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            break;
        }

    }
}

From source file:imperial.modaclouds.monitoring.datacollectors.monitors.EC2SpotPriceMonitor.java

License:BSD License

@Override
public void run() {

    String accessKeyId = null;/*from  www.  j  a  va2s .com*/

    String secretKey = null;

    long startTime = 0;

    while (!spmt.isInterrupted()) {

        if (System.currentTimeMillis() - startTime > 10000) {
            spotInstanceVec = new ArrayList<SpotInstance>();

            for (String metric : getProvidedMetrics()) {
                try {
                    VM resource = new VM(Config.getInstance().getVmType(), Config.getInstance().getVmId());
                    if (dcAgent.shouldMonitor(resource, metric)) {

                        Map<String, String> parameters = dcAgent.getParameters(resource, metric);

                        String endpoint = null;
                        String productDes = null;
                        String instanceType = null;

                        accessKeyId = parameters.get("accessKey");
                        secretKey = parameters.get("secretKey");
                        endpoint = parameters.get("endPoint");
                        productDes = parameters.get("productDescription");
                        instanceType = parameters.get("productDescription");
                        period = Integer.valueOf(parameters.get("samplingTime")) * 1000;
                        samplingProb = Double.valueOf(parameters.get("samplingProbability"));

                        SpotInstance spotInstance = new SpotInstance();
                        spotInstance.productDes = new ArrayList<String>();
                        spotInstance.instanceType = new ArrayList<String>();

                        spotInstance.endpoint = endpoint;
                        spotInstance.productDes.add(productDes);
                        spotInstance.instanceType.add(instanceType);

                        spotInstanceVec.add(spotInstance);
                    }
                } catch (NumberFormatException | ConfigurationException e) {
                    e.printStackTrace();
                }
            }

            startTime = System.currentTimeMillis();
        }

        AWSCredentials credentials = new BasicAWSCredentials(accessKeyId, secretKey);
        AmazonEC2 ec2 = new AmazonEC2Client(credentials);

        //////

        boolean isSent = false;
        if (Math.random() < this.samplingProb) {
            isSent = true;
        }

        for (int i = 0; i < spotInstanceVec.size(); i++) {
            ec2.setEndpoint(spotInstanceVec.get(i).endpoint);

            DescribeSpotPriceHistoryRequest request = new DescribeSpotPriceHistoryRequest();

            request.setProductDescriptions(spotInstanceVec.get(i).productDes);
            request.setInstanceTypes(spotInstanceVec.get(i).instanceType);
            //request.setStartTime(startTime);
            //request.setMaxResults(maxResult);

            String nextToken = "";
            //do {
            request.withNextToken(nextToken);

            DescribeSpotPriceHistoryResult result = ec2.describeSpotPriceHistory(request);

            List<String> splitString = Arrays.asList(result.getSpotPriceHistory().get(0).toString().split(","));

            if (isSent) {
                for (String temp : splitString) {
                    if (temp.contains("SpotPrice")) {
                        temp = temp.replace("SpotPrice: ", "");
                        System.out.println(temp);
                        try {
                            logger.info("Sending datum: {} {} {}", temp, "SpotPrice", monitoredTarget);
                            dcAgent.send(
                                    new VM(Config.getInstance().getVmType(), Config.getInstance().getVmId()),
                                    "SpotPrice", temp);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
            //for (int j = 0; j < result.getSpotPriceHistory().size(); j++) {
            //System.out.println(result.getSpotPriceHistory().get(0));
            //break;
            //}

            //result = result.withNextToken(result.getNextToken());
            //nextToken = result.getNextToken();
            //} while(!nextToken.isEmpty());
        }
        try {
            Thread.sleep(period);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            break;
        }

    }
}

From source file:ingest.utility.IngestUtilities.java

License:Apache License

/**
 * Gets an instance of an S3 client to use.
 * /*  ww  w. j a  v  a  2 s.  c o  m*/
 * @param useEncryption
 *            True if encryption should be used (only for Piazza Bucket). For all external Buckets, encryption is
 *            not used.
 * 
 * @return The S3 client
 */
public AmazonS3 getAwsClient(boolean useEncryption) {
    AmazonS3 s3Client;
    if ((AMAZONS3_ACCESS_KEY.isEmpty()) && (AMAZONS3_PRIVATE_KEY.isEmpty())) {
        s3Client = new AmazonS3Client();
    } else {
        BasicAWSCredentials credentials = new BasicAWSCredentials(AMAZONS3_ACCESS_KEY, AMAZONS3_PRIVATE_KEY);
        // Set up encryption using the KMS CMK Key
        if (useEncryption) {
            KMSEncryptionMaterialsProvider materialProvider = new KMSEncryptionMaterialsProvider(S3_KMS_CMK_ID);
            s3Client = new AmazonS3EncryptionClient(credentials, materialProvider,
                    new CryptoConfiguration().withKmsRegion(Regions.US_EAST_1))
                            .withRegion(Region.getRegion(Regions.US_EAST_1));
        } else {
            s3Client = new AmazonS3Client(credentials);
        }
    }
    return s3Client;
}

From source file:integratedtoolkit.connectors.amazon.EC2.java

License:Apache License

public EC2(String name, HashMap<String, String> h) {
    this.providerName = name;
    // Connector parameters
    accessKeyId = h.get("Access Key Id");
    secretKeyId = h.get("Secret key Id");
    keyPairName = h.get("KeyPair name");
    keyLocation = h.get("Key host location");
    securityGroupName = h.get("SecurityGroup Name");
    placementCode = h.get("Placement");
    placement = getPlacement(placementCode);

    //Connector data init
    activeRequests = 0;/* w ww  .  j  a  v  a2s . c o  m*/
    IPToName = new HashMap<String, String>();
    IPToType = new HashMap<String, String>();
    IPToStart = new HashMap<String, Long>();
    check = false;
    terminate = false;
    smallCount = 0;
    largeCount = 0;
    xlargeCount = 0;
    accumulatedCost = 0.0f;
    known_hosts = new Integer(0);
    stats = new Integer(0);

    //Prepare Amazon for first use
    client = new AmazonEC2Client(new BasicAWSCredentials(accessKeyId, secretKeyId));
    boolean found = false;

    DescribeKeyPairsResult dkpr = client.describeKeyPairs();
    for (KeyPairInfo kp : dkpr.getKeyPairs()) {
        if (kp.getKeyName().compareTo(keyPairName) == 0) {
            found = true;
            break;
        }
    }
    if (!found) {
        try {
            ImportKeyPairRequest ikpReq = new ImportKeyPairRequest(keyPairName, getPublicKey());
            client.importKeyPair(ikpReq);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    found = false;
    DescribeSecurityGroupsResult dsgr = client.describeSecurityGroups();
    for (SecurityGroup sg : dsgr.getSecurityGroups()) {
        if (sg.getGroupName().compareTo(securityGroupName) == 0) {
            found = true;
            break;
        }
    }
    if (!found) {
        try {
            CreateSecurityGroupRequest sg = new CreateSecurityGroupRequest(securityGroupName, "description");
            client.createSecurityGroup(sg);
            IpPermission ipp = new IpPermission();
            ipp.setToPort(22);
            ipp.setFromPort(22);
            ipp.setIpProtocol("tcp");
            ArrayList<String> ipranges = new ArrayList<String>();
            ipranges.add("0.0.0.0/0");
            ipp.setIpRanges(ipranges);
            ArrayList<IpPermission> list_ipp = new ArrayList<IpPermission>();
            list_ipp.add(ipp);
            AuthorizeSecurityGroupIngressRequest asgi = new AuthorizeSecurityGroupIngressRequest(
                    securityGroupName, list_ipp);
            client.authorizeSecurityGroupIngress(asgi);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

From source file:integratedtoolkit.connectors.amazon.EC2_2.java

License:Apache License

public EC2_2(String providerName, HashMap<String, String> props) {
    this.providerName = providerName;

    // Connector parameters
    accessKeyId = props.get("Access Key Id");
    secretKeyId = props.get("Secret key Id");
    keyPairName = props.get("KeyPair name");
    keyLocation = props.get("Key host location");
    securityGroupName = props.get("SecurityGroup Name");
    placementCode = props.get("Placement");
    placement = VM.translatePlacement(placementCode);

    ipToVmId = new HashMap<String, String>();
    //totalCost = 0.0f;
    currentCostPerHour = 0.0f;/*from w w  w  . j  av  a2 s  .c  o m*/
    deletedMachinesCost = 0.0f;
    vmIdToInfo = new HashMap<String, VM>();
    terminate = false;
    check = false;

    vmsToDelete = new TreeSet<VM>();
    vmsAlive = new LinkedList<VM>();

    ipToConnection = Collections.synchronizedMap(new HashMap<String, Connection>());

    client = new AmazonEC2Client(new BasicAWSCredentials(accessKeyId, secretKeyId));
    // TODO: Only for EU (Ireland)
    client.setEndpoint("https://ec2.eu-west-1.amazonaws.com");

    if (props.get("MaxVMCreationTime") != null) {
        MAX_VM_CREATION_TIME = props.get("MaxVMCreationTime");
    }

    dead = new DeadlineThread();
    dead.start();

    boolean found = false;

    DescribeKeyPairsResult dkpr = client.describeKeyPairs();
    for (KeyPairInfo kp : dkpr.getKeyPairs()) {
        if (kp.getKeyName().compareTo(keyPairName) == 0) {
            found = true;
            break;
        }
    }
    if (!found) {
        try {
            ImportKeyPairRequest ikpReq = new ImportKeyPairRequest(keyPairName,
                    KeyManager.getPublicKey(keyLocation));
            client.importKeyPair(ikpReq);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    found = false;
    DescribeSecurityGroupsResult dsgr = client.describeSecurityGroups();
    for (SecurityGroup sg : dsgr.getSecurityGroups()) {
        if (sg.getGroupName().compareTo(securityGroupName) == 0) {
            found = true;
            break;
        }
    }
    if (!found) {
        try {
            CreateSecurityGroupRequest sg = new CreateSecurityGroupRequest(securityGroupName, "description");
            client.createSecurityGroup(sg);
            IpPermission ipp = new IpPermission();
            ipp.setToPort(22);
            ipp.setFromPort(22);
            ipp.setIpProtocol("tcp");
            ArrayList<String> ipranges = new ArrayList<String>();
            ipranges.add("0.0.0.0/0");
            ipp.setIpRanges(ipranges);
            ArrayList<IpPermission> list_ipp = new ArrayList<IpPermission>();
            list_ipp.add(ipp);
            AuthorizeSecurityGroupIngressRequest asgi = new AuthorizeSecurityGroupIngressRequest(
                    securityGroupName, list_ipp);
            client.authorizeSecurityGroupIngress(asgi);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:internal.diff.aws.configuration.AmazonS3ClientConfiguration.java

License:Apache License

@Bean
public AmazonS3Client amazonS3Client() {

    BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);

    return new AmazonS3Client(awsCredentials);
}

From source file:io.confluent.connect.s3.DummyAssertiveCredentialsProvider.java

License:Open Source License

@Override
public void configure(final Map<String, ?> configs) {

    final String accessKeyId = (String) configs.get(ACCESS_KEY_NAME);
    final String secretKey = (String) configs.get(SECRET_KEY_NAME);
    final Integer configsNum = Integer.valueOf((String) configs.get(CONFIGS_NUM_KEY_NAME));

    validateConfigs(configs);//from  w ww  . j a v  a  2  s  .  co  m

    assertEquals(configsNum.intValue(), configs.size());

    credentials = new BasicAWSCredentials(accessKeyId, secretKey);
}

From source file:io.crate.external.S3ClientHelper.java

License:Apache License

protected AmazonS3 initClient(@Nullable String accessKey, @Nullable String secretKey) throws IOException {
    if (accessKey == null || secretKey == null) {
        return new AmazonS3Client(DEFAULT_CREDENTIALS_PROVIDER_CHAIN, CLIENT_CONFIGURATION);
    }/*from  w w w  .  ja  va2  s.  co  m*/
    return new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey), CLIENT_CONFIGURATION);
}

From source file:io.exemplary.aws.DynamoDBServer.java

License:Apache License

private void deleteAllTables() {
    AmazonDynamoDBClient client = new AmazonDynamoDBClient(new BasicAWSCredentials("accessKey", "secretKey"));
    client.setEndpoint(getEndpoint());// ww w .  j av a2s .  c  om
    ListTablesResult result = client.listTables(new ListTablesRequest());
    for (String tableName : result.getTableNames()) {
        client.deleteTable(new DeleteTableRequest(tableName));
    }
    client.shutdown();
}

From source file:io.fathom.cloud.compute.services.Ec2DatacenterManager.java

License:Apache License

public Ec2DatacenterManager(HostGroupData hostGroupData, HostGroupSecretData secretData) {
    this.hostGroupData = hostGroupData;

    if (hostGroupData.getHostGroupType() != HostGroupType.HOST_GROUP_TYPE_AMAZON_EC2) {
        throw new IllegalStateException();
    }/* ww w .ja  v  a  2 s.c  o  m*/

    if (secretData.hasUsername()) {
        String accessKey = secretData.getUsername();
        String secretKey = secretData.getPassword();
        AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
        ec2Client = new AmazonEC2Client(awsCredentials);
    }
}