Skip to content

Instantly share code, notes, and snippets.

@niklasvincent
Created June 16, 2016 20:57
Show Gist options
  • Save niklasvincent/73cd94cfb33730f3d9d4593e5a08e997 to your computer and use it in GitHub Desktop.
Save niklasvincent/73cd94cfb33730f3d9d4593e5a08e997 to your computer and use it in GitHub Desktop.
Download and parse configuration file from AWS S3
import boto3
import ConfigParser
import tempfile
class ConfigFromS3(object):
def __init__(self, bucket_name, key, region_name):
"""Read configuration file from S3 and parse it"""
defaults = {
"aws_region": region_name
}
session = boto3.session.Session(region_name=region_name)
self.bucket = session.resource('s3').Bucket(bucket_name)
temporary_file = tempfile.NamedTemporaryFile()
self.bucket.download_file(key, temporary_file.name)
self.config = ConfigParser.ConfigParser(defaults=defaults)
with open(temporary_file.name, 'r') as f:
self.config.readfp(f)
temporary_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment