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:com.zanox.vertx.mods.KinesisMessageProcessor.java

License:Apache License

private AmazonKinesisAsyncClient createClient() {

    // Building Kinesis configuration
    int connectionTimeout = getOptionalIntConfig(CONNECTION_TIMEOUT,
            ClientConfiguration.DEFAULT_CONNECTION_TIMEOUT);
    int maxConnection = getOptionalIntConfig(MAX_CONNECTION, ClientConfiguration.DEFAULT_MAX_CONNECTIONS);

    // TODO: replace default retry policy
    RetryPolicy retryPolicy = ClientConfiguration.DEFAULT_RETRY_POLICY;
    int socketTimeout = getOptionalIntConfig(SOCKET_TIMEOUT, ClientConfiguration.DEFAULT_SOCKET_TIMEOUT);
    boolean useReaper = getOptionalBooleanConfig(USE_REAPER, ClientConfiguration.DEFAULT_USE_REAPER);
    String userAgent = getOptionalStringConfig(USER_AGENT, ClientConfiguration.DEFAULT_USER_AGENT);

    streamName = getMandatoryStringConfig(STREAM_NAME);
    partitionKey = getMandatoryStringConfig(PARTITION_KEY);
    region = getMandatoryStringConfig(REGION);

    logger.info(" --- Stream name: " + streamName);
    logger.info(" --- Partition key: " + partitionKey);
    logger.info(" --- Region: " + region);

    ClientConfiguration clientConfiguration = new ClientConfiguration();
    clientConfiguration.setConnectionTimeout(connectionTimeout);
    clientConfiguration.setMaxConnections(maxConnection);
    clientConfiguration.setRetryPolicy(retryPolicy);
    clientConfiguration.setSocketTimeout(socketTimeout);
    clientConfiguration.setUseReaper(useReaper);
    clientConfiguration.setUserAgent(userAgent);

    /*/* ww  w.j a  v  a2s.co  m*/
    AWS credentials provider chain that looks for credentials in this order:
       Environment Variables - AWS_ACCESS_KEY_ID and AWS_SECRET_KEY
       Java System Properties - aws.accessKeyId and aws.secretKey
       Credential profiles file at the default location (~/.aws/credentials) shared by all AWS SDKs and the AWS CLI
       Instance profile credentials delivered through the Amazon EC2 metadata service
    */

    AWSCredentialsProvider awsCredentialsProvider = new DefaultAWSCredentialsProviderChain();

    // Configuring Kinesis-client with configuration
    AmazonKinesisAsyncClient kinesisAsyncClient = new AmazonKinesisAsyncClient(awsCredentialsProvider,
            clientConfiguration);
    Region awsRegion = RegionUtils.getRegion(region);
    kinesisAsyncClient.setRegion(awsRegion);

    return kinesisAsyncClient;
}

From source file:de.taimos.pipeline.aws.AWSClientFactory.java

License:Apache License

private static AWSCredentialsProvider getCredentials(EnvVars vars) {
    AWSCredentialsProvider provider = handleStaticCredentials(vars);
    if (provider != null) {
        return provider;
    }/*www .  j  ava 2 s  .  co  m*/

    provider = handleProfile(vars);
    if (provider != null) {
        return provider;
    }

    return new DefaultAWSCredentialsProviderChain();
}

From source file:dk.dma.nogoservice.service.S3DataLoader.java

License:Apache License

@Autowired
public S3DataLoader(@Value("${s3dataloader.tempdir:#{null}}") String tempDirLocation) {
    tempDirLocation = tempDirLocation != null ? tempDirLocation : System.getProperty("java.io.tmpdir");
    tempDir = new File(tempDirLocation);
    if (!tempDir.exists()) {
        cacheLocally = tempDir.mkdirs();
    } else {//from  w w  w.  jav a 2 s  .c om
        cacheLocally = true;
    }

    AWSCredentialsProvider credentialsProvider = new DefaultAWSCredentialsProviderChain();
    try {
        credentialsProvider.getCredentials();
    } catch (SdkClientException e) {
        throw new IllegalStateException(
                "You must define AWS credentials in USER_HOME/.aws/credentials file on the machine. When running on AWS container service, "
                        + "the TaskRole should have permissions to read from S3");
    }

    AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard();
    builder.setCredentials(credentialsProvider);
    builder.setRegion("eu-west-1");
    amazonS3 = builder.build();
    Preconditions.checkArgument(amazonS3.doesBucketExist(DATA_BUCKET),
            "No AWS S3 bucket named " + DATA_BUCKET + " this bucket must exist");
}

From source file:eu.roschi.obdkinesis.HttpReferrerCounterApplication.java

License:Open Source License

