gpt4 book ai didi

java - 无法在 Mac OsX 上设置环境变量

转载 作者:行者123 更新时间:2023-11-30 03:00:32 25 4
gpt4 key购买 nike

我有一个使用 Amazon S3 的 Play 框架 Java 应用程序,我不想在配置中设置我的 secret 和访问 key ,但我想将其保存在环境变量中以使其更安全。

我有这个插件:

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.google.inject.Inject;
import play.Application;
import play.Logger;
import play.Plugin;

public class S3Plugin extends Plugin {

public static final String AWS_S3_BUCKET = "aws.s3.bucket";
public static final String AWS_ACCESS_KEY = "aws.access.key";
public static final String AWS_SECRET_KEY = "aws.secret.key";
private final Application application;

public static AmazonS3 amazonS3;

public static String s3Bucket;

@Inject
public S3Plugin(Application application) {
this.application = application;
}

@Override
public void onStart() {
String accessKey = application.configuration().getString(AWS_ACCESS_KEY);
String secretKey = application.configuration().getString(AWS_SECRET_KEY);
s3Bucket = application.configuration().getString(AWS_S3_BUCKET);

if ((accessKey != null) && (secretKey != null)) {
AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
amazonS3 = new AmazonS3Client(awsCredentials);
amazonS3.createBucket(s3Bucket);
Logger.info("Using S3 Bucket: " + s3Bucket);
}
}

@Override
public boolean enabled() {

System.out.println("ACCESSKEY "+ application.configuration().keys().contains(AWS_ACCESS_KEY));

return (application.configuration().keys().contains(AWS_ACCESS_KEY) &&
application.configuration().keys().contains(AWS_SECRET_KEY) &&
application.configuration().keys().contains(AWS_S3_BUCKET));
}

}

方法 enabled() 打印访问 key 为 null,这意味着我没有将其正确保存在环境变量中。

我使用命令 atom .bash_profile 打开 .bash_profile (/Users/me/.bash_profile) 并粘贴了以下内容:

export AWS_ACCESS_KEY=myAccessKey
export AWS_SECRET_KEY=mySecretKey

并保存它。之后,我运行了 printenv 命令,其中列出了我的环境变量,但 AWS_ACCESS_KEYAWS_SECRET_KEY 都不在那里。

我做错了什么?

最佳答案

您可能想运行:

source /Users/me/.bash_profile

bash_profile 仅在以交互登录模式启动时由 bash 获取。

关于java - 无法在 Mac OsX 上设置环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36128500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com