Skip to content

Commit

Permalink
formmated files
Browse files Browse the repository at this point in the history
  • Loading branch information
psankhe28 committed Jun 29, 2024
1 parent bdb2b3f commit 4dd51ea
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 81 deletions.
143 changes: 74 additions & 69 deletions TusStorageHandler/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,93 +18,98 @@

tus_server = TusServer(minio_client, MINIO_BUCKET)


@app.route("/", methods=["GET"])
def home():
"""Endpoint to return a welcome message."""
return jsonify({"message": "Welcome to the MinIO TUS server!"}), 200
"""Endpoint to return a welcome message."""
return jsonify({"message": "Welcome to the MinIO TUS server!"}), 200


@app.route("/download", methods=["GET"])
def download_bucket():
"""Endpoint to list all objects in the MinIO bucket.
"""Endpoint to list all objects in the MinIO bucket.
It uses pre-signed download links.
"""
try:
objects = minio_client.list_objects(MINIO_BUCKET, recursive=True)
download_links = []
for obj in objects:
download_url = minio_client.presigned_get_object(
MINIO_BUCKET, obj.object_name, expires=datetime.timedelta(seconds=3600)
)
download_links.append(download_url)
It uses pre-signed download links.
"""
try:
objects = minio_client.list_objects(MINIO_BUCKET, recursive=True)
download_links = []
for obj in objects:
download_url = minio_client.presigned_get_object(
MINIO_BUCKET, obj.object_name, expires=datetime.timedelta(seconds=3600)
)
download_links.append(download_url)

return jsonify({"download_links": download_links}), 200
return jsonify({"download_links": download_links}), 200

except S3Error as err:
return jsonify({"error": str(err)}), 500

except S3Error as err:
return jsonify({"error": str(err)}), 500

@app.route("/download/<filename>", methods=["GET"])
def download_file(filename):
"""Endpoint to provide a pre-signed URL for downloading a specific file."""
try:
download_url = minio_client.presigned_get_object(
MINIO_BUCKET,
filename,
expires=datetime.timedelta(seconds=3600),
response_headers={
"response-content-disposition": f'attachment; filename="{filename}"'
},
)
return jsonify({"download_url": download_url}), 200

except S3Error as err:
return jsonify({"error": str(err)}), 500
"""Endpoint to provide a pre-signed URL for downloading a specific file."""
try:
download_url = minio_client.presigned_get_object(
MINIO_BUCKET,
filename,
expires=datetime.timedelta(seconds=3600),
response_headers={
"response-content-disposition": f'attachment; filename="{filename}"'
},
)
return jsonify({"download_url": download_url}), 200

except S3Error as err:
return jsonify({"error": str(err)}), 500


@app.route("/list_files", methods=["GET"])
def list_files():
"""Endpoint to list all files in the MinIO bucket."""
try:
objects = minio_client.list_objects(MINIO_BUCKET)
files = [obj.object_name for obj in objects]
return jsonify({"files": files}), 200
"""Endpoint to list all files in the MinIO bucket."""
try:
objects = minio_client.list_objects(MINIO_BUCKET)
files = [obj.object_name for obj in objects]
return jsonify({"files": files}), 200

except S3Error as err:
return jsonify({"error": str(err)}), 500

except S3Error as err:
return jsonify({"error": str(err)}), 500

@app.route("/upload", methods=["POST"])
def upload_file():
"""Endpoint to handle file uploads using the TUS protocol."""
if "file" not in request.files:
return jsonify({"error": "No file part in the request"}), 400

uploaded_files = request.files.getlist("file")
success_responses = []
error_responses = []

for uploaded_file in uploaded_files:
if uploaded_file.filename == "":
error_responses.append({"error": "Empty filename provided"})
continue

upload_id = str(uuid.uuid4())
filename = secure_filename(uploaded_file.filename)
file_content = uploaded_file.read()

if file_content:
tus_server.write_file(upload_id, file_content)
tus_server.assemble_file(upload_id, filename)
location = f"http://{request.host}/upload/{upload_id}"
success_responses.append({"filename": filename, "location": location})
else:
error_responses.append({
"filename": filename,
"error": "No file content provided"}
)

if error_responses:
return jsonify({"errors": error_responses}), 400
"""Endpoint to handle file uploads using the TUS protocol."""
if "file" not in request.files:
return jsonify({"error": "No file part in the request"}), 400

uploaded_files = request.files.getlist("file")
success_responses = []
error_responses = []

for uploaded_file in uploaded_files:
if uploaded_file.filename == "":
error_responses.append({"error": "Empty filename provided"})
continue

upload_id = str(uuid.uuid4())
filename = secure_filename(uploaded_file.filename)
file_content = uploaded_file.read()

if file_content:
tus_server.write_file(upload_id, file_content)
tus_server.assemble_file(upload_id, filename)
location = f"http://{request.host}/upload/{upload_id}"
success_responses.append({"filename": filename, "location": location})
else:
return jsonify({"success": success_responses}), 201
error_responses.append(
{"filename": filename, "error": "No file content provided"}
)

if error_responses:
return jsonify({"errors": error_responses}), 400
else:
return jsonify({"success": success_responses}), 201


if __name__ == "__main__":
app.run(debug=False)
app.run(debug=False)
3 changes: 2 additions & 1 deletion TusStorageHandler/minio_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- `minio_client`: Initialized Minio client instance
- `create_bucket()`: Function to create a new bucket if not exists.
"""

import os

from dotenv import load_dotenv
Expand All @@ -43,7 +44,7 @@
def create_bucket():
"""Creates a new bucket if it does not already exist.
This function checks if the bucket specified by MINIO_BUCKET_NAME exists.
This function checks if the bucket specified by MINIO_BUCKET_NAME exists.
If it does not exist, it creates a new bucket using the Minio client instance.
"""
if not minio_client.bucket_exists(MINIO_BUCKET_NAME):
Expand Down
23 changes: 12 additions & 11 deletions TusStorageHandler/tus_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class TusServer:
minio_client (Minio): The Minio client object used for file operations.
bucket_name (str): The name of the Minio bucket to operate on.
"""

def __init__(self, minio_client, bucket_name):
"""Initializes a new instance of the TusServer class.
Expand Down Expand Up @@ -60,20 +61,20 @@ def assemble_file(self, upload_id, filename):
filename (str): Name to assign to the uploaded file in Minio.
Raises:
Exception: If the file cannot be uploaded to Minio or if the file specified
Exception: If the file cannot be uploaded to Minio or if the file specified
by `upload_id` does not exist.
"""
file_path = self.get_file_path(upload_id)
if os.path.exists(file_path):
try:
# Upload the file to Minio
self.minio_client.fput_object(self.bucket_name, filename, file_path)
except S3Error as err:
print(f"Failed to upload file to Minio: {err}")
raise Exception("Failed to upload file to Minio") from err
finally:
# Remove the temporary file
os.remove(file_path)
try:
# Upload the file to Minio
self.minio_client.fput_object(self.bucket_name, filename, file_path)
except S3Error as err:
print(f"Failed to upload file to Minio: {err}")
raise Exception("Failed to upload file to Minio") from err
finally:
# Remove the temporary file
os.remove(file_path)
else:
raise Exception(f"File not found for upload_id: {upload_id}")
raise Exception(f"File not found for upload_id: {upload_id}")

0 comments on commit 4dd51ea

Please sign in to comment.