software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider Java Examples

The following examples show how to use software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: S3PinotFS.java    From incubator-pinot with Apache License 2.0 7 votes vote down vote up
@Override
public void init(Configuration config) {
  Preconditions.checkArgument(!isNullOrEmpty(config.getString(REGION)));
  String region = config.getString(REGION);

  AwsCredentialsProvider awsCredentialsProvider;
  try {

    if (!isNullOrEmpty(config.getString(ACCESS_KEY)) && !isNullOrEmpty(config.getString(SECRET_KEY))) {
      String accessKey = config.getString(ACCESS_KEY);
      String secretKey = config.getString(SECRET_KEY);
      AwsBasicCredentials awsBasicCredentials = AwsBasicCredentials.create(accessKey, secretKey);
      awsCredentialsProvider = StaticCredentialsProvider.create(awsBasicCredentials);
    } else {
      awsCredentialsProvider =
          AwsCredentialsProviderChain.builder().addCredentialsProvider(SystemPropertyCredentialsProvider.create())
              .addCredentialsProvider(EnvironmentVariableCredentialsProvider.create()).build();
    }

    _s3Client = S3Client.builder().region(Region.of(region)).credentialsProvider(awsCredentialsProvider).build();
  } catch (S3Exception e) {
    throw new RuntimeException("Could not initialize S3PinotFS", e);
  }
}
 
Example #2
Source File: PublishTextSMS.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public void sendMessage(String id) {

        Region region = Region.US_EAST_1;
        SnsClient snsClient = SnsClient.builder()
                .region(region)
                .credentialsProvider(EnvironmentVariableCredentialsProvider.create())
                .build();
        String message = "A new item with ID value "+ id +" was added to the DynamoDB table";
        String phoneNumber="ENTER MOBILE NUMBER"; //Replace with a mobile phone number

        try {
            PublishRequest request = PublishRequest.builder()
                    .message(message)
                    .phoneNumber(phoneNumber)
                    .build();

            PublishResponse result = snsClient.publish(request);

        } catch (SnsException e) {

            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }
 
Example #3
Source File: DependencyFactory.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * @return an instance of WafRegionalClient
 */
public static WafRegionalClient wafRegionalClient() {
    return WafRegionalClient.builder()
                   .credentialsProvider(EnvironmentVariableCredentialsProvider.create())
                   .region(Region.AP_SOUTHEAST_1)
                   .httpClientBuilder(ApacheHttpClient.builder())
                   .build();
}
 
Example #4
Source File: DependencyFactory.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * @return an instance of KinesisAsyncClient
 */
public static KinesisAsyncClient kinesisClient() {
    return KinesisAsyncClient.builder()
                   .credentialsProvider(EnvironmentVariableCredentialsProvider.create())
                   .region(Region.US_EAST_1)
                   .httpClientBuilder(NettyNioAsyncHttpClient.builder())
                   .build();
}
 
Example #5
Source File: DependencyFactory.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * @return an instance of S3Client
 */
public static S3Client s3Client() {
    return S3Client.builder()
                   .credentialsProvider(EnvironmentVariableCredentialsProvider.create())
                   .region(Region.US_WEST_2)
                   .httpClientBuilder(UrlConnectionHttpClient.builder())
                   .build();
}
 
Example #6
Source File: DependencyFactory.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * @return an instance of DynamoDbClient
 */
public static DynamoDbClient dynamoDbClient() {
    return DynamoDbClient.builder()
                   .credentialsProvider(EnvironmentVariableCredentialsProvider.create())
                   .region(Region.of(System.getenv(SdkSystemSetting.AWS_REGION.environmentVariable())))
                   .httpClientBuilder(ApacheHttpClient.builder())
                   .build();
}
 
Example #7
Source File: DependencyFactory.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * @return an instance of DynamoDbStreamsClient
 */
public static DynamoDbStreamsClient dynamoDbStreamsClient() {
    return DynamoDbStreamsClient.builder()
                   .credentialsProvider(EnvironmentVariableCredentialsProvider.create())
                   .region(Region.AP_SOUTHEAST_1)
                   .httpClientBuilder(ApacheHttpClient.builder())
                   .build();
}
 
Example #8
Source File: ProfileCredentialsUtils.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private AwsCredentialsProvider credentialSourceCredentialProvider(CredentialSourceType credentialSource) {
    switch (credentialSource) {
        case ECS_CONTAINER:
            return ContainerCredentialsProvider.builder().build();
        case EC2_INSTANCE_METADATA:
            return InstanceProfileCredentialsProvider.create();
        case ENVIRONMENT:
            return AwsCredentialsProviderChain.builder()
                .addCredentialsProvider(SystemPropertyCredentialsProvider.create())
                .addCredentialsProvider(EnvironmentVariableCredentialsProvider.create())
                .build();
        default:
            throw noSourceCredentialsException();
    }
}
 
Example #9
Source File: StreamingRetryApp.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String args[]) throws URISyntaxException, ExecutionException, InterruptedException, LineUnavailableException, FileNotFoundException {
    /**
     * Create Transcribe streaming retry client using AWS credentials.
     */

    TranscribeStreamingRetryClient client = new TranscribeStreamingRetryClient(EnvironmentVariableCredentialsProvider.create() ,endpoint, region);

    StartStreamTranscriptionRequest request = StartStreamTranscriptionRequest.builder()
            .languageCode(language)
            .mediaEncoding(encoding)
            .mediaSampleRateHertz(sample_rate)
            .build();
    /**
     * Start real-time speech recognition. The Transcribe streaming java client uses the Reactive-streams 
     * interface. For reference on Reactive-streams: 
     *     https://github.com/reactive-streams/reactive-streams-jvm
     */
    CompletableFuture<Void> result = client.startStreamTranscription(
            /**
             * Request parameters. Refer to API documentation for details.
             */
            request,
            /**
             * Provide an input audio stream.
             * For input from a microphone, use getStreamFromMic().
             * For input from a file, use getStreamFromFile().
             */
            new AudioStreamPublisher(
                    new FileInputStream(new File("FileName"))),
            /**
             * Object that defines the behavior on how to handle the stream
             */
            new StreamTranscriptionBehaviorImpl());

    /**
     * Synchronous wait for stream to close, and close client connection
     */
    result.get();
    client.close();
}
 
Example #10
Source File: DynamoDBEnhanced.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public void injectDynamoItem(Greeting item){

        Region region = Region.US_EAST_1;
        DynamoDbClient ddb = DynamoDbClient.builder()
                .region(region)
                .credentialsProvider(EnvironmentVariableCredentialsProvider.create())
                .build();

        try {

            DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder()
                    .dynamoDbClient(ddb)
                    .build();

            //Create a DynamoDbTable object
            DynamoDbTable<GreetingItems> mappedTable = enhancedClient.table("Greeting", TABLE_SCHEMA);
            GreetingItems gi = new GreetingItems();
            gi.setName(item.getName());
            gi.setMessage(item.getBody());
            gi.setTitle(item.getTitle());
            gi.setId(item.getId());

            PutItemEnhancedRequest enReq = PutItemEnhancedRequest.builder(GreetingItems.class)
                    .item(gi)
                    .build();

            mappedTable.putItem(enReq);

        } catch (Exception e) {
            e.getStackTrace();
        }

    }
 
Example #11
Source File: AwsModule.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public AwsCredentialsProvider deserializeWithType(
    JsonParser jsonParser, DeserializationContext context, TypeDeserializer typeDeserializer)
    throws IOException {
  Map<String, String> asMap =
      jsonParser.readValueAs(new TypeReference<Map<String, String>>() {});

  String typeNameKey = typeDeserializer.getPropertyName();
  String typeName = asMap.get(typeNameKey);
  if (typeName == null) {
    throw new IOException(
        String.format("AWS credentials provider type name key '%s' not found", typeNameKey));
  }

  if (typeName.equals(StaticCredentialsProvider.class.getSimpleName())) {
    return StaticCredentialsProvider.create(
        AwsBasicCredentials.create(asMap.get(ACCESS_KEY_ID), asMap.get(SECRET_ACCESS_KEY)));
  } else if (typeName.equals(DefaultCredentialsProvider.class.getSimpleName())) {
    return DefaultCredentialsProvider.create();
  } else if (typeName.equals(EnvironmentVariableCredentialsProvider.class.getSimpleName())) {
    return EnvironmentVariableCredentialsProvider.create();
  } else if (typeName.equals(SystemPropertyCredentialsProvider.class.getSimpleName())) {
    return SystemPropertyCredentialsProvider.create();
  } else if (typeName.equals(ProfileCredentialsProvider.class.getSimpleName())) {
    return ProfileCredentialsProvider.create();
  } else if (typeName.equals(ContainerCredentialsProvider.class.getSimpleName())) {
    return ContainerCredentialsProvider.builder().build();
  } else {
    throw new IOException(
        String.format("AWS credential provider type '%s' is not supported", typeName));
  }
}
 
Example #12
Source File: AwsModuleTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testAwsCredentialsProviderSerializationDeserialization() throws Exception {
  AwsCredentialsProvider credentialsProvider = DefaultCredentialsProvider.create();
  String serializedCredentialsProvider = objectMapper.writeValueAsString(credentialsProvider);
  AwsCredentialsProvider deserializedCredentialsProvider =
      objectMapper.readValue(serializedCredentialsProvider, DefaultCredentialsProvider.class);
  assertEquals(credentialsProvider.getClass(), deserializedCredentialsProvider.getClass());

  credentialsProvider = EnvironmentVariableCredentialsProvider.create();
  serializedCredentialsProvider = objectMapper.writeValueAsString(credentialsProvider);
  deserializedCredentialsProvider =
      objectMapper.readValue(serializedCredentialsProvider, AwsCredentialsProvider.class);
  assertEquals(credentialsProvider.getClass(), deserializedCredentialsProvider.getClass());

  credentialsProvider = SystemPropertyCredentialsProvider.create();
  serializedCredentialsProvider = objectMapper.writeValueAsString(credentialsProvider);
  deserializedCredentialsProvider =
      objectMapper.readValue(serializedCredentialsProvider, AwsCredentialsProvider.class);
  assertEquals(credentialsProvider.getClass(), deserializedCredentialsProvider.getClass());

  credentialsProvider = ProfileCredentialsProvider.create();
  serializedCredentialsProvider = objectMapper.writeValueAsString(credentialsProvider);
  deserializedCredentialsProvider =
      objectMapper.readValue(serializedCredentialsProvider, AwsCredentialsProvider.class);
  assertEquals(credentialsProvider.getClass(), deserializedCredentialsProvider.getClass());

  credentialsProvider = ContainerCredentialsProvider.builder().build();
  serializedCredentialsProvider = objectMapper.writeValueAsString(credentialsProvider);
  deserializedCredentialsProvider =
      objectMapper.readValue(serializedCredentialsProvider, AwsCredentialsProvider.class);
  assertEquals(credentialsProvider.getClass(), deserializedCredentialsProvider.getClass());
}