Example usage for com.amazonaws.auth DefaultAWSCredentialsProviderChain DefaultAWSCredentialsProviderChain

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

Introduction

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

Prototype

public DefaultAWSCredentialsProviderChain() 

Source Link

Usage

From source file:biz.neustar.webmetrics.plugins.neustar_s3_maven_plugin.S3UploadMojo.java

License:Apache License

/** */
private static AmazonS3 getS3Client(String accessKey, String secretKey) {

    AWSCredentialsProvider provider = null;

    if (accessKey != null && secretKey != null) {
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
        provider = new StaticCredentialsProvider(credentials);
    } else {// w  w  w  .  j a va2  s.  com
        provider = new DefaultAWSCredentialsProviderChain();
    }
    return new AmazonS3Client(provider);
}

From source file:br.com.ingenieux.jenkins.plugins.codecommit.CodeCommitURLHelper.java

License:Apache License

private Iterable<RepositoryUsernameReference> fetchCodeCommitRepositoryNames(GitSCM scm)
        throws CredentialNotFoundException {
    AWSCredentialsProvider credentials = new DefaultAWSCredentialsProviderChain();

    if (isNotBlank(credentialId)) {
        credentials = CredentialsFactory.getCredentials(credentialId);

        if (null == credentials)
            throw new CredentialNotFoundException(
                    "CredentialId '" + credentialId + "' specified but not found.");
    }/*from  w w  w. jav  a  2s  . c  o m*/

    Set<RepositoryUsernameReference> results = new LinkedHashSet<RepositoryUsernameReference>();

    for (RemoteConfig cfg : scm.getRepositories()) {
        for (URIish u : cfg.getURIs()) {
            final String repositoryUrl = u.toPrivateASCIIString();
            final Matcher m = PATTERN_CODECOMMIT_REPO.matcher(repositoryUrl);

            if (m.matches()) {
                final String awsRegion = m.group(1);
                final String repositoryName = m.group(2);

                String usernamePassword = new CodeCommitRequestSigner(credentials, repositoryName, awsRegion,
                        new Date()).getPushUrl();

                int lastIndex = usernamePassword.lastIndexOf(':');

                String username = usernamePassword.substring(0, lastIndex);

                String password = usernamePassword.substring(1 + lastIndex);

                results.add(new RepositoryUsernameReference(repositoryUrl, repositoryName, username, password));
            }
        }
    }

    return results;
}

From source file:com.alertlogic.aws.analytics.poc.DeleteResources.java

License:Open Source License

public static void main(String[] args) {
    if (args.length != 4) {
        System.err.println("Usage: " + DeleteResources.class.getSimpleName()
                + " <application name> <stream name> <DynamoDB table name> <region>");
        System.exit(1);/*  w ww .  j a va  2  s . c  om*/
    }

    String applicationName = args[0];
    String streamName = args[1];
    String countsTableName = args[2];
    Region region = Utils.parseRegion(args[3]);

    AWSCredentialsProvider credentialsProvider = new DefaultAWSCredentialsProviderChain();
    ClientConfiguration clientConfig = Utils.configureUserAgentForSample(new ClientConfiguration());
    AmazonKinesis kinesis = new AmazonKinesisClient(credentialsProvider, clientConfig);
    kinesis.setRegion(region);
    AmazonDynamoDB dynamoDB = new AmazonDynamoDBClient(credentialsProvider, clientConfig);
    dynamoDB.setRegion(region);

    StreamUtils streamUtils = new StreamUtils(kinesis);
    DynamoDBUtils dynamoDBUtils = new DynamoDBUtils(dynamoDB);

    LOG.info("Removing Amazon Kinesis and DynamoDB resources used by the sample application...");

    streamUtils.deleteStream(streamName);
    // The Kinesis Client Library creates a table to manage shard leases and uses the application name for its name.
    dynamoDBUtils.deleteTable(applicationName);
    dynamoDBUtils.deleteTable(countsTableName);
}

From source file:com.alertlogic.aws.kinesis.test1.StreamProcessor.java

License:Open Source License

/**
 * Start the Kinesis Client application.
 * /*from   ww  w. java  2  s . co  m*/
 * @param args Expecting 4 arguments: Application name to use for the Kinesis Client Application, Stream name to
 *        read from, DynamoDB table name to persist counts into, and the AWS region in which these resources
 *        exist or should be created.
 */
