Search in sources :

Example 51 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project aws-doc-sdk-examples by awsdocs.

the class Main_test method main.

public static void main(String[] args) {
    AWSCredentials credentials_profile = null;
    try {
        credentials_profile = new ProfileCredentialsProvider("default").getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load credentials from .aws/credentials file. " + "Make sure that the credentials file exists and the profile name is specified within it.", e);
    }
    AmazonElasticMapReduce emr = AmazonElasticMapReduceClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials_profile)).withRegion(Regions.US_WEST_1).build();
    List<StepConfig> stepConfigs = new ArrayList<StepConfig>();
    HadoopJarStepConfig flinkWordCountConf = new HadoopJarStepConfig().withJar("command-runner.jar").withArgs("bash", "-c", "flink", "run", "-m", "yarn-cluster", "-yn", "2", "/usr/lib/flink/examples/streaming/WordCount.jar", "--input", "s3://path/to/input-file.txt", "--output", "s3://path/to/output/");
    StepConfig flinkRunWordCountStep = new StepConfig().withName("Flink add a wordcount step and terminate").withActionOnFailure("CONTINUE").withHadoopJarStep(flinkWordCountConf);
    stepConfigs.add(flinkRunWordCountStep);
    Application flink = new Application().withName("Flink");
    RunJobFlowRequest request = new RunJobFlowRequest().withName("flink-transient").withReleaseLabel("emr-5.20.0").withApplications(flink).withServiceRole("EMR_DefaultRole").withJobFlowRole("EMR_EC2_DefaultRole").withLogUri("s3://path/to/my/logfiles").withInstances(new JobFlowInstancesConfig().withEc2KeyName("myEc2Key").withEc2SubnetId("subnet-12ab3c45").withInstanceCount(3).withKeepJobFlowAliveWhenNoSteps(false).withMasterInstanceType("m4.large").withSlaveInstanceType("m4.large")).withSteps(stepConfigs);
    RunJobFlowResult result = emr.runJobFlow(request);
    System.out.println("The cluster ID is " + result.toString());
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) AWSCredentials(com.amazonaws.auth.AWSCredentials) AmazonClientException(com.amazonaws.AmazonClientException) AmazonElasticMapReduce(com.amazonaws.services.elasticmapreduce.AmazonElasticMapReduce) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider)

Example 52 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project aws-doc-sdk-examples by awsdocs.

the class MakingRequestsWithFederatedTempCredentials method main.

public static void main(String[] args) throws IOException {
    Regions clientRegion = Regions.DEFAULT_REGION;
    String bucketName = "*** Specify bucket name ***";
    String federatedUser = "*** Federated user name ***";
    String resourceARN = "arn:aws:s3:::" + bucketName;
    try {
        AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard().withCredentials(new ProfileCredentialsProvider()).withRegion(clientRegion).build();
        GetFederationTokenRequest getFederationTokenRequest = new GetFederationTokenRequest();
        getFederationTokenRequest.setDurationSeconds(7200);
        getFederationTokenRequest.setName(federatedUser);
        // Define the policy and add it to the request.
        Policy policy = new Policy();
        policy.withStatements(new Statement(Effect.Allow).withActions(S3Actions.ListObjects).withResources(new Resource(resourceARN)));
        getFederationTokenRequest.setPolicy(policy.toJson());
        // Get the temporary security credentials.
        GetFederationTokenResult federationTokenResult = stsClient.getFederationToken(getFederationTokenRequest);
        Credentials sessionCredentials = federationTokenResult.getCredentials();
        // Package the session credentials as a BasicSessionCredentials
        // object for an Amazon S3 client object to use.
        BasicSessionCredentials basicSessionCredentials = new BasicSessionCredentials(sessionCredentials.getAccessKeyId(), sessionCredentials.getSecretAccessKey(), sessionCredentials.getSessionToken());
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(basicSessionCredentials)).withRegion(clientRegion).build();
        // To verify that the client works, send a listObjects request using
        // the temporary security credentials.
        ObjectListing objects = s3Client.listObjects(bucketName);
        System.out.println("No. of Objects = " + objects.getObjectSummaries().size());
    } catch (AmazonServiceException e) {
        // The call was transmitted successfully, but Amazon S3 couldn't process
        // it, so it returned an error response.
        e.printStackTrace();
    } catch (SdkClientException e) {
        // Amazon S3 couldn't be contacted for a response, or the client
        // couldn't parse the response from Amazon S3.
        e.printStackTrace();
    }
}
Also used : Policy(com.amazonaws.auth.policy.Policy) AmazonS3(com.amazonaws.services.s3.AmazonS3) BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) Statement(com.amazonaws.auth.policy.Statement) GetFederationTokenRequest(com.amazonaws.services.securitytoken.model.GetFederationTokenRequest) Resource(com.amazonaws.auth.policy.Resource) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) Regions(com.amazonaws.regions.Regions) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) SdkClientException(com.amazonaws.SdkClientException) GetFederationTokenResult(com.amazonaws.services.securitytoken.model.GetFederationTokenResult) AmazonServiceException(com.amazonaws.AmazonServiceException) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) AWSSecurityTokenService(com.amazonaws.services.securitytoken.AWSSecurityTokenService) BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) Credentials(com.amazonaws.services.securitytoken.model.Credentials)

Example 53 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project aws-doc-sdk-examples by awsdocs.

the class MakingRequestsWithIAMTempCredentials method main.

