AWS Systems Manager参数存储:使用StringList作为Java(Lambda)中的键值对 [英] AWS Systems Manager Parameter Store: Using StringList as Key Value Pairs in Java (Lambda)

查看:69
本文介绍了AWS Systems Manager参数存储:使用StringList作为Java(Lambda)中的键值对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Api Gateway,AWS Lambda和AWS RDS构建API.我的Lambda功能代码是Java.当前,我正在使用AWS Systems Manager参数存储成功连接到我的数据库.因此,我创建了一个名为"connection"的参数,该参数的类型为String并保存了完整的连接URL.在Lambda函数中,我可以通过以下方式成功访问此参数:

Im using Api Gateway and AWS Lambda and AWS RDS to build an API. My Lambda Function Code is Java. Currently im using the AWS Systems Manager Parameter Store successfully to connect to my Database. Therefore I created a parameter called "connection" which has the type String and holds my complete connection url. In Lambda functions i can access this parameter successfully this way:

GetParameterRequest parameterRequest = new GetParameterRequest().withName("connection").withWithDecryption(false);
        AWSSimpleSystemsManagement ssmclient = AWSSimpleSystemsManagementClientBuilder.defaultClient();
        GetParameterResult parameterResult = ssmclient.getParameter(parameterRequest);
        String url = parameterResult.getParameter().getValue();

现在我的问题是:在其他lambda函数中,我想发送邮件.为此,我要保存 SMTP服务器用户名密码默认发件人邮件,等等上.

Now my question: In other lambda functions I want to send mails. Therefor I want to save the SMTP-Server and the username and the password and a default sender mail and so on.

是否可以将此信息保存为StringStringType类型(如键/值对(映射))?这样的事情是可能的:

Would it be possible to save this information as Type StringList like Key/Value Pairs (Map)? That something like this could be possible:

    //Get StringList
    GetParameterRequest parameterRequest = new GetParameterRequest().withName("mailInfo").withWithDecryption(false);
    AWSSimpleSystemsManagement ssmclient = AWSSimpleSystemsManagementClientBuilder.defaultClient();
        GetParameterResult parameterResult = ssmclient.getParameter(parameterRequest);
    //Get values from the list
    String smtp_server = parameterResult.getParameter("smtp").getValue();
    String to_mail = parameterResult.getParameter("defaultToMail").getValue();
    ...

谢谢.

推荐答案

从根本上说,SSM参数始终是字符串

Fundamentally SSM parameters are always strings (docs). Whether they're just a string, or an encrypted string, or a "list" which is really a string where you've agreed to use a comma as a separator for items.

幸运的是,字符串非常灵活,其窍门是将数据与字符串表示形式进行封送处理.

Luckily strings are incredibly flexible and the trick will be to marshal your data to/from a string representation.

最明显的可能是使用SSM StringList 类型并要求对字符串列表进行排序,例如 mailInfo = smpt,用户名,密码,defaultToMail .此时,您可以进行自己的编组:

Probably the most obvious is to use the SSM StringList type and require that the string list is ordered, for example mailInfo = smpt,username,password,defaultToMail. At which point you can do your own marshalling:

GetParameterRequest parameterRequest = new GetParameterRequest().withName("mailInfo").withWithDecryption(false);
AWSSimpleSystemsManagement ssmclient = AWSSimpleSystemsManagementClientBuilder.defaultClient();
GetParameterResult parameterResult = ssmclient.getParameter(parameterRequest);
String mailInfo = parameterResult.getParameter().getValue();

String[] params = mailInfo.split(",");
String stmp = params[0];
String username = params[1];
String password = params[2];
String defaultToMail = params[3];

与其他选项(例如,序列化一个类并保存结果)相比,将StringList编组可能是更可取的,因为它在界面中是用户可编辑的.但是,您可能希望扩展格式,使顺序更明确,例如 smtp = smtp_value,username = username_value ... ,然后按 = 拆分每个列表项并进行相应分配.

Marshalling a StringList is probably prefered against other options (for example serialising a class and saving the result), as it's user-editable in the interface. You might, however, want to extend the format to be explicit about the ordering, e.g. smtp=smtp_value,username=username_value... and then split each list item by = and assign accordingly.

这篇关于AWS Systems Manager参数存储:使用StringList作为Java(Lambda)中的键值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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