Example usage for com.amazonaws.auth.profile ProfileCredentialsProvider ProfileCredentialsProvider

List of usage examples for com.amazonaws.auth.profile ProfileCredentialsProvider ProfileCredentialsProvider

Introduction

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

Prototype

public ProfileCredentialsProvider() 

Source Link

Document

Creates a new profile credentials provider that returns the AWS security credentials configured for the default profile.

Usage

From source file:AmazonKinesisSample.java

License:Open Source License

private static void init() throws Exception {

    /*/*ww  w  .  j a  va  2s.c o m*/
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * (~/.aws/credentials).
     */
    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider().getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (~/.aws/credentials), and is in valid format.", e);
    }

    kinesisClient = new AmazonKinesisClient(credentials);
}

From source file:AddUserPassword.java

License:Open Source License

public static void main(String[] args) throws Exception {

    AmazonDynamoDBClient client = new AmazonDynamoDBClient(new ProfileCredentialsProvider());

    DynamoDB dynamoDB = new DynamoDB(client);

    Table table = dynamoDB.getTable("UserLogin");

    int UserID = 0;
    String UserName = "Patrick Laflin";
    int UserPassword = 18924;

    try {/*w w  w .jav a 2s . co  m*/
        System.out.println("Adding a new item...");
        PutItemOutcome outcome = table.putItem(new Item().withPrimaryKey("UserID", UserID)
                .withString("User Name", UserName).withNumber("User Password", UserPassword));

        System.out.println("PutItem succeeded:\n" + outcome.getPutItemResult());

    } catch (Exception e) {
        System.err.println("Unable to add item: " + UserID + " " + UserName);
        System.err.println(e.getMessage());
    }

}

From source file:AwsSQSDemo.java

License:Open Source License

public static void main(String[] args) throws Exception {

    /*/*w ww.  j  av a2s  .c  o  m*/
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * (~/.aws/credentials).
     */
    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider().getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (~/.aws/credentials), and is in valid format.", e);
    }

    AmazonSQS sqs = new AmazonSQSClient(credentials);
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    sqs.setRegion(usWest2);

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon SQS");
    System.out.println("===========================================\n");

    try {
        // Create a queue
        System.out.println("Creating a new SQS queue called MyQueue.\n");
        CreateQueueRequest createQueueRequest = new CreateQueueRequest("MyQueue");
        String myQueueUrl = sqs.createQueue(createQueueRequest).getQueueUrl();

        // List queues
        System.out.println("Listing all queues in your account.\n");
        for (String queueUrl : sqs.listQueues().getQueueUrls()) {
            System.out.println("  QueueUrl: " + queueUrl);
        }
        System.out.println();

        // Send a message
        System.out.println("Sending a message to MyQueue.\n");
        sqs.sendMessage(new SendMessageRequest(myQueueUrl, "This is my message text."));

        // Receive messages
        System.out.println("Receiving messages from MyQueue.\n");
        ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl);
        List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
        for (Message message : messages) {
            System.out.println("  Message");
            System.out.println("    MessageId:     " + message.getMessageId());
            System.out.println("    ReceiptHandle: " + message.getReceiptHandle());
            System.out.println("    MD5OfBody:     " + message.getMD5OfBody());
            System.out.println("    Body:          " + message.getBody());
            for (Entry<String, String> entry : message.getAttributes().entrySet()) {
                System.out.println("  Attribute");
                System.out.println("    Name:  " + entry.getKey());
                System.out.println("    Value: " + entry.getValue());
            }
        }
        System.out.println();

        // Delete a message
        System.out.println("Deleting a message.\n");
        String messageRecieptHandle = messages.get(0).getReceiptHandle();
        sqs.deleteMessage(new DeleteMessageRequest(myQueueUrl, messageRecieptHandle));

        // Delete a queue
        System.out.println("Deleting the test queue.\n");
        sqs.deleteQueue(new DeleteQueueRequest(myQueueUrl));
    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which means your request made it "
                + "to Amazon SQS, 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 "
                + "a serious internal problem while trying to communicate with SQS, such as not "
                + "being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:EC2LaunchWaitTerminate.java

License:Open Source License

/**
 * The only information needed to create a client are security credentials
 * consisting of the AWS Access Key ID and Secret Access Key. All other
 * configuration, such as the service endpoints, are performed
 * automatically. Client parameters, such as proxies, can be specified in an
 * optional ClientConfiguration object when constructing a client.
 *
 * @see com.amazonaws.auth.BasicAWSCredentials
 * @see com.amazonaws.auth.PropertiesCredentials
 * @see com.amazonaws.ClientConfiguration
 *///from   w ww  .j  av a2 s .c o m
private static void init() throws Exception {

    /*
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * (~/.aws/credentials).
     */
    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider().getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (~/.aws/credentials), and is in valid format.", e);
    }
    ec2 = new AmazonEC2Client(credentials);
}

From source file:AmazonKinesisAuditVerify.java

License:Open Source License