public static void main(String[] args) {
    String clientRegion = "*** Client region ***";
    String roleARN = "*** ARN for role to be assumed ***";
    String roleSessionName = "*** Role session name ***";
    String bucketName = "*** Bucket name ***";
    try {
        // Creating the STS client is part of your trusted code. It has
        // the security credentials you use to obtain temporary security credentials.
        AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard().withCredentials(new ProfileCredentialsProvider()).withRegion(clientRegion).build();
        // Obtain credentials for the IAM role. Note that you cannot assume the role of an AWS root account;
        // Amazon S3 will deny access. You must use credentials for an IAM user or an IAM role.
        AssumeRoleRequest roleRequest = new AssumeRoleRequest().withRoleArn(roleARN).withRoleSessionName(roleSessionName);
        AssumeRoleResult roleResponse = stsClient.assumeRole(roleRequest);
        Credentials sessionCredentials = roleResponse.getCredentials();
        // Create a BasicSessionCredentials object that contains the credentials you just retrieved.
        BasicSessionCredentials awsCredentials = new BasicSessionCredentials(sessionCredentials.getAccessKeyId(), sessionCredentials.getSecretAccessKey(), sessionCredentials.getSessionToken());
        // Provide temporary security credentials so that the Amazon S3 client
        // can send authenticated requests to Amazon S3. You create the client
        // using the sessionCredentials object.
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).withRegion(clientRegion).build();
        // Verify that assuming the role worked and the permissions are set correctly
        // by getting a set of object keys from the bucket.
        ObjectListing objects = s3Client.listObjects(bucketName);
        System.out.println("No. of Objects: " + objects.getObjectSummaries().size());
    } catch (AmazonServiceException e) {
        // The call was transmitted successfully, but Amazon S3 couldn't process
        // it, so it returned an error response.
        e.printStackTrace();
    } catch (SdkClientException e) {
        // Amazon S3 couldn't be contacted for a response, or the client
        // couldn't parse the response from Amazon S3.
        e.printStackTrace();
    }
}
Also used : AssumeRoleRequest(com.amazonaws.services.securitytoken.model.AssumeRoleRequest) AmazonS3(com.amazonaws.services.s3.AmazonS3) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) SdkClientException(com.amazonaws.SdkClientException) AmazonServiceException(com.amazonaws.AmazonServiceException) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) AssumeRoleResult(com.amazonaws.services.securitytoken.model.AssumeRoleResult) AWSSecurityTokenService(com.amazonaws.services.securitytoken.AWSSecurityTokenService) BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) Credentials(com.amazonaws.services.securitytoken.model.Credentials)

Example 54 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project aws-doc-sdk-examples by awsdocs.

the class App method main.

public static void main(String[] args) {
    // Create credentials using a provider chain. For more information, see
    // https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html
    AWSCredentialsProvider awsCreds = DefaultAWSCredentialsProviderChain.getInstance();
    AWSTranslate translate = AmazonTranslateClient.standard().withCredentials(new AWSStaticCredentialsProvider(awsCreds)).withRegion(REGION).build();
    TranslateTextRequest request = new TranslateTextRequest().withText("Hello, world").withSourceLanguageCode("en").withTargetLanguageCode("es");
    TranslateTextResult result = translate.translateText(request);
    System.out.println(result.getTranslatedText());
}
Also used : AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) AWSTranslate(com.amazonaws.services.translate.AWSTranslate) TranslateTextRequest(com.amazonaws.services.translate.model.TranslateTextRequest) TranslateTextResult(com.amazonaws.services.translate.model.TranslateTextResult)

Example 55 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project beam by apache.

the class KinesisIOWriteTest method testReadWithBasicCredentials.

@Test
public void testReadWithBasicCredentials() {
    Regions region = Regions.US_EAST_1;
    Write write = KinesisIO.write().withAWSClientsProvider(ACCESS_KEY_ID, SECRET_KEY, region);
    assertEquals(write.getAWSClientsProvider(), new BasicKinesisProvider(new AWSStaticCredentialsProvider(new BasicAWSCredentials(ACCESS_KEY_ID, SECRET_KEY)), region, null, true));
}
Also used : Write(org.apache.beam.sdk.io.kinesis.KinesisIO.Write) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) Regions(com.amazonaws.regions.Regions) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) Test(org.junit.Test)

Aggregations

AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)79 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)57 AWSCredentialsProvider (com.amazonaws.auth.AWSCredentialsProvider)18 AWSCredentials (com.amazonaws.auth.AWSCredentials)17 Test (org.junit.Test)14 ClientConfiguration (com.amazonaws.ClientConfiguration)13 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)11 AmazonS3 (com.amazonaws.services.s3.AmazonS3)11 AmazonS3ClientBuilder (com.amazonaws.services.s3.AmazonS3ClientBuilder)11 SdkClientException (com.amazonaws.SdkClientException)10 BasicSessionCredentials (com.amazonaws.auth.BasicSessionCredentials)9 AwsClientBuilder (com.amazonaws.client.builder.AwsClientBuilder)9 AmazonClientException (com.amazonaws.AmazonClientException)8 Regions (com.amazonaws.regions.Regions)8 Test (org.junit.jupiter.api.Test)7 EndpointConfiguration (com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration)6 File (java.io.File)6 AmazonServiceException (com.amazonaws.AmazonServiceException)5 AWSLambda (com.amazonaws.services.lambda.AWSLambda)4 Segment (com.amazonaws.xray.entities.Segment)4