0

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);
    }
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
DemeCarvO
  • 487
  • 4
  • 16
  • 28

1 Answers1

0

Hi you can always use multiple config file option such that one will be local-app.properties and live-app.properties

and when you start your application you pass along the file that you want to use as configuration using the parameter

--spring.config.location=local-app.properties

or

--spring.config.location=live-app.properties

your local file will contain properties for proxy and live wont contain it (thus setting it to null, i hope null works or you can add null checks).

for more details look at this question : Spring boot multiple config files it dosent answer your question but answer by ganesh jadhav clearly shows an example.

Community
  • 1
  • 1
  • Kiran, about "...setting it to null, i hope null works..." I get "java.lang.IllegalArgumentException: client configuration cannot be null" when "... new AmazonS3Client(credentials, null)". Well, I can check if itis null but it means that I will have different flow depending on profile I am using. I mean, I will control the route with an "if" to use either new AmazonS3Client(credentials) or new AmazonS3Client(credentials, config). Gentily, I would like to highlight this part of my question "using my local proxy configuration but taking it out when deploying the code?" – DemeCarvO Feb 01 '17 at 14:05
  • Please, note how strange is my solution although it works. I keep the values blank in application.properties and then I test if all four proxy variables were filled in. Note that by changing spring.config.location will not improve at all. I have to instanciate AmazonS3Client in diferent ways. Is there any approach where I can pass AmazonS3Client according to the profile? Maybe by extending it and initialize according to the profile? – DemeCarvO Feb 01 '17 at 14:25
  • AmazonS3Client.java source shows that AmazonS3Client(credentials) internally calls new AmazonS3Client(credentials, config) where config=new ClientConfiguration(); refer [AmazonS3Client source](http://grepcode.com/file/repo1.maven.org/maven2/com.amazonaws/aws-java-sdk/1.8.10/com/amazonaws/services/s3/AmazonS3Client.java#AmazonS3Client.%3Cinit%3E%28com.amazonaws.auth.AWSCredentials%2Ccom.amazonaws.ClientConfiguration%29) – Kiran Phadatare Feb 06 '17 at 13:47