Skip to content

Support for optional S3 server. Support for Python3 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
49 changes: 32 additions & 17 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
from logging.handlers import RotatingFileHandler
import os
import ConfigParser
import configparser
import re

logger = logging.getLogger()
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)


Expand All @@ -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)
4 changes: 4 additions & 0 deletions templates/configure.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ <h5>Configure S3 credentials.</h5><hr>
<p>
Provide the IAM user which has access to S3 bucket. For more details on creating user <a href="https://docs.aws.amazon.com/AmazonS3/latest/dev/using-iam-policies.html" target="_blank">click here</a>
</p>
<div class="form-row">
<label for="s3_url">S3 endpoint url (optional)</label>
<input type="text" class="form-control" name="s3_url" id="s3_url">
</div>
<div class="form-row">
<div class="col-md-4 mb-3">
<label for="validationCustom01">AWS Access Key</label>
Expand Down
4 changes: 4 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ <h5 class="modal-title" id="exampleModalCenterTitle">Please provide all the deta
</div>
<div class="modal-body">
<form method="post" action="/saveConfig" class="needs-validation p-3 mb-5 rounded" novalidate>
<div class="form-row">
<label for="s3_url">S3 endpoint url (optional)</label>
<input type="text" class="form-control" name="s3_url" id="s3_url">
</div>
<div class="form-row">
<div class="col-md-4 mb-3">
<label for="validationCustom01">AWS Access Key</label>
Expand Down