diff --git a/README.md b/README.md index aec181f..a964e5e 100644 --- a/README.md +++ b/README.md @@ -32,10 +32,10 @@ $ python app.py WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: off -2018-09-23 14:01:54,639 werkzeug _log():88 INFO * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) +2018-09-23 14:01:54,639 werkzeug _log():88 INFO * Running on http://0.0.0.0:6000/ (Press CTRL+C to quit) ``` -Launch browser and access http://localhost:5000/ +Launch browser and access http://localhost:6000/ ## Pull Request If you want to contribute. Open a [pull request](https://github.com/amjad489/pys3browser/pulls) diff --git a/app.py b/app.py index b0b8e2e..de231d0 100644 --- a/app.py +++ b/app.py @@ -4,7 +4,7 @@ import logging from logging.handlers import RotatingFileHandler import os -import ConfigParser +import configparser import re logger = logging.getLogger() @@ -29,15 +29,27 @@ def validate_config_file(): logging.info("config file exists") else: logging.error("config file doesn't exists, creating config.") - config = ConfigParser.RawConfigParser() + config = configparser.RawConfigParser() config.add_section('credentials') config.set('credentials', 'aws_access_key', '') config.set('credentials', 'aws_secret_key', '') config.set('credentials', 's3_bucket_name', '') - with open(config_file, 'wb') as cfg_file: + with open(config_file, 'w') as cfg_file: config.write(cfg_file) + +def read_config_file(config_file): + config = configparser.RawConfigParser() + config.read_file(open(config_file)) + s3_endpoint_url = config.get('s3_endpoint', 'url', fallback=None) + s3_access_id = config.get('credentials', 'aws_access_key', fallback=None) + s3_access_key = config.get('credentials', 'aws_secret_key', fallback=None) + s3_bucket_name = config.get('credentials', 's3_bucket_name', fallback=None) + return s3_endpoint_url, s3_access_id, s3_access_key, s3_bucket_name + + + def get_fa_icon(_s3_key): _s3_filename, _s3_file_extension = os.path.splitext(_s3_key) if re.search(r'(gz|zip|bz2|7z|tar|nar|jar|war|ear)', _s3_file_extension): @@ -79,12 +91,8 @@ def home_page(): global conn global s3_bucket_name validate_config_file() - config = ConfigParser.ConfigParser() - config.readfp(open(config_file)) - s3_access_id = config.get('credentials', 'aws_access_key') - s3_access_key = config.get('credentials', 'aws_secret_key') - s3_bucket_name = config.get('credentials', 's3_bucket_name') - conn = boto3.client('s3', aws_access_key_id=s3_access_id, aws_secret_access_key=s3_access_key) + s3_endpoint_url, s3_access_id, s3_access_key, s3_bucket_name = read_config_file(config_file) + conn = boto3.client('s3', endpoint_url=s3_endpoint_url, aws_access_key_id=s3_access_id, aws_secret_access_key=s3_access_key) if s3_access_id and s3_access_key and s3_bucket_name: return render_template('index.html', bucketname=s3_bucket_name) else: @@ -159,18 +167,25 @@ def generate(): def save_config_file(): global conn global s3_bucket_name - config = ConfigParser.RawConfigParser() - config.readfp(open(config_file)) + config = configparser.RawConfigParser() + config.read_file(open(config_file)) + + + if request.form['s3_url'] not in ["", None]: + if not config.has_section('s3_endpoint'): + config.add_section('s3_endpoint') + config.set('s3_endpoint', 'url', request.form['s3_url']) + if not config.has_section('credentials'): + config.add_section('credentials') config.set('credentials', 'aws_access_key', request.form['accesskey']) config.set('credentials', 'aws_secret_key', request.form['secretkey']) config.set('credentials', 's3_bucket_name', request.form['bucketname']) - with open(config_file, 'wb') as cfg_file: + with open(config_file, 'w') as cfg_file: config.write(cfg_file) logging.info("credentials saved to file.") - s3_access_id = config.get('credentials', 'aws_access_key') - s3_access_key = config.get('credentials', 'aws_secret_key') - s3_bucket_name = config.get('credentials', 's3_bucket_name') - conn = boto3.client('s3', aws_access_key_id=s3_access_id, aws_secret_access_key=s3_access_key) + + s3_endpoint_url, s3_access_id, s3_access_key, s3_bucket_name = read_config_file(config_file) + conn = boto3.client('s3', endpoint_url=s3_endpoint_url, aws_access_key_id=s3_access_id, aws_secret_access_key=s3_access_key) return redirect('/', code=302) @@ -183,4 +198,4 @@ def upload_file(): if __name__ == '__main__': - app.run(host='0.0.0.0') + app.run(host='0.0.0.0', port=6000) diff --git a/templates/configure.html b/templates/configure.html index a2325f7..3a2d59d 100644 --- a/templates/configure.html +++ b/templates/configure.html @@ -40,6 +40,10 @@
Provide the IAM user which has access to S3 bucket. For more details on creating user click here
+