from flask import Flask, send_file, request, jsonify import boto3 import os app = Flask(__name__) # AWS / DigitalOcean Spaces credentials ACCESS_ID = os.getenv('ACCESS_ID', 'DO0026WEQUG4WF6WQNJ9') SECRET_KEY = os.getenv('SECRET_KEY', 'UG7kQicGgWmkfVmESWK889RxZG49UqV7vRfYUJDFFUo') @app.route('/download/', methods=['GET']) def download_file(filename): # Configure the client with your credentials session = boto3.session.Session() client = session.client('s3', region_name='nyc3', endpoint_url='https://nyc3.digitaloceanspaces.com', aws_access_key_id=ACCESS_ID, aws_secret_access_key=SECRET_KEY) # Define the bucket and object key bucket_name = 'sing' # Your bucket name object_key = f'{filename}' # Construct the object key # Define the local path to save the file local_file_path = os.path.join('weights', filename) # Download the file from the bucket try: client.download_file(bucket_name, object_key, local_file_path) except client.exceptions.NoSuchKey: return jsonify({'error': 'File not found in the bucket'}), 404 except Exception as e: return jsonify({'error': str(e)}), 500 # Optional: Send the file directly to the client # return send_file(local_file_path, as_attachment=True) return jsonify({'success': True, 'message': 'File downloaded successfully', 'file_path': local_file_path}) if __name__ == '__main__': app.run(debug=True)