简体   繁体   English

Python中的AWS Elastic Beanstalk环境变量

[英]AWS Elastic Beanstalk Environment Variables in Python

Recently I have been trying to deploy a django webapp to AWS Elastic Beanstalk and everything has been going fine. 最近我一直在尝试将django webapp部署到AWS Elastic Beanstalk,一切都很顺利。 However part of my app uses that Twitter API so I need to import my API keys. 但是,我的应用程序的一部分使用Twitter API,因此我需要导入我的API密钥。 My understanding is that I should use Configuration > Software Configurations > Environment Properties. 我的理解是我应该使用Configuration> Software Configurations> Environment Properties。 I set this up inputting my keys but when I checked the site it still failed. 我把它设置为输入我的密钥但是当我检查网站时它仍然失败了。

I have been using this to try and import the variables is that correct? 我一直用这个来尝试导入变量是否正确?

import os

os.enviorn.get('TWITTER_ACCESS_TOKEN')

I checked to see if the variables were making it to the server and when I ran eb printenv I was shown this: 我检查了变量是否进入服务器,当我运行eb printenv我看到了这个:

 Environment Variables:
     TWITTER_ACCESS_TOKEN = XXXXX
     TWITTER_ACCESS_SECRET = XXXX
     TWITTER_CONSUMER_SECRET = XXXX
     TWITTER_CONSUMER_KEY = XXXXX

Any help would be greatly appreciated. 任何帮助将不胜感激。

In order to get system environment from AWS Elastic Beanstalk Properties (which is not OS environment variables) you need to "source" it to your environment. 为了从AWS Elastic Beanstalk属性(不是OS环境变量)获取系统环境,您需要将其“源”到您的环境中。 In case of Python, EB Properties are stored at /opt/python/current/env file. 对于Python,EB Properties存储在/ opt / python / current / env文件中。 So simply run this command: 所以只需运行此命令:

source /opt/python/current/env

Now you got your env variables updated. 现在,您已更新了env变量。

The key you are trying to get doesn't exist among your environment variables. 您尝试获取的密钥在您的环境变量中不存在。 Changing the code to - os.environ.get('TWITTER_ACCESS_TOKEN') or any other key among your env vars should do the trick. 将代码更改为 - os.environ.get('TWITTER_ACCESS_TOKEN')或env变量中的任何其他键应该可以解决问题。

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

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