This repository was archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Allows anonymous download of S3 buckets #50
Open
cshinaver
wants to merge
10
commits into
master
Choose a base branch
from
issue_24
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
542f727
Updated docs for creating new environments
winhamwr 9027d0d
Pulled attempt to download from S3 bucket into its own method
winhamwr f4a1cdb
Refs #24. First crack at HTTP S3 download attempt.
winhamwr a6fcd82
Refs #24. Added requests to the dependencies.
winhamwr 5016a21
Refs #24. Don't immediately bail if boto isn't installed.
winhamwr 8e2a1d7
Refs #24 Fixed failing test to not immediately bail if boto not insta…
cshinaver 95b4a96
Added requests==1.0.0 requirement which was causing test errors
3f78eab
Refs #24 Added requests==1.0.0 requirement which was causing test errors
697de7f
Merge branch 'issue_24' of github.com:PolicyStat/terrarium into issue_24
e5f042d
Refs #24 Now displays helpful error when s3 bucket hash is not public…
cshinaver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| nose | ||
| requests==1.0.0 | ||
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| import tempfile | ||
| import shutil | ||
| import logging | ||
| import requests | ||
|
|
||
| from logging import getLogger, StreamHandler | ||
|
|
||
|
|
@@ -393,6 +394,50 @@ def _get_s3_bucket(self): | |
| pass | ||
| 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 | ||
| # Check if permissions denied | ||
| elif not self.args.s3_access_key and 'AccessDenied' in r.content: | ||
| print ( | ||
| 'Access was denied to s3 bucket hash %s ' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like we're trying the anonymous download even if a key is provided, right? I don't think this message is accurate. |
||
| 'and no key was provided. ' | ||
| 'Make sure the s3 bucket hash is public ' | ||
| 'readable.' % (remote_key) | ||
| ) | ||
|
|
||
| if not boto: | ||
| return False | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should give a different message if an access key was provided, but boto isn't installed. |
||
| bucket = self._get_s3_bucket() | ||
| if bucket: | ||
| key = bucket.get_key(remote_key) | ||
| if key: | ||
| logger.info( | ||
| 'Downloading %s/%s from S3 ' | ||
| '(this may take time) ...' | ||
| % (self.args.s3_bucket, remote_key) | ||
| ) | ||
| fd, archive = tempfile.mkstemp() | ||
| key.get_contents_to_filename(archive) | ||
| self.extract(archive, target) | ||
| os.close(fd) | ||
| os.unlink(archive) | ||
| return True | ||
|
|
||
| def download(self, target): | ||
| if self.args.storage_dir: | ||
| remote_archive = os.path.join( | ||
|
|
@@ -413,23 +458,10 @@ def download(self, target): | |
| os.unlink(local_archive) | ||
| return True | ||
| logger.error('Download archive failed') | ||
| if boto and self.args.s3_bucket: | ||
| bucket = self._get_s3_bucket() | ||
| if bucket: | ||
| remote_key = self.make_remote_key() | ||
| key = bucket.get_key(remote_key) | ||
| if key: | ||
| logger.info( | ||
| 'Downloading %s/%s from S3 ' | ||
| '(this may take time) ...' | ||
| % (self.args.s3_bucket, remote_key) | ||
| ) | ||
| fd, archive = tempfile.mkstemp() | ||
| key.get_contents_to_filename(archive) | ||
| self.extract(archive, target) | ||
| os.close(fd) | ||
| os.unlink(archive) | ||
| return True | ||
|
|
||
| if self.args.s3_bucket: | ||
| download_successful = self._attempt_s3_download(target) | ||
| return download_successful | ||
|
|
||
| def make_remote_key(self): | ||
| import platform | ||
|
|
@@ -493,8 +525,14 @@ def upload(self, target): | |
| target, | ||
| self.args.storage_dir, | ||
| ) | ||
| if boto and self.args.s3_bucket: | ||
| self.upload_to_s3(target) | ||
| if self.args.s3_bucket: | ||
| if boto: | ||
| self.upload_to_s3(target) | ||
| else: | ||
| logger.critical( | ||
| "--upload combined with --s3-bucket requires that you " | ||
| "have boto installed, which does not appear to be the case" | ||
| ) | ||
|
|
||
| def create_bootstrap(self, dest): | ||
| extra_text = ( | ||
|
|
@@ -784,12 +822,6 @@ def parse_args(): | |
| args = ap.parse_args() | ||
| args.add_sensitive_arguments(*sensitive_arguments) | ||
|
|
||
| if not boto and args.s3_bucket is not None: | ||
| ap.error( | ||
| '--s3-bucket requires that you have boto installed, ' | ||
| 'which does not appear to be the case' | ||
| ) | ||
|
|
||
| return args | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is already mentioned in
setup.py, you don't need to also add it here.