private static void init() {
    // Ensure the JVM will refresh the cached IP values of AWS resources (e.g. service endpoints).
    java.security.Security.setProperty("networkaddress.cache.ttl", "60");

    /*//from   ww  w  . ja  va 2  s.c o m
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * (/Users/vpadyachi/.aws/credentials).
     */
    credentialsProvider = new ProfileCredentialsProvider();
    try {
        credentialsProvider.getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (/Users/vpadyachi/.aws/credentials), and is in valid format.", e);
    }
}

From source file:AmazonKinesisAuditVerify.java

License:Open Source License

private static void configure(String propertiesFile) throws IOException {

    if (propertiesFile != null) {
        loadProperties(propertiesFile);//from w  w w.  j a va 2s .co m
    }

    // ensure the JVM will refresh the cached IP values of AWS resources (e.g. service endpoints). 
    java.security.Security.setProperty("networkaddress.cache.ttl", "60");

    String workerId = InetAddress.getLocalHost().getCanonicalHostName() + ":" + UUID.randomUUID();
    LOG.info("Using workerId: " + workerId);

    // Get credentials from IMDS. If unsuccessful, get them from the classpath.  
    //AWSCredentialsProvider credentialsProvider = null; 
    try {
        credentialsProvider = new ProfileCredentialsProvider();
        // Verify we can fetch credentials from the provider 
        credentialsProvider.getCredentials();
        LOG.info("Obtained credentials from the IMDS.");
    } catch (AmazonClientException e) {
        LOG.info("Unable to obtain credentials from the IMDS, trying classpath properties", e);
        credentialsProvider = new ClasspathPropertiesFileCredentialsProvider();
        // Verify we can fetch credentials from the provider 
        credentialsProvider.getCredentials();
        LOG.info("Obtained credentials from the properties file.");
    }

    LOG.info("Using credentials with access key id: "
            + credentialsProvider.getCredentials().getAWSAccessKeyId());

    kinesisClientLibConfiguration = new KinesisClientLibConfiguration(applicationName, streamName,
            credentialsProvider, workerId).withInitialPositionInStream(initialPositionInStream);
}

From source file:SimpleQueueServiceMultiThread.java

License:Open Source License

public static void main(String[] args) throws Exception {

    /*/*from w ww  .j a va 2s.co m*/
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * (~/.aws/credentials).
     */
    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider().getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (~/.aws/credentials), and is in valid format.", e);
    }
    //AmazonSQS sqs = new AmazonSQSClient(credentials,clientConfig);
    sqs = new AmazonSQSClient(credentials);

    Region apSouthEast1 = Region.getRegion(Regions.AP_SOUTHEAST_1);
    sqs.setRegion(apSouthEast1);

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon SQS");
    System.out.println("===========================================\n");

    //Define thread pool size
    ExecutorService executor = Executors.newFixedThreadPool(poolSize);

    //create a list to hold the Future object associated with Callable
    List<Future<Long>> list = new ArrayList<Future<Long>>();

    //Create Callable instance
    Callable<Long> callable = new SimpleQueueServiceMultiThread();

    for (int i = 0; i < threadSize; i++) {

        //submit Callable tasks to be executed by thread pool
        Future<Long> future = executor.submit(callable);

        //add Future to the list, we can get return value using Future
        list.add(future);

    }

    for (Future<Long> fut : list) {

        try {
            // Future.get() waits for task to get completed
            System.out.println("Time difference : " + fut.get());

            long timeDiff = fut.get().longValue();

            totalTime = Long.valueOf(totalTime + timeDiff);

            if (timeDiff >= maxTime) {
                maxTime = timeDiff;
            }

            if (minTime == 0L && timeDiff >= minTime) {
                minTime = timeDiff;
                //}else if (timeDiff <= minTime && timeDiff != 0L) {
            } else if (timeDiff <= minTime) {
                minTime = timeDiff;
            }

        } catch (InterruptedException | ExecutionException e) {

            e.printStackTrace();

        }
    }

    if (totalTime >= 0L) {

        averageTime = totalTime / threadSize;

    }

    System.out.println("Total Time : " + Long.valueOf(totalTime).toString());
    System.out.println("Max Time : " + Long.valueOf(maxTime).toString());
    System.out.println("Min Time : " + Long.valueOf(minTime).toString());
    System.out.println("Average Time : " + Long.valueOf(averageTime).toString());

    //shut down the executor service now
    executor.shutdown();

}

From source file:CreateUserLoginTable.java

License:Open Source License

public static void main(String[] args) throws Exception {

    AmazonDynamoDBClient client = new AmazonDynamoDBClient(new ProfileCredentialsProvider());

    DynamoDB dynamoDB = new DynamoDB(client);

    String tableName = "UserLogin";

    try {//  w w w  .ja va2 s.c o m
        System.out.println("Attempting to create table; please wait...");
        Table table = dynamoDB.createTable(tableName,
                Arrays.asList(new KeySchemaElement("UserID", KeyType.HASH)), //Partition key
                Arrays.asList(new AttributeDefinition("UserID", ScalarAttributeType.N)),
                new ProvisionedThroughput(10L, 10L));
        table.waitForActive();
        System.out.println("Success.  Table status: " + table.getDescription().getTableStatus());

    } catch (Exception e) {
        System.err.println("Unable to create table: ");
        System.err.println(e.getMessage());
    }
}