public static void main(String[] args) throws UnknownHostException {
    if (args.length != 4) {
        System.err.println("Usage: " + StreamProcessor.class.getSimpleName()
                + " <application name> <stream name> <DynamoDB table name> <region>");
        System.exit(1);
    }

    String applicationName = args[0];
    String streamName = args[1];
    String countsTableName = args[2];
    Region region = SampleUtils.parseRegion(args[3]);

    AWSCredentialsProvider credentialsProvider = new DefaultAWSCredentialsProviderChain();
    ClientConfiguration clientConfig = SampleUtils.configureUserAgentForSample(new ClientConfiguration());
    AmazonKinesis kinesis = new AmazonKinesisClient(credentialsProvider, clientConfig);
    kinesis.setRegion(region);
    AmazonDynamoDB dynamoDB = new AmazonDynamoDBClient(credentialsProvider, clientConfig);
    dynamoDB.setRegion(region);

    // Creates a stream to write to, if it doesn't exist
    StreamUtils streamUtils = new StreamUtils(kinesis);
    streamUtils.createStreamIfNotExists(streamName, 2);
    LOG.info(String.format("%s stream is ready for use", streamName));

    DynamoDBUtils dynamoDBUtils = new DynamoDBUtils(dynamoDB);
    dynamoDBUtils.createCountTableIfNotExists(countsTableName);
    LOG.info(String.format("%s DynamoDB table is ready for use", countsTableName));

    String workerId = String.valueOf(UUID.randomUUID());
    LOG.info(String.format("Using working id: %s", workerId));
    KinesisClientLibConfiguration kclConfig = new KinesisClientLibConfiguration(applicationName, streamName,
            credentialsProvider, workerId);
    kclConfig.withCommonClientConfig(clientConfig);
    kclConfig.withRegionName(region.getName());
    kclConfig.withInitialPositionInStream(InitialPositionInStream.LATEST);

    // Persist counts to DynamoDB
    DynamoDBCountPersister persister = new DynamoDBCountPersister(
            dynamoDBUtils.createMapperForTable(countsTableName));

    IRecordProcessorFactory recordProcessor = new CountingRecordProcessorFactory<HttpReferrerPair>(
            HttpReferrerPair.class, persister, COMPUTE_RANGE_FOR_COUNTS_IN_MILLIS, COMPUTE_INTERVAL_IN_MILLIS);

    Worker worker = new Worker(recordProcessor, kclConfig);

    int exitCode = 0;
    try {
        worker.run();
    } catch (Throwable t) {
        LOG.error("Caught throwable while processing data.", t);
        exitCode = 1;
    }
    System.exit(exitCode);
}

From source file:com.alertlogic.aws.kinesis.test1.StreamWriter.java

License:Open Source License

/**
 * Start a number of threads and send randomly generated {@link HttpReferrerPair}s to a Kinesis Stream until the
 * program is terminated.// w w w .j ava 2  s  . c  om
 *
 * @param args Expecting 3 arguments: A numeric value indicating the number of threads to use to send
 *        data to Kinesis and the name of the stream to send records to, and the AWS region in which these resources
 *        exist or should be created.
 * @throws InterruptedException If this application is interrupted while sending records to Kinesis.
 */
public static void main(String[] args) throws InterruptedException {
    if (args.length != 3) {
        System.err.println(
                "Usage: " + StreamWriter.class.getSimpleName() + " <number of threads> <stream name> <region>");
        System.exit(1);
    }

    int numberOfThreads = Integer.parseInt(args[0]);
    String streamName = args[1];
    Region region = SampleUtils.parseRegion(args[2]);

    AWSCredentialsProvider credentialsProvider = new DefaultAWSCredentialsProviderChain();
    ClientConfiguration clientConfig = SampleUtils.configureUserAgentForSample(new ClientConfiguration());
    AmazonKinesis kinesis = new AmazonKinesisClient(credentialsProvider, clientConfig);
    kinesis.setRegion(region);

    // The more resources we declare the higher write IOPS we need on our DynamoDB table.
    // We write a record for each resource every interval.
    // If interval = 500ms, resource count = 7 we need: (1000/500 * 7) = 14 write IOPS minimum.
    List<String> resources = new ArrayList<>();
    resources.add("/index.html");

    // These are the possible referrers to use when generating pairs
    List<String> referrers = new ArrayList<>();
    referrers.add("http://www.amazon.com");
    referrers.add("http://www.google.com");
    referrers.add("http://www.yahoo.com");
    referrers.add("http://www.bing.com");
    referrers.add("http://www.stackoverflow.com");
    referrers.add("http://www.reddit.com");

    HttpReferrerPairFactory pairFactory = new HttpReferrerPairFactory(resources, referrers);

    // Creates a stream to write to with 2 shards if it doesn't exist
    StreamUtils streamUtils = new StreamUtils(kinesis);
    streamUtils.createStreamIfNotExists(streamName, 2);
    LOG.info(String.format("%s stream is ready for use", streamName));

    final HttpReferrerKinesisPutter putter = new HttpReferrerKinesisPutter(pairFactory, kinesis, streamName);

    ExecutorService es = Executors.newCachedThreadPool();

    Runnable pairSender = new Runnable() {
        @Override
        public void run() {
            try {
                putter.sendPairsIndefinitely(DELAY_BETWEEN_RECORDS_IN_MILLIS, TimeUnit.MILLISECONDS);
            } catch (Exception ex) {
                LOG.warn(
                        "Thread encountered an error while sending records. Records will no longer be put by this thread.",
                        ex);
            }
        }
    };

    for (int i = 0; i < numberOfThreads; i++) {
        es.submit(pairSender);
    }

    LOG.info(String.format("Sending pairs with a %dms delay between records with %d thread(s).",
            DELAY_BETWEEN_RECORDS_IN_MILLIS, numberOfThreads));

    es.shutdown();
    es.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
}

