我有一个使用Amazon S3的play框架java应用程序,我不想在配置中设置我的秘密和访问密钥,但我想将它保存在环境变量中以使其更安全。
我有这个插件:
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()
打印出访问键为空,这意味着我没有在环境变量中正确保存它。
我使用命令.bash_profile (/Users/me/.bash_profile)
打开了atom .bash_profile
并粘贴了以下内容:
export AWS_ACCESS_KEY=myAccessKey
export AWS_SECRET_KEY=mySecretKey
并保存。之后,我运行printenv
命令列出了我的环境变量,但AWS_ACCESS_KEY
和AWS_SECRET_KEY
都没有。
我做错了什么?
答案 0 :(得分:2)
您可能想要运行:
source /Users/me/.bash_profile
bash_profile
仅在以交互式登录模式启动时由bash提供。