我已经阅读了一段时间的文档。我可以看到JavaScript和Go SDK的示例,它们通过将AWS_SDK_LOAD_CONFIG
环境变量设置为truthy值来显示如何加载配置文件。文档分别为here和here。
但是,根据我的要求,我必须使用Java。我在Java SDK中找不到等效的引用。这让我承担了三件事。
ack -i "AWS_SDK_LOAD_CONFIG"
进行搜索都会显示两个项目都没有使用此变量。为清楚起见,我需要加载的配置文件是sbx
,它位于我的配置中,但在凭证文件中没有相邻值。这是我的〜/ .aws / config文件:
[profile shared]
output = json
region = us-west-2
adfs_config.ssl_verification = True
adfs_config.role_arn = ....
adfs_config.adfs_host = ....
adfs_config.adfs_user = ....
[profile sbx]
role_arn = ... (this is different from the adfs_config.role_arn above)
source_profile = shared
region = us-west-2
和〜/ .aws / credentials文件:(此文件自动填充aws-adfs
命令。
[shared]
aws_access_key_id = ....
aws_secret_access_key = ....
aws_session_token = ....
aws_security_token = ....
答案 0 :(得分:0)
对于Java SDK,变量名称似乎为AWS_CONFIG_FILE
:https://github.com/aws/aws-sdk-java/blob/4734de6fb0f80fe5768a6587aad3b9d0eaec388f/aws-java-sdk-core/src/main/java/com/amazonaws/SDKGlobalConfiguration.java#L233
答案 1 :(得分:0)
如果您需要在配置文件中指定特定的配置文件,则需要使用ProfileCredentialsProvider:
(我没有在IDE中输入此代码,因此可能存在拼写错误;可以随意编辑):
ProfileCredentialsProvider creds = new ProfileCredentialsProvider("myProfile");
AmazonS3 s3Client = new AmazonS3ClientBuilder()
.withCredentials(creds)
.build()
这将(我相信)使用默认文件位置~/.aws/credentials
。还有一个构造函数,允许您将路径传递给文件。
如果您可以使用默认配置文件,则可以使用所有构建器公开的静态defaultClient()
函数。这样做的好处是,您可以使用显式环境变量覆盖cred,或从实例配置文件中检索。
答案 2 :(得分:0)
我想出了答案。此问题的问题在于,默认情况下已加载凭据文件,但它并不总是具有配置文件中的所有可用信息。我们需要同时加载和展平。
AWS已经提供了ProfileAssumeRoleCredentialsProvider
,使我们可以从个人资料中承担角色。一旦我们提供了所需的所有信息,它就可以毫无问题地承担角色(假设您的令牌是最新的)
/**
* @author Paul Nelson Baker
* @see <a href="https://github.com/paul-nelson-baker/">GitHub</a>
* @see <a href="https://www.linkedin.com/in/paul-n-baker/">LinkedIn</a>
* @since 2018-11
*/
public class CredentialsChain {
public static final AWSCredentialsProviderChain CREDENTIALS_PROVIDER_CHAIN;
static {
AllProfiles allProfiles = flattenConfigurationFiles(
DEFAULT_CONFIG_LOCATION_PROVIDER.getLocation(), // ~/.aws/config
DEFAULT_CREDENTIALS_LOCATION_PROVIDER.getLocation() // ~/.aws/credentials
);
String currentProfileName = AwsProfileNameLoader.INSTANCE.loadProfileName();
BasicProfile currentProfile = allProfiles.getProfile(currentProfileName);
STSProfileCredentialsService profileCredentialsService = new STSProfileCredentialsService();
// We stick our merged profile provider first, but we still want the default behavior to apply
// so create a new chain with the default chain as the tail provider.
CREDENTIALS_PROVIDER_CHAIN = new AWSCredentialsProviderChain(
new ProfileAssumeRoleCredentialsProvider(profileCredentialsService, allProfiles, currentProfile),
new DefaultAWSCredentialsProviderChain()
);
}
private static AllProfiles flattenConfigurationFiles(File firstFile, File... additionalFiles) {
// Utilize the AWS SDK to load the actual profile objects
List<ProfilesConfigFile> allProfileConfigFiles = Stream.concat(Stream.of(firstFile), Arrays.stream(additionalFiles))
.map(ProfilesConfigFile::new).collect(Collectors.toList());
// Process each file one by one, look at their profiles, and place their values into a single map
// Duplicate profiles will now have the single key/value pairs.
Map<String, Map<String, String>> buildingMap = new LinkedHashMap<>();
for (ProfilesConfigFile currentConfigFile : allProfileConfigFiles) {
for (Entry<String, BasicProfile> currentProfile : currentConfigFile.getAllBasicProfiles().entrySet()) {
// Some profiles are prefixed with "profile " so we want to cull it so we're actually merging the correct data
String currentProfileName = currentProfile.getKey().replaceAll("^profile\\s+", "");
if (!buildingMap.containsKey(currentProfileName)) {
buildingMap.put(currentProfileName, new LinkedHashMap<>());
}
Map<String, String> profileKeyValuePairs = buildingMap.get(currentProfileName);
for (Entry<String, String> overridingEntry : currentProfile.getValue().getProperties().entrySet()) {
profileKeyValuePairs.put(overridingEntry.getKey(), overridingEntry.getValue());
}
}
}
// Take the results, and convert them to AWS SDK Types
Map<String, BasicProfile> finalResult = new LinkedHashMap<>();
for (Entry<String, Map<String, String>> currentFinalProfile : buildingMap.entrySet()) {
String currentProfileName = currentFinalProfile.getKey();
finalResult.put(currentProfileName, new BasicProfile(currentProfileName, currentFinalProfile.getValue()));
}
return new AllProfiles(finalResult);
}
private CredentialsChain() {
}
}