File size: 2,947 Bytes
7235b12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
Big File Streamer (Anti-Block Uploader)
=======================================
This script uploads MASSIVE files (10GB+) to APIs without crashing your RAM
or getting blocked by Windows Network limits. It streams the file directly
from the Hard Drive to the Network.

Usage:
  python bin\big_file_sender.py https://api.example.com/upload my_huge_model.bin
  python bin\big_file_sender.py https://api.example.com/upload data.zip --token "YOUR_API_KEY"
"""

import sys
import argparse
import requests
import os
from tqdm import tqdm

def upload_huge_file(url, file_path, token=None):
    if not os.path.exists(file_path):
        print(f"[ERROR] File not found: {file_path}")
        return

    file_size = os.path.getsize(file_path)
    file_name = os.path.basename(file_path)
    
    print(f"[INFO] Preparing to stream: {file_name} ({file_size / (1024*1024):.2f} MB)")
    print(f"[INFO] Target API: {url}")
    
    headers = {
        "Content-Type": "application/octet-stream",
        "Content-Disposition": f'attachment; filename="{file_name}"',
        # Keep connection alive so Windows doesn't drop it
        "Connection": "keep-alive" 
    }
    
    if token:
        headers["Authorization"] = f"Bearer {token}"

    # Create a streaming generator to read the file in chunks of 8MB
    # This prevents Windows RAM overload and bypasses strict buffer limits
    def file_stream_generator(filepath, chunk_size=8192*1024):
        with open(filepath, 'rb') as f:
            with tqdm(total=file_size, unit='B', unit_scale=True, desc="Uploading") as pbar:
                while True:
                    chunk = f.read(chunk_size)
                    if not chunk:
                        break
                    yield chunk
                    pbar.update(len(chunk))

    try:
        # requests will stream the generator directly to the network socket
        response = requests.post(
            url, 
            data=file_stream_generator(file_path), 
            headers=headers,
            stream=True,
            timeout=3600 # 1 hour timeout for huge files
        )
        
        print("\n[SUCCESS] Upload Complete!")
        print(f"Server Response ({response.status_code}): {response.text}")
        
    except requests.exceptions.ConnectionError:
        print("\n[ERROR] Connection Dropped by Target Server or Windows Firewall.")
        print("Tip: If it keeps dropping, use the 'rclone' tool provided in your bin folder.")
    except Exception as e:
        print(f"\n[ERROR] Upload Failed: {e}")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Upload huge files via streaming.")
    parser.add_argument("url", help="The API Endpoint URL")
    parser.add_argument("file", help="Path to the large file")
    parser.add_argument("--token", help="Optional Bearer API Token", default=None)
    
    args = parser.parse_args()
    upload_huge_file(args.url, args.file, args.token)