|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""上传 qtcloud-write Flutter Web 构建产物到阿里云 OSS""" |
| 3 | + |
| 4 | +import oss2 |
| 5 | +import os |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +# 阿里云配置 |
| 9 | +ACCESS_KEY_ID = os.environ.get("OSS_ACCESS_KEY_ID", "") |
| 10 | +ACCESS_KEY_SECRET = os.environ.get("OSS_ACCESS_KEY_SECRET", "") |
| 11 | +BUCKET_NAME = "qtcloud-write" |
| 12 | +REGION = "cn-hangzhou" |
| 13 | +ENDPOINT = f"oss-{REGION}.aliyuncs.com" |
| 14 | + |
| 15 | +# 本地 build 目录 |
| 16 | +BUILD_DIR = Path(__file__).parent.parent / "src" / "studio" / "build" / "web" |
| 17 | + |
| 18 | + |
| 19 | +def upload_file(bucket, local_path: Path, remote_path: str): |
| 20 | + """上传单个文件""" |
| 21 | + with open(local_path, "rb") as f: |
| 22 | + content = f.read() |
| 23 | + |
| 24 | + result = bucket.put_object(remote_path, content) |
| 25 | + if result.status == 200: |
| 26 | + print(f" OK {remote_path}") |
| 27 | + else: |
| 28 | + print(f" FAIL {remote_path} (status={result.status})") |
| 29 | + return result.status == 200 |
| 30 | + |
| 31 | + |
| 32 | +def main(): |
| 33 | + print(f"Connecting OSS: {BUCKET_NAME} ({ENDPOINT})...") |
| 34 | + |
| 35 | + auth = oss2.Auth(ACCESS_KEY_ID, ACCESS_KEY_SECRET) |
| 36 | + bucket = oss2.Bucket(auth, ENDPOINT, BUCKET_NAME) |
| 37 | + |
| 38 | + if not BUILD_DIR.exists(): |
| 39 | + print(f"错误: build 目录不存在: {BUILD_DIR}") |
| 40 | + print("请先运行: cd src/studio && flutter build web --release") |
| 41 | + return |
| 42 | + |
| 43 | + print(f"Uploading from: {BUILD_DIR}") |
| 44 | + print("-" * 50) |
| 45 | + |
| 46 | + success = 0 |
| 47 | + failed = 0 |
| 48 | + |
| 49 | + for root, dirs, files in os.walk(BUILD_DIR): |
| 50 | + root_path = Path(root) |
| 51 | + |
| 52 | + for filename in files: |
| 53 | + local_path = root_path / filename |
| 54 | + relative = local_path.relative_to(BUILD_DIR) |
| 55 | + remote_path = str(relative).replace("\\", "/") |
| 56 | + |
| 57 | + if upload_file(bucket, local_path, remote_path): |
| 58 | + success += 1 |
| 59 | + else: |
| 60 | + failed += 1 |
| 61 | + |
| 62 | + print("-" * 50) |
| 63 | + print(f"完成: {success} 成功, {failed} 失败") |
| 64 | + print(f"访问: https://{BUCKET_NAME}.{ENDPOINT}") |
| 65 | + |
| 66 | + |
| 67 | +if __name__ == "__main__": |
| 68 | + main() |
0 commit comments