Skip to content

Commit 7e8a89c

Browse files
author
Jesse Crocker
committed
Switch to using boto3 for s3 uploads
1 parent 60554e8 commit 7e8a89c

1 file changed

Lines changed: 17 additions & 16 deletions

File tree

upload_mbtiles.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@
55
import sqlite3
66
from multiprocessing.pool import ThreadPool
77
from functools import partial
8-
import cStringIO
98

10-
from boto.s3.connection import S3Connection
11-
from boto.s3.key import Key
12-
from boto.exception import S3ResponseError
13-
import boto
9+
import boto3
1410
import click
1511

1612
import utils
@@ -47,22 +43,24 @@ def next(self):
4743
return zoom, x, y, tile
4844

4945

50-
def upload_tile(bucket, key_template, headers, tile_stuff, progress=True, retries=0):
46+
def upload_tile(s3, bucket, key_template, headers, tile_stuff, progress=True, retries=0):
5147
try:
5248
zoom, x, y, tile = tile_stuff
53-
k = Key(bucket)
54-
k.key = key_template.format(z=zoom, x=x, y=y)
55-
for key, value in headers.items():
56-
k.set_metadata(key, value)
57-
k.set_contents_from_string(str(tile))
49+
s3.put_object(Body=str(tile),
50+
Bucket=bucket,
51+
Key=key_template.format(z=zoom, x=x, y=y),
52+
ContentType=headers.get("Content-Type", None),
53+
ContentEncoding=headers.get("Content-Encoding", None),
54+
CacheControl=headers.get("Cache-Control", None)
55+
)
5856
global upload_count
5957
upload_count += 1
6058
if progress and upload_count % 10 == 0:
6159
print("%i/%i" % (upload_count, tile_count))
6260
except Exception, e:
6361
utils.error(str(e))
6462
if retries < 2:
65-
upload_tile(bucket, key_template, headers, tile_stuff, progress=progress, retries=retries + 1)
63+
upload_tile(s3, bucket, key_template, headers, tile_stuff, progress=progress, retries=retries + 1)
6664
else:
6765
raise Exception("Too Many upload failures")
6866

@@ -85,14 +83,17 @@ def upload(mbtiles, s3_url, threads, extension, header):
8583
s3_url: url to an s3 bucket to upload tiles to
8684
"""
8785
base_url = urlparse(s3_url)
88-
conn = S3Connection(calling_format=boto.s3.connection.OrdinaryCallingFormat())
89-
bucket = conn.get_bucket(base_url.netloc)
86+
87+
s3 = boto3.client('s3')
88+
bucket = base_url.netloc
9089
key_prefix = base_url.path.lstrip("/")
9190

9291
headers = {}
9392
if header is not None:
9493
for h in header:
9594
k,v = h.split(":")
95+
if k not in ("Cache-Control", "Content-Type", "Content-Encoding"):
96+
raise Exception("Unsupported header")
9697
headers[k] = v
9798

9899
if extension == ".pbf":
@@ -118,9 +119,9 @@ def upload(mbtiles, s3_url, threads, extension, header):
118119
tile_count = tiles.len()
119120

120121
key_template = key_prefix + "{z}/{x}/{y}" + extension
121-
print("uploading tiles from %s to s3://%s/%s" % (mbtiles, bucket.name, key_template))
122+
print("uploading tiles from %s to s3://%s/%s" % (mbtiles, bucket, key_template))
122123
pool = ThreadPool(threads)
123-
func = partial(upload_tile, bucket, key_template, headers)
124+
func = partial(upload_tile, s3, bucket, key_template, headers)
124125
pool.map(func, tiles)
125126

126127

0 commit comments

Comments
 (0)