Is there a way to get the User Name attached to the Access Key for the credentials you’re using to access AWS via Java? I would like to be able to get the User Name that’s defined in the IAM Users section so that I can setup user-specific buckets/folders and then dynamically point the script to them based on the access key’s User Name (so I can change the access key in the future, if necessary, without changing the bucket/folder name).
Advertisement
Answer
I’ve now found a much better method of getting the user name from the AWS Access Credentials in Java. The previously mentioned answer was a simple work around since no other solution was provided at the time. This new method is actually part of the AWS SDK for Java (it may not have been at the time of the original post).
Anyway, to get the user name, just use the following line:
iamServ.getUser().getUser().getUserName();
This returns a string that can be stored to a variable. I’m not exactly sure why they set it up so you have to call .getUser()
twice, but they did. Other methods you can use after the second .getUser()
include: .getUserID()
, .getArn()
, .getPath()
, and .getCreateDate()
.
Note: I’m leaving the old answer as it does still work, but this is now the better solution.