portable-devtools / bin /big_file_sender.py
codekingpro's picture
Add files using upload-large-folder tool
7235b12 verified
Raw
History Blame Contribute Delete
2.95 kB
"""
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)