Python - Read key and secret to Athena - from credential file
- bdata3
- Feb 20, 2020
- 1 min read
You don't need to hardcode your credentials in your program just read them - use the following code, read access and secret and select from aws Athena:
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
def run_query(q_text,profile='default',FILE_NAME='',bck_name=STAGE_BUKET):
from pyathena import connect
user,key=get_profile_credentials(profile)
cursor = connect(aws_access_key_id=user,
aws_secret_access_key=key,
s3_staging_dir='s3://c-res-stg-d/s1/',
region_name='eu-west-1').cursor()
res=cursor.execute(q_text)
return(res)
if __name__=='__main__':
print([x for x in run_query("select 'yes' "))
Comments