从Java中的Amazon Access Key获取用户名 [英] Get Username from Amazon Access Key in Java

查看:193
本文介绍了从Java中的Amazon Access Key获取用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以获取用于通过Java访问AWS的凭证的用户名附加到访问密钥的方法?我希望能够获得在 IAM用户部分中定义的用户名,以便我可以设置用户特定的存储桶/文件夹,然后根据访问密钥的用户将脚本动态地指向它们.名称(因此将来可以在不需要更改存储桶/文件夹名称的情况下更改访问密钥).

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

推荐答案

我现在找到了一种从Java中的AWS Access Credentials获取用户名的更好方法.前面提到的答案是一个简单的解决方法,因为当时没有提供其他解决方案.此新方法实际上是适用于Java的AWS开发工具包的一部分(可能不是在原始帖子发布时).

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

无论如何,要获取用户名,只需使用以下行: iamServ.getUser().getUser().getUserName();

Anyway, to get the user name, just use the following line: iamServ.getUser().getUser().getUserName();

这将返回一个可以存储到变量的字符串.我不确定他们为什么设置它,所以您必须两次调用.getUser(),但是他们做到了.在第二个.getUser()之后可以使用的其他方法包括:.getUserID().getArn().getPath().getCreateDate().

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.

这篇关于从Java中的Amazon Access Key获取用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