-
-
Save drwelby/5181820 to your computer and use it in GitHub Desktop.
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/env python | |
"""Script to upload contents of an mbtile to an s3 storage bucket""" | |
from boto.s3.connection import S3Connection | |
from boto.exception import S3ResponseError | |
import sqlite3 | |
import sys | |
import os | |
import time | |
def main(): | |
"""Start here""" | |
print "Opening file" | |
# Try to work with the mbtile file | |
mbcon = sqlite3.connect(mbtile_fname) | |
metadata = dict(mbcon.execute('select name, value from metadata;').fetchall( | |
)) | |
bucket_name = metadata['name'].lower().replace(' ','_') | |
total = mbcon.execute('select count(zoom_level) from tiles;').fetchone()[0] | |
print "Connecting to s3" | |
# Try to connect to s3 and create our bucket | |
s3con = S3Connection(AWS_ID, AWS_SECRET_KEY) | |
bucket = s3con.create_bucket(bucket_name) | |
bucket.set_acl('public-read') | |
# Set up the visuals | |
done = 0 | |
# Do the work | |
for tile in mbcon.execute('select zoom_level, tile_column, tile_row, tile_data from tiles;'): | |
try_key(tile, bucket) | |
done = done + 1 | |
sys.stdout.write("\r%s of %s" % (done,total)) | |
sys.stdout.flush() | |
return(0) | |
def try_key(tile,bucket,mime_type = 'image/png'): | |
name = "%s/%s/%s.png" % (tile[0], tile[1], tile[2]) | |
key = bucket.new_key(name) | |
key.content_type = mime_type | |
key.set_contents_from_string(tile[3]) | |
try: | |
key.set_acl('public-read') | |
except S3ResponseError: | |
sys.stdout.write("\ntrying %s key again\n" % name) | |
sys.stdout.flush() | |
time.sleep(2) | |
try_key(tile,bucket,mime_type = 'image/png') | |
if __name__ == '__main__': | |
AWS_ID = os.environ['AWS_ID'] | |
AWS_SECRET_KEY = os.environ['AWS_SECRET_KEY'] | |
mbtile_fname = sys.argv[1] | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment