com.amazonaws.AmazonWebServiceClient Java Examples

The following examples show how to use com.amazonaws.AmazonWebServiceClient. 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: TestPrestoS3FileSystem.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultS3ClientConfiguration()
        throws Exception
{
    HiveS3Config defaults = new HiveS3Config();
    try (PrestoS3FileSystem fs = new PrestoS3FileSystem()) {
        fs.initialize(new URI("s3n://test-bucket/"), new Configuration(false));
        ClientConfiguration config = getFieldValue(fs.getS3Client(), AmazonWebServiceClient.class, "clientConfiguration", ClientConfiguration.class);
        assertEquals(config.getMaxErrorRetry(), defaults.getS3MaxErrorRetries());
        assertEquals(config.getConnectionTimeout(), defaults.getS3ConnectTimeout().toMillis());
        assertEquals(config.getSocketTimeout(), defaults.getS3SocketTimeout().toMillis());
        assertEquals(config.getMaxConnections(), defaults.getS3MaxConnections());
        assertEquals(config.getUserAgentSuffix(), S3_USER_AGENT_SUFFIX);
        assertEquals(config.getUserAgentPrefix(), "");
    }
}
 
Example #2
Source File: CachingClientProvider.java    From fullstop with Apache License 2.0 6 votes vote down vote up
private CacheLoader<Key<?>, CacheValue> createCacheLoader() {
    return new CacheLoader<Key<?>, CacheValue>() {
        @Override
        public CacheValue load(@Nonnull final Key<?> key) {
            log.debug("Creating a new AmazonWebServiceClient client for {}", key);
            final STSAssumeRoleSessionCredentialsProvider tempCredentials = new STSAssumeRoleSessionCredentialsProvider
                    .Builder(buildRoleArn(key.accountId), ROLE_SESSION_NAME).withStsClient(awsSecurityTokenService)
                    .build();

            final String builderName = key.type.getName() + "Builder";
            final Class<?> className = ClassUtils.resolveClassName(builderName, ClassUtils.getDefaultClassLoader());
            final Method method = ClassUtils.getStaticMethod(className, "standard");
            Assert.notNull(method, "Could not find standard() method in class:'" + className.getName() + "'");

            final AwsClientBuilder<?, ?> builder = (AwsClientBuilder<?, ?>) ReflectionUtils.invokeMethod(method, null);
            builder.withCredentials(tempCredentials);
            builder.withRegion(key.region.getName());
            builder.withClientConfiguration(new ClientConfiguration().withMaxErrorRetry(MAX_ERROR_RETRY));
            final AmazonWebServiceClient client = (AmazonWebServiceClient) builder.build();
            return new CacheValue(client, tempCredentials);
        }
    };
}
 
Example #3
Source File: Aws.java    From digdag with Apache License 2.0 6 votes vote down vote up
static void configureServiceClient(AmazonWebServiceClient client, Optional<String> endpoint, Optional<String> regionName)
{
    // Configure endpoint or region. Endpoint takes precedence over region.
    if (endpoint.isPresent()) {
        client.setEndpoint(endpoint.get());
    }
    else if (regionName.isPresent()) {
        Regions region;
        try {
            region = Regions.fromName(regionName.get());
        }
        catch (IllegalArgumentException e) {
            throw new ConfigException("Illegal AWS region: " + regionName.get());
        }
        client.setRegion(Region.getRegion(region));
    }
}
 
Example #4
Source File: AwsParamStoreBootstrapConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void testWithStaticRegion() {
	String region = "us-east-2";
	AwsParamStoreProperties awsParamStoreProperties = new AwsParamStoreProperties();
	awsParamStoreProperties.setRegion(region);

	Method SSMClientMethod = ReflectionUtils.findMethod(
			AwsParamStoreBootstrapConfiguration.class, "ssmClient",
			AwsParamStoreProperties.class);
	SSMClientMethod.setAccessible(true);
	AWSSimpleSystemsManagementClient awsSimpleClient = (AWSSimpleSystemsManagementClient) ReflectionUtils
			.invokeMethod(SSMClientMethod, bootstrapConfig, awsParamStoreProperties);

	Method signingRegionMethod = ReflectionUtils
			.findMethod(AmazonWebServiceClient.class, "getSigningRegion");
	signingRegionMethod.setAccessible(true);
	String signedRegion = (String) ReflectionUtils.invokeMethod(signingRegionMethod,
			awsSimpleClient);

	assertThat(signedRegion).isEqualTo(region);
}
 
Example #5
Source File: AwsSecretsManagerBootstrapConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void testWithStaticRegion() {
	String region = "us-east-2";
	AwsSecretsManagerProperties awsParamStoreProperties = new AwsSecretsManagerProperties();
	awsParamStoreProperties.setRegion(region);

	Method SMClientMethod = ReflectionUtils.findMethod(
			AwsSecretsManagerBootstrapConfiguration.class, "smClient",
			AwsSecretsManagerProperties.class);
	SMClientMethod.setAccessible(true);
	AWSSecretsManagerClient awsSimpleClient = (AWSSecretsManagerClient) ReflectionUtils
			.invokeMethod(SMClientMethod, bootstrapConfig, awsParamStoreProperties);

	Method signingRegionMethod = ReflectionUtils
			.findMethod(AmazonWebServiceClient.class, "getSigningRegion");
	signingRegionMethod.setAccessible(true);
	String signedRegion = (String) ReflectionUtils.invokeMethod(signingRegionMethod,
			awsSimpleClient);

	assertThat(signedRegion).isEqualTo(region);
}
 