From source file:com.alertlogic.aws.kinesis.test1.utils.DeleteSampleResources.java

License:Open Source License

public static void main(String[] args) {
    if (args.length != 4) {
        System.err.println("Usage: " + DeleteSampleResources.class.getSimpleName()
                + " <application name> <stream name> <DynamoDB table name> <region>");
        System.exit(1);/*from  w  w w  . java 2s .  c o m*/
    }

    String applicationName = args[0];
    String streamName = args[1];
    String countsTableName = args[2];
    Region region = SampleUtils.parseRegion(args[3]);

    AWSCredentialsProvider credentialsProvider = new DefaultAWSCredentialsProviderChain();
    ClientConfiguration clientConfig = SampleUtils.configureUserAgentForSample(new ClientConfiguration());
    AmazonKinesis kinesis = new AmazonKinesisClient(credentialsProvider, clientConfig);
    kinesis.setRegion(region);
    AmazonDynamoDB dynamoDB = new AmazonDynamoDBClient(credentialsProvider, clientConfig);
    dynamoDB.setRegion(region);

    StreamUtils streamUtils = new StreamUtils(kinesis);
    DynamoDBUtils dynamoDBUtils = new DynamoDBUtils(dynamoDB);

    LOG.info("Removing Amazon Kinesis and DynamoDB resources used by the sample application...");

    streamUtils.deleteStream(streamName);
    // The Kinesis Client Library creates a table to manage shard leases and uses the application name for its name.
    dynamoDBUtils.deleteTable(applicationName);
    dynamoDBUtils.deleteTable(countsTableName);
}

From source file:com.alertlogic.aws.kinesis.test1.WebServer.java

License:Open Source License

/**
 * Start an embedded web server.//from w w  w  .j  a  v  a 2 s  . c  om
 * 
 * @param args Expecting 4 arguments: Port number, File path to static content, the name of the
 *        DynamoDB table where counts are persisted to, and the AWS region in which these resources
 *        exist or should be created.
 * @throws Exception Error starting the web server.
 */
public static void main(String[] args) throws Exception {
    if (args.length != 4) {
        System.err.println("Usage: " + WebServer.class
                + " <port number> <directory for static content> <DynamoDB table name> <region>");
        System.exit(1);
    }
    Server server = new Server(Integer.parseInt(args[0]));
    String wwwroot = args[1];
    String countsTableName = args[2];
    Region region = SampleUtils.parseRegion(args[3]);

    // Servlet context
    ServletContextHandler context = new ServletContextHandler(
            ServletContextHandler.NO_SESSIONS | ServletContextHandler.NO_SECURITY);
    context.setContextPath("/api");

    // Static resource context
    ResourceHandler resources = new ResourceHandler();
    resources.setDirectoriesListed(false);
    resources.setWelcomeFiles(new String[] { "graph.html" });
    resources.setResourceBase(wwwroot);

    // Create the servlet to handle /GetCounts
    AWSCredentialsProvider credentialsProvider = new DefaultAWSCredentialsProviderChain();
    ClientConfiguration clientConfig = SampleUtils.configureUserAgentForSample(new ClientConfiguration());
    AmazonDynamoDB dynamoDB = new AmazonDynamoDBClient(credentialsProvider, clientConfig);
    dynamoDB.setRegion(region);
    DynamoDBUtils dynamoDBUtils = new DynamoDBUtils(dynamoDB);
    context.addServlet(
            new ServletHolder(new GetCountsServlet(dynamoDBUtils.createMapperForTable(countsTableName))),
            "/GetCounts/*");

    HandlerList handlers = new HandlerList();
    handlers.addHandler(context);
    handlers.addHandler(resources);
    handlers.addHandler(new DefaultHandler());

    server.setHandler(handlers);
    server.start();
    server.join();
}

From source file:com.amazon.photosharing.utils.ContentHelper.java

License:Open Source License

private void initS3Client() {
    s3Client = new AmazonS3Client(new DefaultAWSCredentialsProviderChain())
            .withRegion(Regions.fromName(ConfigFacade.get(Configuration.S3_REGION)));
}

From source file:com.amazon.services.awsrum.kinesis.KinesisConnectorExecutor.java

License:Open Source License

/**
 * Returns an {@link AWSCredentialsProvider} with the permissions necessary to accomplish all specified
 * tasks. At the minimum it will require Kinesis read permissions. Additional read permissions
 * and write permissions may be required based on the Pipeline used
 * /*  ww  w.j  ava  2 s . c om*/
 * @return
 */
public AWSCredentialsProvider getAWSCredentialsProvider() {
    return new DefaultAWSCredentialsProviderChain();
}

From source file:com.datatorrent.contrib.kinesis.KinesisOperatorTestBase.java

License:Open Source License

private void createClient() {
    AWSCredentialsProvider credentials = new DefaultAWSCredentialsProviderChain();
    client = new AmazonKinesisClient(credentials);
}