简体   繁体   English

从 Java 中的 Amazon Access Key 获取用户名

[英]Get Username from Amazon Access Key in Java

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?有没有办法将用户名附加到您用来通过 Java 访问 AWS 的凭证的访问密钥上? 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).我希望能够获取在IAM 用户部分中定义的用户名,以便我可以设置特定于用户的存储桶/文件夹,然后根据访问密钥的用户名动态地将脚本指向它们(因此我可以更改访问密钥,如有必要,无需更改存储桶/文件夹名称)。

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());如果你只是使用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.否则,显示的代码将返回UserName而不是字符串中的所有额外“垃圾”。 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.无论如何,我希望这可以帮助其他任何可能正在寻找获取当前访问 ID 用户名的解决方案的人。 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.我现在找到了一种从 Java 中的 AWS 访问凭证获取用户名的更好方法。 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).这种新方法实际上是适用于 JavaAWS 开发工具包的一部分(在最初发布时可能还没有)。

Anyway, to get the user name, just use the following line: iamServ.getUser().getUser().getUserName();无论如何,要获取用户名,只需使用以下行: 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.我不确定他们为什么设置它,所以你必须调用.getUser()两次,但他们做到了。 Other methods you can use after the second .getUser() include: .getUserID() , .getArn() , .getPath() , and .getCreateDate() .您可以在第二个.getUser()之后使用的其他方法包括: .getUserID().getArn().getPath().getCreateDate()

Note: I'm leaving the old answer as it does still work, but this is now the better solution.注意:我将保留旧答案,因为它仍然有效,但现在这是更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM