Get Username from Amazon Access Key in Java

醉酒当歌 提交于 2019-12-21 20:58:38

问题


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).


回答1:


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.




回答2:


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:

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.identitymanagement.AmazonIdentityManagementClient;

public class test {

    private static AmazonIdentityManagementClient iamServ;

    private static void init() throws Exception {
        AWSCredentials credentials = new PropertiesCredentials(test.class.getResourceAsStream("AwsCredentials.properties"));        
        iamServ = new AmazonIdentityManagementClient(credentials);
    }

    public static void main(String[] args) throws Exception {
        init();
        String theUser = iamServ.getUser().toString().substring(27); 
        int userNameEnd = theUser.indexOf(",");
        String userName = theUser.substring(0, userNameEnd);
        System.out.println(userName);
    }
}

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.



来源:https://stackoverflow.com/questions/13595541/get-username-from-amazon-access-key-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!