使用AWS SSO时,AWS Java SDK找不到配置文件

使用AWS SSO进行登录时,我无法访问AWS。 我从我的计算机使用以下方式登录:

aws sso login --profile staging

配置文件配置如下:

[profile staging]
sso_start_url = https://som-nice-working-url
sso_region = us-east-1
sso_account_id = 1234
sso_role_name = the-role-name
region = eu-west-1
output = yaml

登录后,我可以通过AWS CLI访问AWS。

然后我设置变量:AWS_PROFILE=staging 但是在java上我得到了以下异常:

com.amazonaws.SdkClientException: Unable to load AWS credentials from any provider in the chain: [EnvironmentVariableCredentialsProvider: Unable to load AWS credentials from environment variables (AWS_ACCESS_KEY_ID (or AWS_ACCESS_KEY) and AWS_SECRET_KEY (or AWS_SECRET_ACCESS_KEY)), SystemPropertiesCredentialsProvider: Unable to load AWS credentials from Java system properties (aws.accessKeyId and aws.secretKey), WebIdentityTokenCredentialsProvider: You must specify a value for roleArn and roleSessionName, com.amazonaws.auth.profile.ProfileCredentialsProvider@369a95a5: No AWS profile named 'staging', com.amazonaws.auth.EC2ContainerCredentialsProviderWrapper@6d6f6ca9: Failed to connect to service endpoint: ]

我已尝试将ProfileCredentialsProvider与";Staging";一起使用,但结果相同。

CredentialsProvider我应该使用什么?

我的代码使用的是DefaultProviderChain:

AWSGlueClient.builder()
            .withRegion("eu-west-1")
            .build()

谢谢您。


解决方案

对于JAVA应用程序,您需要SSO Dependency。

截至撰写时,最新的IS版本2.16.76

// Gradle example
dependencies {
    
    implementation(platform("software.amazon.awssdk:bom:2.16.70"))
    implementation("software.amazon.awssdk:sso:2.16.76")
}

您还需要在~/.aws/configuration~/.aws/credentials中设置default配置文件

更多信息如下:

https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/credentials.html https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/setup.html#setup-credentials https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/setup-additional.html#setup-additional-credentials

但是,您还应该能够将AWS_PROFILE环境变量设置为您的配置文件,并且它也应该在没有SSO依赖的情况下神奇地工作。

在您的示例中,具体如下:

AWS_PROFILE=staging

相关文章