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).
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Once again I'm answering my own question... I always seem to be better at answering it on my own once I ask others how to do it...
Anyway, you'll find the code below that will allow you to extract the user name for the access credentials you're currently using:
The execution is crude, but it works; or at least in the instances I've tried it has. If you just use the
System.out.println(iamServ.getUser());
, the username will show up looking similar to this:{User: {Path: /, UserName: UserName, UserId: xxxxxxxxxxxxxxxxxxxxxx, Arn: arn:aws:iam::xxxxxxxxxxx:user/UserName, CreateDate: Day Mon DayNum hr:min:sec Tmz Year, }, }
Otherwise the code shown returns UserName instead of all the extra "junk" in the string. Anyway, I hope this helps anyone else out there that might be looking for a solution to getting the current access ID's user name. If anyone knows another, better method, please let us know in the comments.
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.