Example #6
Source File: ServiceBuilder.java    From aws-sdk-java-resources with Apache License 2.0 6 votes vote down vote up
private C createClient() {
    Class<? extends C> clientImplType = factory.getClientImplType();
    C client = ReflectionUtils.newInstance(
            clientImplType, credentials, configuration);

    if (client instanceof AmazonWebServiceClient) {
        AmazonWebServiceClient awsc = (AmazonWebServiceClient) client;
        if (region != null) {
            awsc.setRegion(region);
        }
        if (endpoint != null) {
            awsc.setEndpoint(endpoint);
        }
    }

    return client;
}
 
Example #7
Source File: TestPrestoS3FileSystem.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testUserAgentPrefix()
        throws Exception
{
    String userAgentPrefix = "agent_prefix";
    Configuration config = new Configuration(false);
    config.set(S3_USER_AGENT_PREFIX, userAgentPrefix);
    try (PrestoS3FileSystem fs = new PrestoS3FileSystem()) {
        fs.initialize(new URI("s3n://test-bucket/"), config);
        ClientConfiguration clientConfig = getFieldValue(fs.getS3Client(), AmazonWebServiceClient.class, "clientConfiguration", ClientConfiguration.class);
        assertEquals(clientConfig.getUserAgentSuffix(), S3_USER_AGENT_SUFFIX);
        assertEquals(clientConfig.getUserAgentPrefix(), userAgentPrefix);
    }
}
 
Example #8
Source File: AWSClientFactory.java    From awseb-deployment-plugin with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked", "deprecation"})
public <T> T getService(Class<T> serviceClazz)
        throws NoSuchMethodException, IllegalAccessException,
        InvocationTargetException, InstantiationException {

    Class<?> paramTypes[] = new Class<?>[]{AWSCredentialsProvider.class, ClientConfiguration.class};

    ClientConfiguration newClientConfiguration = new ClientConfiguration(this.clientConfiguration);

    if (AmazonS3.class.isAssignableFrom(serviceClazz)) {
        newClientConfiguration = newClientConfiguration.withSignerOverride("AWSS3V4SignerType");
    } else {
        newClientConfiguration = newClientConfiguration.withSignerOverride(null);
    }

    Object params[] = new Object[]{provider, newClientConfiguration};

    T resultObj = (T) ConstructorUtils.invokeConstructor(serviceClazz, params, paramTypes);

    if (DEFAULT_REGION.equals(defaultString(region, DEFAULT_REGION))) {
        return resultObj;
    } else {
        for (ServiceEndpointFormatter formatter : ServiceEndpointFormatter.values()) {
            if (formatter.matches(resultObj)) {
                ((AmazonWebServiceClient) resultObj).setEndpoint(getEndpointFor(formatter));
                break;
            }
        }
    }

    return resultObj;
}
 
Example #9
Source File: AWSClientFactory.java    From awseb-deployment-plugin with Apache License 2.0 5 votes vote down vote up
<T extends AmazonWebServiceClient> String getEndpointFor(T client) {
    try {
        URI endpointUri = (URI) FieldUtils.readField(client, "endpoint", true);

        return endpointUri.toASCIIString();
    } catch (Exception e) {
        return null;
    }
}
 
Example #10
Source File: CachingClientProvider.java    From fullstop with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends AmazonWebServiceClient> T getClient(final Class<T> type, final String accountId, final Region region) {
    final Key<T> k = new Key<>(type, accountId, region);
    return type.cast(cache.getUnchecked(k).client);
}
 
Example #11
Source File: CachingClientProvider.java    From fullstop with Apache License 2.0 4 votes vote down vote up
private void removalHook(RemovalNotification<Key<?>, CacheValue> notification) {
    log.debug("Shutting down expired client for key: {}", notification.getKey());
    final Optional<CacheValue> value = Optional.ofNullable(notification.getValue());
    value.map(v -> v.client).ifPresent(AmazonWebServiceClient::shutdown);
    value.map(v -> v.tempCredentials).ifPresent(STSAssumeRoleSessionCredentialsProvider::close);
}
 
Example #12
Source File: CachingClientProvider.java    From fullstop with Apache License 2.0 4 votes vote down vote up
private CacheValue(AmazonWebServiceClient client, STSAssumeRoleSessionCredentialsProvider tempCredentials) {
    this.client = client;
    this.tempCredentials = tempCredentials;
}
 
Example #13
Source File: EC2InstanceContextImpl.java    From fullstop with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends AmazonWebServiceClient> T getClient(final Class<T> type) {
    return clientProvider.getClient(type, getAccountId(), getRegion());
}
 
Example #14
Source File: ClientProvider.java    From fullstop with Apache License 2.0 votes vote down vote up
<T extends AmazonWebServiceClient> T getClient(Class<T> type, String accountId, Region region); 
Example #15
Source File: EC2InstanceContext.java    From fullstop with Apache License 2.0 votes vote down vote up
<T extends AmazonWebServiceClient> T getClient(Class<T> type);