According to http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html there are 5 ways to work with AWS Credentials. All 5, as far as I can see, offer someway to settup AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. Nevertheless, I use proxy configuration when I am developing but I don't need it when it is running in AWS. I am manually commenting the code where I set up the proxy details while deploying to AWS. I couldn't find better solution but I am sure I am not doing the best way. My straigh question is: how can I turn it more flexible? I mean, using my local proxy configuration but taking it out when deploying the code? Certainly, there is some option using maven profile or other way.
Basically, the point is: I am using "new AmazonS3Client(credentials, config)" while developing but "new AmazonS3Client(credentials)" when building and deploying to AWS.
@Component
public class MyClass {
@Value("${bucketName}")
private String bucketName;
@Value("${accessKey}")
private String accessKey;
@Value("${secretKey}")
private String secretKey;
//...
AWSCredentials credentials = null;
credentials = new BasicAWSCredentials(accessKey, secretKey);
When I am developing:
ClientConfiguration config = new ClientConfiguration();
String proxyHost = "my.local.proxy.ip";
String proxyPort = "8080";
String proxyUser = "my-local-network-user";
String proxyPassword = "pwpwd";
config.setProxyHost(proxyHost);
config.setProxyPort(Integer.valueOf(proxyPort));
config.setProxyUsername(proxyUser);
config.setProxyPassword(proxyPassword);
AmazonS3 s3client = new AmazonS3Client(credentials, config);
when deployed to AWS
AmazonS3 s3client = new AmazonS3Client(credentials);
application.properties (file commonly used by Spring Boot)
accessKey=A..W
secretKey=b1234..rewq
*** Edited in February 1st 2017
I am using this work around but I am sure there is better way to accomplish it
@Value("${proxyHost}")
private String proxyHost;
@Value("${proxyPort}")
private Integer proxyPort;
@Value("${proxyUser}")
private String proxyUser;
@Value("${proxyPassword}")
private String proxyPassword;
//...
credentials = new BasicAWSCredentials(accessKey, secretKey);
if (proxyHost != null && proxyHost.length() > 0 && proxyPort != null && proxyUser != null
&& proxyUser.length() > 0 && proxyPassword != null && proxyPassword.length() > 0) {
config = new ClientConfiguration();
config.setProxyHost(proxyHost);
config.setProxyPort(proxyPort);
config.setProxyUsername(proxyUser);
config.setProxyPassword(proxyPassword);
s3client = new AmazonS3Client(credentials, config);
} else {
s3client = new AmazonS3Client(credentials);
}