#!/usr/bin/env python3 """上传日志到自己S3空间(华为OBS官方SDK): user//log//.json""" import json, os, sys, datetime WORK = os.path.expanduser("~/.fmode-harness-agent") def upload(): ident_path = "/opt/data/fmode-identity.json" if not os.path.exists(ident_path): ident_path = os.path.join(WORK, "fmode-identity.json") ident = json.load(open(ident_path)) userid = ident["userid"] log_file = os.path.join(WORK, "tmp", "agent-log.json") if not os.path.exists(log_file): print("先跑 collect_metrics.py"); sys.exit(1) ak = os.environ.get("S3_AK") or os.environ.get("CLOUD_SDK_AK") sk = os.environ.get("S3_SK") or os.environ.get("CLOUD_SDK_SK") if not ak: print("缺少S3凭证"); sys.exit(1) from obs import ObsClient obs = ObsClient(access_key_id=ak, secret_access_key=sk, server="https://obs.cn-north-4.myhuaweicloud.com") now = datetime.datetime.now() key = f"user/{userid}/log/{now:%Y%m%d}/{now:%H%M}.json" r = obs.putObject("storage-s3-nkkj", key, content=open(log_file, "rb").read()) if r.status < 300: print(f"uploaded: {key} (status {r.status})") else: print(f"upload FAILED: {r.status} {r.reason}"); sys.exit(1) # 清理30天前旧日志 cutoff = (now - datetime.timedelta(days=30)).strftime("%Y%m%d") resp = obs.listObjects("storage-s3-nkkj", prefix=f"user/{userid}/log/") cleaned = 0 if resp.status < 300 and resp.body.contents: for obj in resp.body.contents: day = obj.key.split("/")[3] if day < cutoff: obs.deleteObject("storage-s3-nkkj", obj.key) cleaned += 1 if cleaned: print(f"cleaned {cleaned} old logs") if __name__ == "__main__": upload()