Created
June 29, 2016 20:08
-
-
Save deckerego/c18fe5e22280996e274527f68b1791c0 to your computer and use it in GitHub Desktop.
Extract data from a database table, format it as a CSV and upload it to S3
This file contains 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
#!/usr/bin/python | |
import MySQLdb | |
import csv | |
from boto.s3.connection import S3Connection | |
from boto.s3.key import Key | |
query = "\ | |
select id, name, gross_amt \ | |
from cafeteria \ | |
where not kids_eat;" | |
conn = MySQLdb.connect(user='hoagiesandgrinders', passwd='meatloafsandwich', host='sloppy.joe', db='NavyBeans') | |
print "Finding Food" | |
cursor = conn.cursor() | |
cursor.execute(query) | |
print "Writing Food List" | |
with open('foods.csv', 'wb') as csvfile: | |
writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONE) | |
writer.writerow(['id', 'name', 'gross_amt']) | |
writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONNUMERIC) | |
for (ident, name, grossness) in cursor: | |
writer.writerow([ident, name, grossness]) | |
print "Closing Connections" | |
cursor.close() | |
conn.close() | |
print "Uploading to S3" | |
s3conn = S3Connection('aws_access_key_id', 'aws_secret_access_key', calling_format='boto.s3.connection.OrdinaryCallingFormat') | |
bucket = s3conn.get_bucket('bukkit_name') | |
entry = Key(bucket) | |
entry.key = 'foods.csv' | |
entry.set_contents_from_filename('foods.csv') | |
entry.copy(entry.bucket.name, entry.name, {'Content-Type':'text/csv'}) | |
print "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment