File size: 1,567 Bytes
e5b72ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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/<filename>', 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)