Created
June 9, 2022 11:31
-
-
Save kdgregory/d6bed8c245ce326d461aeb65825358d2 to your computer and use it in GitHub Desktop.
Examples of using proxies with AWS Java SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// This code has been released into the public domain by the author. | |
// It may be used or modified without attribution. and is distributed | |
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | |
// either express or implied. | |
// | |
package com.kdgregory.sandbox.aws; | |
import com.amazonaws.ClientConfiguration; | |
import com.amazonaws.Protocol; | |
import com.amazonaws.services.securitytoken.AWSSecurityTokenService; | |
import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder; | |
import com.amazonaws.services.securitytoken.model.GetCallerIdentityRequest; | |
import com.amazonaws.services.securitytoken.model.GetCallerIdentityResult; | |
/** | |
* Retrieves the caller's identity using an explicit proxy server. | |
*/ | |
public class ExplicitProxyV1 | |
{ | |
public static void main(String[] argv) | |
throws Exception | |
{ | |
ClientConfiguration config = new ClientConfiguration() | |
.withProxyProtocol(Protocol.HTTP) | |
.withProxyHost("localhost") | |
.withProxyPort(3128) | |
.withNonProxyHosts("169.254.169.254"); | |
AWSSecurityTokenService client = AWSSecurityTokenServiceClientBuilder.standard() | |
.withClientConfiguration(config) | |
.build(); | |
GetCallerIdentityRequest request = new GetCallerIdentityRequest(); | |
GetCallerIdentityResult response = client.getCallerIdentity(request); | |
System.out.println("account: " + response.getAccount()); | |
System.out.println("user id: " + response.getUserId()); | |
System.out.println("arn: " + response.getArn()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// This code has been released into the public domain by the author. | |
// It may be used or modified without attribution. and is distributed | |
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | |
// either express or implied. | |
// | |
package com.kdgregory.sandbox.awsv2; | |
import java.net.URI; | |
import software.amazon.awssdk.http.apache.ApacheHttpClient; | |
import software.amazon.awssdk.http.apache.ProxyConfiguration; | |
import software.amazon.awssdk.services.sts.StsClient; | |
import software.amazon.awssdk.services.sts.model.*; | |
/** | |
* Retrieves the caller's identity using an explicit proxy server with the | |
* Apache HTTP client. | |
*/ | |
public class ExplicitProxyV2 | |
{ | |
public static void main(String[] argv) | |
throws Exception | |
{ | |
ProxyConfiguration config = ProxyConfiguration.builder() | |
.endpoint(new URI("http://localhost:3128")) | |
.addNonProxyHost("169.254.169.254") | |
.useSystemPropertyValues(Boolean.FALSE) | |
.build(); | |
ApacheHttpClient.Builder clientBuilder = ApacheHttpClient.builder() | |
.proxyConfiguration(config); | |
StsClient client = StsClient.builder() | |
.httpClientBuilder(clientBuilder) | |
.build(); | |
GetCallerIdentityRequest request = GetCallerIdentityRequest.builder().build(); | |
GetCallerIdentityResponse response = client.getCallerIdentity(request); | |
System.out.println("account: " + response.account()); | |
System.out.println("user id: " + response.userId()); | |
System.out.println("arn: " + response.arn()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment