【发布时间】:2019-02-08 14:51:53
【问题描述】:
我想在本地测试我的 AWS 代码,所以我必须为 AWS 客户端设置代理。
在我的环境中通过变量HTTPS_PROXY 设置了一个代理主机(http://user@pass:my-corporate-proxy.com:8080)。
我没有找到如何设置整个代理的方法,所以我想出了以下代码:
AmazonSNS sns = AmazonSNSClientBuilder.standard()
.withClientConfiguration(clientConfig(System.getenv("HTTPS_PROXY")))
.withRegion(Regions.fromName(System.getenv("AWS_REGION")))
.withCredentials(new DefaultAWSCredentialsProviderChain())
.build();
ClientConfiguration clientConfig(String proxy) {
ClientConfiguration configuration = new ClientConfiguration();
if (proxy != null && !proxy.isEmpty()) {
Matcher matcher = Pattern.compile("(\\w{3,5})://((\\w+):(\\w+)@)?(.+):(\\d{1,5})").matcher(proxy);
if (!matcher.matches()) {
throw new IllegalArgumentException("Proxy not valid: " + proxy);
}
configuration.setProxyHost(matcher.group(5));
configuration.setProxyPort(Integer.parseInt(matcher.group(6)));
configuration.setProxyUsername(matcher.group(3));
configuration.setProxyPassword(matcher.group(4));
}
return configuration;
}
整个方法clientConfig 只是样板代码。
有什么优雅的方法可以做到这一点吗?
【问题讨论】:
-
老实说,考虑到您的环境,这听起来和它会得到的一样好。我不确定在未设置代理的情况下发送一个空的
ClientConfiguration会做什么,因为我没有使用它,但我假设在这种情况下它会被忽略。 -
像
clientConfiguration.setProxy(String proxy)这样的方法会好得多...
标签: amazon-web-services aws-java-sdk