/**
 * Start the Kinesis Client application.
 * /*from ww w  .j  a va 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 != 2) {
        System.err.println("Using default values");
        COMPUTE_RANGE_FOR_COUNTS_IN_MILLIS = 30000;
        COMPUTE_INTERVAL_IN_MILLIS = 2000;
    } else {
        COMPUTE_RANGE_FOR_COUNTS_IN_MILLIS = Integer.parseInt(args[0]);
        COMPUTE_INTERVAL_IN_MILLIS = Integer.parseInt(args[1]);
        System.err.println(
                "Using values " + Integer.parseInt(args[0]) + " width " + Integer.parseInt(args[1]) + " rate");
    }
    String applicationName = "obd_kinesis";
    String streamName = "obd_input_stream";
    String countsTableName = "obd_kinesis_count";
    Region region = SampleUtils.parseRegion("us-west-2");

    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:eu.roschi.obdkinesis.WebServer.java

License:Open Source License

/**
 * Start an embedded web server.//from  w  w  w . jav  a 2s . co m
 * 
 * @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 != 1) {
        System.err.println("Usage: " + WebServer.class + " <directory for static content>");
        System.exit(1);
    }
    Server server = new Server(8080);
    String wwwroot = args[0];
    String countsTableName = "obd_kinesis_count";
    Region region = SampleUtils.parseRegion("us-west-2");

    // 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:io.confluent.connect.s3.S3SinkConnectorTestBase.java

License:Open Source License

public AmazonS3 newS3Client(S3SinkConnectorConfig config) {
    AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard()
            .withAccelerateModeEnabled(config.getBoolean(S3SinkConnectorConfig.WAN_MODE_CONFIG))
            .withPathStyleAccessEnabled(true).withCredentials(new DefaultAWSCredentialsProviderChain());

    builder = url == null ? builder.withRegion(config.getString(S3SinkConnectorConfig.REGION_CONFIG))
            : builder.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(url, ""));

    return builder.build();
}

From source file:io.hekate.cluster.seed.jclouds.aws.AwsCredentialsSupplier.java

License:Apache License

@Override
public Credentials get() {
    String identity = getIdentity() != null ? getIdentity().trim() : null;
    String credential = getCredential() != null ? getCredential().trim() : null;

    if (identity == null || identity.isEmpty() || credential == null || credential.isEmpty()) {
        DefaultAWSCredentialsProviderChain chain = new DefaultAWSCredentialsProviderChain();

        AWSCredentials cred = chain.getCredentials();

        if (cred instanceof BasicSessionCredentials) {
            BasicSessionCredentials sesCred = (BasicSessionCredentials) cred;

            return new SessionCredentials.Builder().identity(sesCred.getAWSAccessKeyId())
                    .credential(sesCred.getAWSSecretKey()).sessionToken(sesCred.getSessionToken()).build();
        } else {/*  w w w.  j  av a2  s .  c  o  m*/
            return new Credentials.Builder<>().identity(cred.getAWSAccessKeyId())
                    .credential(cred.getAWSSecretKey()).build();
        }
    }

    return super.get();
}

From source file:io.ignitr.dispatchr.manager.Application.java

License:Apache License

@Autowired
@Bean//from  w ww .jav  a 2  s  . c  om
public AmazonSNSClient amazonSNSClient(DeploymentContext deploymentContext) {
    AmazonSNSClient client = new AmazonSNSClient(new DefaultAWSCredentialsProviderChain());
    client.setRegion(Region.getRegion(Regions.fromName(deploymentContext.getRegion())));

    return client;
}

From source file:io.ignitr.dispatchr.manager.Application.java

License:Apache License

@Autowired
@Bean//from  w ww .  ja v a2  s .c  om
public AmazonDynamoDBClient amazonDynamoDBClient(DeploymentContext deploymentContext) {
    AmazonDynamoDBClient client = new AmazonDynamoDBClient(new DefaultAWSCredentialsProviderChain());
    client.setRegion(Region.getRegion(Regions.fromName(deploymentContext.getRegion())));

    return client;
}

From source file:io.kodokojo.config.module.AwsModule.java

License:Open Source License

@Provides
@Singleton/*from  w  ww.jav a2  s.c  o m*/
DnsManager provideDnsManager(ApplicationConfig applicationConfig, AwsConfig awsConfig) {
    AWSCredentials credentials = null;
    try {
        DefaultAWSCredentialsProviderChain defaultAWSCredentialsProviderChain = new DefaultAWSCredentialsProviderChain();
        credentials = defaultAWSCredentialsProviderChain.getCredentials();
    } catch (RuntimeException e) {
        LOGGER.warn("Unable to retrieve AWS credentials.");
    }
    if (StringUtils.isNotBlank(System.getenv("NO_DNS")) || credentials == null) {
        LOGGER.info("Using NoOpDnsManager as DnsManger implementation");
        return new NoOpDnsManager();
    } else {
        LOGGER.info("Using Route53DnsManager as DnsManger implementation");
        return new Route53DnsManager(applicationConfig.domain(),
                Region.getRegion(Regions.fromName(awsConfig.region())));
    }
}