- 
      
- 
        Save jqn/fa7ec283283cdaaa2f07666c36233561 to your computer and use it in GitHub Desktop. 
    Simple python 2.7 implementation of a config reader for ~/.aws/credentials file
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | def get_profile_credentials(profile_name): | |
| from ConfigParser import ConfigParser | |
| from ConfigParser import ParsingError | |
| from ConfigParser import NoOptionError | |
| from ConfigParser import NoSectionError | |
| from os import path | |
| config = ConfigParser() | |
| config.read([path.join(path.expanduser("~"),'.aws/credentials')]) | |
| try: | |
| aws_access_key_id = config.get(profile_name, 'aws_access_key_id') | |
| aws_secret_access_key = config.get(profile_name, 'aws_secret_access_key') | |
| except ParsingError: | |
| print('Error parsing config file') | |
| raise | |
| except (NoSectionError, NoOptionError): | |
| try: | |
| aws_access_key_id = config.get('default', 'aws_access_key_id') | |
| aws_secret_access_key = config.get('default', 'aws_secret_access_key') | |
| except (NoSectionError, NoOptionError): | |
| print('Unable to find valid AWS credentials') | |
| raise | |
| return aws_access_key_id, aws_secret_access_key | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment