Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
Refs #24. First crack at HTTP S3 download attempt.
Browse files Browse the repository at this point in the history
  • Loading branch information
winhamwr committed Apr 22, 2014
1 parent 9027d0d commit f4a1cdb
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions terrarium/terrarium.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import tempfile
import shutil
import logging
import requests

from logging import getLogger, StreamHandler

Expand Down Expand Up @@ -394,11 +395,27 @@ def _get_s3_bucket(self):
return boto.s3.bucket.Bucket(conn, name=self.args.s3_bucket)

def _attempt_s3_download(self, target):
# attempt to download from public S3 url
base_s3_url = "https://%(bucket_name)s.s3.amazonaws.com/%(key)s"
remote_key = self.make_remote_key()
bucket_name = self.args.s3_bucket
s3_url = base_s3_url % {"bucket_name": bucket_name, "key": remote_key}
r = requests.get(s3_url, stream=True)
if r.ok:
fd, archive = tempfile.mkstemp()
with open(archive, 'wb') as f:
for chunk in r.iter_content(1024):
f.write(chunk)

self.extract(archive, target)
os.close(fd)
os.unlink(archive)
return True

if not boto:
return False
bucket = self._get_s3_bucket()
if bucket:
remote_key = self.make_remote_key()
key = bucket.get_key(remote_key)
if key:
logger.info(
Expand Down Expand Up @@ -435,7 +452,8 @@ def download(self, target):
logger.error('Download archive failed')

if self.args.s3_bucket:
self._attempt_s3_download(target)
download_successful = self._attempt_s3_download(target)
return download_successful

def make_remote_key(self):
import platform
Expand Down

0 comments on commit f4a1cdb

Please sign in to comment.