0

我正在尝试使用 AWS SDK Java 调用 AWS ParameterStore (GetParameter),但我无法做到这一点。

我有 roleArn 但没有 accessKey,secretKey 所以我假设我必须使用assumeRole 并获得获取参数的访问权限。

我看到这段代码使用用户主目录中的配置属性,但我怎样才能让它使用我的 roleArn 访问 AWS

 package aws_sdk_java.aws;

import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;




import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ssm.SsmClient;
import software.amazon.awssdk.services.ssm.model.GetParameterRequest;
import software.amazon.awssdk.services.ssm.model.GetParameterResponse;
import software.amazon.awssdk.services.ssm.model.SsmException;

public class App {

    public static void main(String[] args) {

        final String USAGE = "\n" +
                "Usage:\n" +
                "    GetParameter <paraName>\n\n" +
                "Where:\n" +
                "    paraName - the name of the parameter.\n";

        if (args.length != 1) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String paraName = "Test";
        Region region = Region.US_WEST_2;

        SsmClient ssmClient = SsmClient.builder()
                .region(region)
                .build();

        getParaValue(ssmClient, paraName);
        ssmClient.close();
    }

    public static void getParaValue(SsmClient ssmClient, String paraName) {

        try {
            GetParameterRequest parameterRequest = GetParameterRequest.builder()
                .name(paraName)
                .build();

            GetParameterResponse parameterResponse = ssmClient.getParameter(parameterRequest);
            System.out.println("The parameter value is "+parameterResponse.parameter().value());

        } catch (SsmException e) {
        System.err.println(e.getMessage());
        System.exit(1);
        }
   }
}

所以基本上切换上面的代码来使用 roleARN 并获取参数。有人能帮我吗 ?

4

0 回答 0