Search in sources :

Example 66 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project aws-xray-sdk-java by aws.

the class TracingHandlerTest method testS3PutObjectSubsegmentContainsBucketName.

@Test
void testS3PutObjectSubsegmentContainsBucketName() {
    // Setup test
    AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRequestHandlers(new TracingHandler()).withRegion(Regions.US_EAST_1).withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("fake", "fake"))).build();
    mockHttpClient(s3, null);
    String bucket = "test-bucket";
    String key = "test-key";
    // Test logic
    Segment segment = AWSXRay.beginSegment("test");
    s3.putObject(bucket, key, "This is a test from java");
    Assertions.assertEquals(1, segment.getSubsegments().size());
    Assertions.assertEquals("PutObject", segment.getSubsegments().get(0).getAws().get("operation"));
    Assertions.assertEquals(bucket, segment.getSubsegments().get(0).getAws().get("bucket_name"));
    Assertions.assertEquals(key, segment.getSubsegments().get(0).getAws().get("key"));
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) Segment(com.amazonaws.xray.entities.Segment) Test(org.junit.jupiter.api.Test)

Example 67 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project ats-framework by Axway.

the class S3Operations method getClient.

/**
 * Gets configured AmazonS3 client instance. Does not perform actual request until first remote data is needed
 */
private AmazonS3 getClient() {
    if (s3Client != null) {
        // already cached
        return s3Client;
    }
    ClientConfiguration config = new ClientConfiguration();
    if (endpoint != null && endpoint.startsWith("https://")) {
        config.setProtocol(Protocol.HTTPS);
    } else {
        config.setProtocol(Protocol.HTTP);
    }
    BasicAWSCredentials creds = new BasicAWSCredentials(accessKey, secretKey);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Creating S3 client to " + ((endpoint == null) ? "default Amazon" : endpoint) + " endpoint with access key " + accessKey);
    }
    if (this.endpoint != null) {
        if (region == null || region.trim().length() == 0) {
            region = Regions.DEFAULT_REGION.name();
        }
        s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(creds)).withEndpointConfiguration(new EndpointConfiguration(endpoint, region)).withClientConfiguration(config).withPathStyleAccessEnabled(true).build();
    } else {
        s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(creds)).withClientConfiguration(config).withPathStyleAccessEnabled(true).build();
    }
    return s3Client;
}
Also used : AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) EndpointConfiguration(com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration) ClientConfiguration(com.amazonaws.ClientConfiguration) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials)

Example 68 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project tutorials by eugenp.

the class EC2Application method main.

