Boto: Dynamically get aws_access_key_id and aws_secret_access_key in Python code from config?

I have aws_access_key_id and aws_secret_access_key stored in ~/.boto and were wondering if there is a way to get these values ​​in my Python code using Boto, since I need to paste them into my SQL statement so that I can copy the CSV file from S3 .

+2
python amazon-web-services boto
source share
2 answers

This should work:

 import boto access_key = boto.config.get_value('Credentials', 'aws_access_key_id') secret_key = boto.config.get_value('Credentials', 'aws_secret_access_key') 
+2
source share

Here's a helper that will look in ~/.aws/credentials if boto.config doesn't work. I did not examine it in detail, but it seems that Boto 2 does not look in ~/.aws/credentials .

 def get_aws_credentials(): # I think this will look in ~/.boto ([Credentials] section) aws_access_key_id = boto.config.get_value("Credentials", 'aws_access_key_id') aws_secret_access_key = boto.config.get_value("Credentials", 'aws_secret_access_key') # I don't think Boto 2 looks in ~/.aws/credentials, so we look if aws_access_key_id is None or aws_secret_access_key is None: with open(os.path.expanduser("~/.aws/credentials")) as f: for line in f: try: key, val = line.strip().split('=') if key == 'aws_access_key_id': aws_access_key_id = val elif key == 'aws_secret_access_key': aws_secret_access_key = val except ValueError: pass return aws_access_key_id, aws_secret_access_key 
0
source share

All Articles