简体   繁体   English

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

[英]Can not set environment variable on Mac OsX

I have a play framework java application which uses Amazon S3 and I do not want to set my secret and access keys in the configurations, but I want to save it in environment variable to make it more secure. 我有一个使用Amazon S3的play framework java应用程序,我不想在配置中设置我的秘密和访问密钥,但是我想将其保存在环境变量中以使其更安全。

I have this plugin: 我有这个插件:

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

}

and method enabled() prints that access key is null, which means that I did not save it properly in environment variables. enabled()方法将访问密钥打印为空,这意味着我没有正确地将其保存在环境变量中。

I opened .bash_profile (/Users/me/.bash_profile) with command atom .bash_profile and I pasted the following: 我使用atom .bash_profile命令atom .bash_profile打开.bash_profile (/Users/me/.bash_profile) ,并粘贴了以下内容:

export AWS_ACCESS_KEY=myAccessKey
export AWS_SECRET_KEY=mySecretKey

and saved it. 并保存。 After that, I ran printenv command which listed my environment variables, but neither AWS_ACCESS_KEY nor AWS_SECRET_KEY were there. 之后,我运行了printenv命令,该命令列出了我的环境变量,但是AWS_ACCESS_KEYAWS_SECRET_KEY都不存在。

What am I doing wrong? 我究竟做错了什么?

You may want to run : 您可能要运行:

source /Users/me/.bash_profile

bash_profile is only sourced by bash when started in interactive login mode. bash_profile仅在以交互式登录方式启动时由bash来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM