Flame_Phoenix
Flame_Phoenix

Reputation: 17594

Access AWS credentials file in WAR

I am making a Java application that uses, Spring, Maven and the AWS-SDK-Java. In order to the AWS SDK to work I have to place the AWSCredentials.properties file inside the "MyProject/src/main/resources" folder.

So far so so good. Then I have to create a .war file. To do that I use mvn install command and voilá.

Now inside the .war created, the file I want to access is in the "/WEB-INF/classes/" folder and I need to read it.

How can I access this file inside the war so I can read its content? I have experimented with ServeltContext but so far nothing I try works!

Upvotes: 1

Views: 912

Answers (2)

Sony Kadavan
Sony Kadavan

Reputation: 4072

It is generally not a good practice to keep credential in code package (war). Instead I would suggest you use IAM Roles.

This will make it easy for you to move your code from one AWS account to another (say dev environment to production). Since the code will be submitted to a version control system which will be accessed by many, it is also good from a security point of view to use IAM roles.

Upvotes: 2

Flame_Phoenix
Flame_Phoenix

Reputation: 17594

I found a way to do it. I can access the file by using:

InputStream inputStream = getClass().getClassLoader().getResourceAsStream("/resources/email/file.txt");

As explained in this discusison: Reading a text file in war archive

Upvotes: 1

Related Questions