Skip to content

Commit

Permalink
Expose public url for files on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
orenc17 committed Oct 31, 2024
1 parent 2b3ae2e commit efedd08
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
30 changes: 17 additions & 13 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import boto3
import botocore
import humanize
from flask import Flask, render_template, request
from flask import Flask, Response, redirect, render_template, request

app = Flask(__name__)
app.secret_key = "your_secure_random_key_here" # noqa: S105
Expand Down Expand Up @@ -44,12 +44,11 @@ class S3Entry:

name: str
type: str
url: str = ""
size: str = ""
date_modified: str = ""


def parse_responses(responses: list, s3_client: botocore.client.BaseClient, bucket_name: str, search_param: str) -> list[S3Entry]:
def parse_responses(responses: list, search_param: str) -> list[S3Entry]:
contents: set[S3Entry] = set()
for response in responses:
# Add folders to contents
Expand All @@ -61,16 +60,10 @@ def parse_responses(responses: list, s3_client: botocore.client.BaseClient, buck
if "Contents" in response:
for item in response["Contents"]:
if not item["Key"].endswith("/"):
url = s3_client.generate_presigned_url(
"get_object",
Params={"Bucket": bucket_name, "Key": item["Key"]},
ExpiresIn=3600,
) # URL expires in 1 hour
contents.add(
S3Entry(
name=f'{bucket_name}/{item["Key"]}',
name=item["Key"],
type="file",
url=url,
size=humanize.naturalsize(item["Size"]),
date_modified=item["LastModified"])
)
Expand Down Expand Up @@ -98,7 +91,7 @@ def list_objects(s3_client: botocore.client.BaseClient, bucket_name: str, path:
return responses


@app.route("/search//buckets/<bucket_name>", defaults={"path": ""})
@app.route("/search/buckets/<bucket_name>", defaults={"path": ""})
@app.route("/search/buckets/<bucket_name>/<path:path>")
def search_bucket(bucket_name: str, path: str) -> str:
s3_client = boto3.client("s3", **AWS_KWARGS)
Expand All @@ -121,7 +114,7 @@ def search_bucket(bucket_name: str, path: str) -> str:
return render_template("error.html", error=f"An unknown error occurred: {e}")

search_param = request.args.get("search", "")
contents = parse_responses(responses, s3_client, bucket_name, search_param)
contents = parse_responses(responses, search_param)
return render_template(
"bucket_contents.html",
contents=contents,
Expand Down Expand Up @@ -153,7 +146,7 @@ def view_bucket(bucket_name: str, path: str) -> str:
return render_template("error.html", error=f"An unknown error occurred: {e}")

search_param = request.args.get("search", "")
contents = parse_responses(responses, s3_client, bucket_name, search_param)
contents = parse_responses(responses, search_param)
return render_template(
"bucket_contents.html",
contents=contents,
Expand All @@ -163,5 +156,16 @@ def view_bucket(bucket_name: str, path: str) -> str:
)


@app.route("/download/buckets/<bucket_name>/<path:path>")
def download_file(bucket_name: str, path: str) -> Response:
s3_client = boto3.client("s3", **AWS_KWARGS)
url = s3_client.generate_presigned_url(
"get_object",
Params={"Bucket": bucket_name, "Key": path},
ExpiresIn=3600,
) # URL expires in 1 hour
return redirect(url)


if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000) # noqa: S104
2 changes: 1 addition & 1 deletion templates/bucket_contents.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h4>Contents of {{ bucket_name }}</h4>
{% if item.type == "folder" %}
<a href="{{ url_for('view_bucket', bucket_name=bucket_name, path=item.name) }}">{{ item.name }}</a>
{% else %}
{{ item.date_modified }} | {{ item.size }} | <a href="{{ item.url }}" target="_blank">{{ item.name }}</a>
{{ item.date_modified }} | {{ item.size }} | <a href="{{ url_for('download_file', bucket_name=bucket_name, path=item.name) }}" target="_blank">{{ item.name }}</a>
{% endif %}
</li>
{% endfor %}
Expand Down

0 comments on commit efedd08

Please sign in to comment.