Skip to content

Commit

Permalink
fix(requirement): add boto3
Browse files Browse the repository at this point in the history
  • Loading branch information
giangbui committed Apr 25, 2018
1 parent 48fff4c commit c0ce565
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
55 changes: 55 additions & 0 deletions peregrine/utils/s3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import boto3
import flask


def get_s3_client(host):
"""
Get a connection to a given storage host based on configuration in the
current app context.
"""
config = flask.current_app.config["STORAGE"]["s3"]
return boto3.client(
's3',
aws_access_key_id=config["keys"][host]["access_key"],
aws_secret_access_key=config["keys"][host]["secret_key"]
)


def get_submission_bucket():
conn = get_s3_client(flask.current_app.config['SUBMISSION']['host'])
return conn.get_bucket(flask.current_app.config['SUBMISSION']['bucket'])


def put_data_to_s3(filename, key_name):
host = flask.current_app.config['SUBMISSION']['host']
bucket_name = flask.current_app.config['SUBMISSION']['bucket']

data = open(filename, 'rb')
config = flask.current_app.config["STORAGE"]["s3"]

try:
s3 = boto3.resource(
's3',
aws_access_key_id=config["keys"][host]["access_key"],
aws_secret_access_key=config["keys"][host]["secret_key"])
s3.Bucket(bucket_name).put_object(Key=key_name, Body=data)
return True
except Exception:
return False


def generate_presigned_url(keyname):
host = flask.current_app.config['SUBMISSION']['host']
bucket_name = flask.current_app.config['SUBMISSION']['bucket']

client = get_s3_client(host)
url = client.generate_presigned_url(
ClientMethod='get_object',
Params={
'Bucket': bucket_name,
'Key': keyname,
'ResponseContentDisposition': 'attachment; filename=manifest_bag.zip',
'ResponseContentType': 'application/zip'
}
)
return url
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
bdbag==1.2.3
boto3==1.7.8
defusedxml==0.5.0
scipy==0.18.1
SurvivalPy==1.0.2
Expand Down

0 comments on commit c0ce565

Please sign in to comment.