public static void main(String[] args) {
    // Set up the client
    AmazonEC2 ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials)).withRegion(Regions.US_EAST_1).build();
    // Create a security group
    CreateSecurityGroupRequest createSecurityGroupRequest = new CreateSecurityGroupRequest().withGroupName("BaeldungSecurityGroup").withDescription("Baeldung Security Group");
    ec2Client.createSecurityGroup(createSecurityGroupRequest);
    // Allow HTTP and SSH traffic
    IpRange ipRange1 = new IpRange().withCidrIp("0.0.0.0/0");
    IpPermission ipPermission1 = new IpPermission().withIpv4Ranges(Arrays.asList(new IpRange[] { ipRange1 })).withIpProtocol("tcp").withFromPort(80).withToPort(80);
    IpPermission ipPermission2 = new IpPermission().withIpv4Ranges(Arrays.asList(new IpRange[] { ipRange1 })).withIpProtocol("tcp").withFromPort(22).withToPort(22);
    AuthorizeSecurityGroupIngressRequest authorizeSecurityGroupIngressRequest = new AuthorizeSecurityGroupIngressRequest().withGroupName("BaeldungSecurityGroup").withIpPermissions(ipPermission1, ipPermission2);
    ec2Client.authorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest);
    // Create KeyPair
    CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest().withKeyName("baeldung-key-pair");
    CreateKeyPairResult createKeyPairResult = ec2Client.createKeyPair(createKeyPairRequest);
    String privateKey = createKeyPairResult.getKeyPair().getKeyMaterial();
    // See what key-pairs you've got
    DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest();
    DescribeKeyPairsResult describeKeyPairsResult = ec2Client.describeKeyPairs(describeKeyPairsRequest);
    // Launch an Amazon Instance
    RunInstancesRequest runInstancesRequest = // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html | https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/usingsharedamis-finding.html
    new RunInstancesRequest().withImageId("ami-97785bed").withInstanceType(// https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html
    "t2.micro").withMinCount(1).withMaxCount(1).withKeyName(// optional - if not present, can't connect to instance
    "baeldung-key-pair").withSecurityGroups("BaeldungSecurityGroup");
    String yourInstanceId = ec2Client.runInstances(runInstancesRequest).getReservation().getInstances().get(0).getInstanceId();
    // Start an Instance
    StartInstancesRequest startInstancesRequest = new StartInstancesRequest().withInstanceIds(yourInstanceId);
    ec2Client.startInstances(startInstancesRequest);
    // Monitor Instances
    MonitorInstancesRequest monitorInstancesRequest = new MonitorInstancesRequest().withInstanceIds(yourInstanceId);
    ec2Client.monitorInstances(monitorInstancesRequest);
    UnmonitorInstancesRequest unmonitorInstancesRequest = new UnmonitorInstancesRequest().withInstanceIds(yourInstanceId);
    ec2Client.unmonitorInstances(unmonitorInstancesRequest);
    // Reboot an Instance
    RebootInstancesRequest rebootInstancesRequest = new RebootInstancesRequest().withInstanceIds(yourInstanceId);
    ec2Client.rebootInstances(rebootInstancesRequest);
    // Stop an Instance
    StopInstancesRequest stopInstancesRequest = new StopInstancesRequest().withInstanceIds(yourInstanceId);
    ec2Client.stopInstances(stopInstancesRequest).getStoppingInstances().get(0).getPreviousState().getName();
    // Describe an Instance
    DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
    DescribeInstancesResult response = ec2Client.describeInstances(describeInstancesRequest);
    System.out.println(response.getReservations().get(0).getInstances().get(0).getKernelId());
}
Also used : IpRange(com.amazonaws.services.ec2.model.IpRange) DescribeKeyPairsRequest(com.amazonaws.services.ec2.model.DescribeKeyPairsRequest) StartInstancesRequest(com.amazonaws.services.ec2.model.StartInstancesRequest) DescribeKeyPairsResult(com.amazonaws.services.ec2.model.DescribeKeyPairsResult) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) CreateSecurityGroupRequest(com.amazonaws.services.ec2.model.CreateSecurityGroupRequest) CreateKeyPairRequest(com.amazonaws.services.ec2.model.CreateKeyPairRequest) MonitorInstancesRequest(com.amazonaws.services.ec2.model.MonitorInstancesRequest) RebootInstancesRequest(com.amazonaws.services.ec2.model.RebootInstancesRequest) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) CreateKeyPairResult(com.amazonaws.services.ec2.model.CreateKeyPairResult) AuthorizeSecurityGroupIngressRequest(com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressRequest) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) IpPermission(com.amazonaws.services.ec2.model.IpPermission) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest) UnmonitorInstancesRequest(com.amazonaws.services.ec2.model.UnmonitorInstancesRequest)

Example 69 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project spring-integration-aws by spring-projects.

the class DynamoDbLocalRunning method apply.

@Override
public Statement apply(Statement base, Description description) {
    assumeTrue(dynamoDbOnline.get(this.port));
    String url = "http://localhost:" + this.port;
    this.amazonDynamoDB = AmazonDynamoDBAsyncClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("", ""))).withClientConfiguration(new ClientConfiguration().withMaxErrorRetry(0).withConnectionTimeout(1000)).withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(url, Regions.DEFAULT_REGION.getName())).build();
    try {
        this.amazonDynamoDB.listTables();
    } catch (SdkClientException e) {
        logger.warn("Tests not running because no DynamoDb on " + url, e);
        assumeNoException(e);
    }
    return super.apply(base, description);
}
Also used : AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) SdkClientException(com.amazonaws.SdkClientException) AwsClientBuilder(com.amazonaws.client.builder.AwsClientBuilder) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) ClientConfiguration(com.amazonaws.ClientConfiguration)

Example 70 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project spring-integration-aws by spring-projects.

the class KinesisLocalRunning method apply.

@Override
public Statement apply(Statement base, Description description) {
    assumeTrue(kinesisOnline.get(this.port));
    String url = "http://localhost:" + this.port;
    // See https://github.com/mhart/kinesalite#cbor-protocol-issues-with-the-java-sdk
    System.setProperty(SDKGlobalConfiguration.AWS_CBOR_DISABLE_SYSTEM_PROPERTY, "true");
    this.amazonKinesis = AmazonKinesisAsyncClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("", ""))).withClientConfiguration(new ClientConfiguration().withMaxErrorRetry(0).withConnectionTimeout(1000)).withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(url, Regions.DEFAULT_REGION.getName())).build();
    try {
        this.amazonKinesis.listStreams();
    } catch (SdkClientException e) {
        logger.warn("Tests not running because no Kinesis on " + url, e);
        assumeNoException(e);
    }
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            try {
                base.evaluate();
            } finally {
                System.clearProperty(SDKGlobalConfiguration.AWS_CBOR_DISABLE_SYSTEM_PROPERTY);
            }
        }
    };
}
Also used : AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) SdkClientException(com.amazonaws.SdkClientException) Statement(org.junit.runners.model.Statement) AwsClientBuilder(com.amazonaws.client.builder.AwsClientBuilder) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) ClientConfiguration(com.amazonaws.ClientConfiguration)

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