![](/img/trans.png)
[英]Access environment variables stored in Google Secret Manager from Bitbucket pipelines
[英]How to access environment variables from .properties using java?
我正在使用 java 来实施 AWS SQS。 我想要一个 .properties 文件,我想在其中存储和访问 AWS SQS 访问和密钥的环境变量。 目前,my.properties 文件是:
dev.access.key={$AWS_ACCESS_KEY}
dev.secret.access.key={$AWS_SECRET_ACCESS_KEY}
我访问这些变量的代码是:
String awsAccessKey = properties.getProperty("dev.access.key");
String awsSecretAccessKey = properties.getProperty("dev.secret.access.key");
我已经实例化了属性并且定义明确。 我能够获得其他属性,但无法访问我需要的环境变量 Access 和 Secret Key。 目前是 awsAccessKey,awsSecretAccessKey 定义为 null。我访问这些环境变量的语法是否有问题?
这可以通过https://github.com/webcompere/lightweight-config来实现
您的占位符语法需要稍作更改:
dev.access.key=${AWS_ACCESS_KEY}
dev.secret.access.key=${AWS_SECRET_ACCESS_KEY}
然后你会写:
Properties myProps = ConfigLoader.loadPropertiesFromResource(
myproperties.properties");
Lightweight Config 还支持文件系统上的属性,而不是类路径上的属性(如上所述),并允许您使用 YML 而不是.properties
语法。
您可以使用org.apache.commons.text.StringSubstitutor
import java.util.Properties;
import org.apache.commons.text.StringSubstitutor;
public class EnvSubst {
public static void main(String[] args) {
// For the demo, create a Properties, normally you will read Properties from file
Properties p = new Properties();
p.put("tempfolder", "${TEMP}");
System.out.println("path value : " +
new StringSubstitutor(System.getenv()).replace(p.get("tempfolder")));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.