Spaces:
Runtime error
Runtime error
# remote_uploader.py | |
import requests | |
import argparse | |
import os | |
import time | |
from watchdog.observers import Observer | |
from watchdog.events import FileSystemEventHandler | |
class FileUploadHandler(FileSystemEventHandler): | |
def __init__(self, server_url, api_key, space_id): | |
self.server_url = server_url | |
self.api_key = api_key | |
self.space_id = space_id | |
self.uploaded_files = set() # 跟踪已上传的文件 | |
def on_created(self, event): | |
"""当新文件被创建时触发""" | |
if not event.is_directory: | |
# 等待一段时间确保文件写入完成 | |
time.sleep(1) | |
self.upload_file(event.src_path) | |
def on_modified(self, event): | |
"""当文件被修改时触发""" | |
if not event.is_directory: | |
# 等待一段时间确保文件写入完成 | |
time.sleep(1) | |
self.upload_file(event.src_path) | |
def upload_file(self, file_path): | |
"""上传单个文件""" | |
if not os.path.exists(file_path): | |
print(f"文件不存在: {file_path}") | |
return | |
filename = os.path.basename(file_path) | |
# 避免重复上传同一个文件 | |
if file_path in self.uploaded_files: | |
return | |
print(f"检测到新文件: {filename}") | |
print(f"正在上传到服务器...") | |
# 构建完整的 API 端点 URL | |
upload_url = f"{self.server_url.rstrip('/')}/api/remote_upload" | |
# 准备请求头和数据 | |
headers = { | |
'X-API-Key': self.api_key | |
} | |
data = { | |
'space_id': self.space_id | |
} | |
# 读取文件内容并上传 | |
try: | |
with open(file_path, 'rb') as f: | |
files = { | |
'file': (filename, f) | |
} | |
# 发送 POST 请求 | |
response = requests.post(upload_url, headers=headers, data=data, files=files) | |
# 打印服务器响应 | |
print(f"服务器响应 ({response.status_code}):") | |
try: | |
result = response.json() | |
print(result) | |
if response.status_code == 200: | |
self.uploaded_files.add(file_path) | |
print(f"✅ 文件 '{filename}' 上传成功!") | |
else: | |
print(f"❌ 上传失败: {result.get('error', '未知错误')}") | |
except requests.exceptions.JSONDecodeError: | |
print(response.text) | |
except IOError as e: | |
print(f"读取文件时出错: {e}") | |
except requests.exceptions.RequestException as e: | |
print(f"请求时出错: {e}") | |
def monitor_directory(watch_dir, server_url, api_key, space_id): | |
"""监听目录变化""" | |
if not os.path.exists(watch_dir): | |
print(f"创建监听目录: {watch_dir}") | |
os.makedirs(watch_dir, exist_ok=True) | |
print(f"🔍 开始监听目录: {watch_dir}") | |
print(f"📡 服务器地址: {server_url}") | |
print(f"🔑 Space ID: {space_id}") | |
print("等待新文件...") | |
print("-" * 50) | |
# 首先上传目录中已存在的文件 | |
handler = FileUploadHandler(server_url, api_key, space_id) | |
existing_files = [f for f in os.listdir(watch_dir) if os.path.isfile(os.path.join(watch_dir, f))] | |
if existing_files: | |
print(f"发现 {len(existing_files)} 个已存在的文件,开始上传...") | |
for filename in existing_files: | |
file_path = os.path.join(watch_dir, filename) | |
handler.upload_file(file_path) | |
print("-" * 50) | |
# 创建观察者并开始监听 | |
observer = Observer() | |
observer.schedule(handler, watch_dir, recursive=False) | |
observer.start() | |
try: | |
while True: | |
time.sleep(1) | |
except KeyboardInterrupt: | |
print("\n停止监听...") | |
observer.stop() | |
observer.join() | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="远程文件监听上传器 - 自动监听 output 文件夹并上传新文件") | |
parser.add_argument("api_key", help="您的 API 密钥") | |
parser.add_argument("space_id", help="Space ID") | |
parser.add_argument("--server", default="http://127.0.0.1:5001", help="服务器的 URL 地址 (默认: http://127.0.0.1:5001)") | |
parser.add_argument("--watch-dir", default="output", help="要监听的目录 (默认: output)") | |
args = parser.parse_args() | |
# 开始监听目录 | |
monitor_directory(args.watch_dir, args.server, args.api_key, args.space_id) |