寻找样本代码以从AWS参数存储读取参数值

时间:2020-06-10 02:14:04

标签: java amazon-web-services amazon-iam

寻找示例Java代码以从aws参数存储中读取参数存储值(如RDS连接字符串)。适当的代码或任何参考链接。谢谢。

3 个答案:

答案 0 :(得分:1)

这是V2(不是V1)示例,用于从AWS参数存储中读取特定的参数值:

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 GetParameter {

    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);
         }

        // Get args
        String paraName = args[0];

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

        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);
        }
   }
}

答案 1 :(得分:0)

我认为GitHub可能会有所帮助。我搜索了SsmClient getParameter language:java,其中一些结果似乎很有希望。

This one for example:

    public static String getDiscordToken(SsmClient ssmClient) {
        GetParameterRequest request = GetParameterRequest.builder().
                name("/discord/token").
                withDecryption(Boolean.TRUE).
                build();
        GetParameterResponse response = ssmClient.getParameter(request);
        return response.parameter().value();
    }

答案 2 :(得分:0)

import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement;
import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClientBuilder;
import com.amazonaws.services.simplesystemsmanagement.model.GetParametersRequest;
import com.amazonaws.services.simplesystemsmanagement.model.GetParametersResult;

...

private static AWSSimpleSystemsManagement ssmclient = AWSSimpleSystemsManagementClientBuilder
            .standard().withRegion(System.getProperty("SystemsManagerRegion")).build();

...

GetParametersRequest paramRequest = new GetParametersRequest()
                .withNames(parameterName).withWithDecryption(encrypted);
        GetParametersResult paramResult = new GetParametersResult();
        paramResult = ssmclient.getParameters(paramRequest);