From source file:SimpleQueueServiceMultiThread1.java

License:Open Source License

public static void main(String[] args) throws Exception {

    /*//from   ww  w . ja va 2  s .co m
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * (~/.aws/credentials).
     */
    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider().getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (~/.aws/credentials), and is in valid format.", e);
    }
    //AmazonSQS sqs = new AmazonSQSClient(credentials,clientConfig);
    sqs = new AmazonSQSClient(credentials);

    Region apSouthEast1 = Region.getRegion(Regions.AP_SOUTHEAST_1);
    sqs.setRegion(apSouthEast1);

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon SQS");
    System.out.println("===========================================\n");

    //Define thread pool size
    ExecutorService executor = Executors.newFixedThreadPool(poolSize);

    //create a list to hold the Future object associated with Callable
    List<Future<Long>> list = new ArrayList<Future<Long>>();

    //Create Callable instance
    Callable<Long> callable = new SimpleQueueServiceMultiThread1();

    for (int i = 0; i < threadSize; i++) {

        //submit Callable tasks to be executed by thread pool
        Future<Long> future = executor.submit(callable);

        //add Future to the list, we can get return value using Future
        list.add(future);

    }

    for (Future<Long> fut : list) {

        try {
            // Future.get() waits for task to get completed
            System.out.println("Time difference : " + fut.get());

            long timeDiff = fut.get().longValue();

            totalTime = Long.valueOf(totalTime + timeDiff);

            if (timeDiff >= maxTime) {
                maxTime = timeDiff;
            }

            if (minTime == 0L && timeDiff >= minTime) {
                minTime = timeDiff;
                //}else if (timeDiff <= minTime && timeDiff != 0L) {
            } else if (timeDiff <= minTime) {
                minTime = timeDiff;
            }

        } catch (InterruptedException | ExecutionException e) {

            e.printStackTrace();

        }
    }

    if (totalTime >= 0L) {

        averageTime = totalTime / threadSize;

    }

    System.out.println("Total Time : " + Long.valueOf(totalTime).toString());
    System.out.println("Max Time : " + Long.valueOf(maxTime).toString());
    System.out.println("Min Time : " + Long.valueOf(minTime).toString());
    System.out.println("Average Time : " + Long.valueOf(averageTime).toString());

    //shut down the executor service now
    executor.shutdown();

}

From source file:SimpleQueueServiceMultiThread4.java

License:Open Source License

public static void main(String[] args) throws Exception {

    /*//from   w ww.  java 2 s  .  c  om
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * (~/.aws/credentials).
     */
    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider().getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (~/.aws/credentials), and is in valid format.", e);
    }
    //AmazonSQS sqs = new AmazonSQSClient(credentials,clientConfig);
    sqs = new AmazonSQSClient(credentials);

    Region apSouthEast1 = Region.getRegion(Regions.AP_SOUTHEAST_1);
    sqs.setRegion(apSouthEast1);

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon SQS");
    System.out.println("===========================================\n");

    //Define thread pool size
    ExecutorService executor = Executors.newFixedThreadPool(poolSize);

    //create a list to hold the Future object associated with Callable
    List<Future<Long>> list = new ArrayList<Future<Long>>();

    //Create Callable instance
    Callable<Long> callable = new SimpleQueueServiceMultiThread4();

    for (int i = 0; i < threadSize; i++) {

        //submit Callable tasks to be executed by thread pool
        Future<Long> future = executor.submit(callable);

        //add Future to the list, we can get return value using Future
        list.add(future);

    }

    for (Future<Long> fut : list) {

        try {
            // Future.get() waits for task to get completed
            System.out.println("Time difference : " + fut.get());

            long timeDiff = fut.get().longValue();

            totalTime = Long.valueOf(totalTime + timeDiff);

            if (timeDiff >= maxTime) {
                maxTime = timeDiff;
            }

            if (minTime == 0L && timeDiff >= minTime) {
                minTime = timeDiff;
                //}else if (timeDiff <= minTime && timeDiff != 0L) {
            } else if (timeDiff <= minTime) {
                minTime = timeDiff;
            }

        } catch (InterruptedException | ExecutionException e) {

            e.printStackTrace();

        }
    }

    if (totalTime >= 0L) {

        averageTime = totalTime / threadSize;

    }

    System.out.println("Total Time : " + Long.valueOf(totalTime).toString());
    System.out.println("Max Time : " + Long.valueOf(maxTime).toString());
    System.out.println("Min Time : " + Long.valueOf(minTime).toString());
    System.out.println("Average Time : " + Long.valueOf(averageTime).toString());

    //shut down the executor service now
    executor.shutdown();

}