Created
August 3, 2017 20:50
-
-
Save csabatini/ca48e6b75ac982dcf11bbfd3fde48109 to your computer and use it in GitHub Desktop.
Tiny Flask web service for triggering S3 downloads
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
from flask import Flask, request, abort | |
import boto3 | |
import json | |
import os | |
app = Flask(__name__) | |
s3client = boto3.client('s3') | |
@app.route('/', methods=['GET']) | |
def index(): | |
return 'S3 Download Service' | |
# expects a json payload {"bucket":"", "key":"", "filename":""} | |
@app.route('/download', methods=['POST']) | |
def download(): | |
payload = request.get_json() | |
for ele in ['bucket', 'key', 'filename']: | |
if ele not in payload: | |
abort(400) | |
objResponse = s3client.get_object( | |
Bucket=payload['bucket'], | |
Key=payload['key'] | |
) | |
contents = objResponse['Body'].read() | |
with open(os.path.expanduser('~') + '/' + payload['filename'], 'w') as f: | |
f.write(contents) | |
return json.dumps({'result': 'success'}) | |
if __name__ == '__main__': | |
app.run(host="0.0.0.0") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment