#!/bin/bash set -e if [ $# -lt 1 ]; then echo "❌ 用法: $0 <本地文件路径>" exit 1 fi file="$1" bucket="plugai" host="https://s3.szaiai.com" endpoint="https://api.szaiai.com" access_key="admin" secret_key="Admin@123.." alias="autoalias" if [ ! -f "$file" ]; then echo "❌ 文件不存在: $file" exit 1 fi mc alias rm $alias 2>/dev/null || true mc alias set $alias $endpoint $access_key $secret_key > /dev/null mc mb --ignore-existing "$alias/$bucket" > /dev/null filename=$(basename "$file") filesize=$(stat -c %s "$file") limit_bytes=1048576000 # 1000MB # 判断读取长度 if [ "$filesize" -le "$limit_bytes" ]; then read_size=$filesize echo "📦 文件小于等于 1GB,使用全文件做 hash 校验" local_hash=$(sha256sum "$file" | awk '{print $1}') else read_size=$limit_bytes echo "📦 文件大于 1GB,仅使用前 1000MB 做 hash 校验" local_hash=$(head -c "$read_size" "$file" | sha256sum | awk '{print $1}') fi echo "🚀 开始上传文件: $file" mc cp "$file" "$alias/$bucket/$filename" echo "✅ 上传完成,开始验证远程数据..." remote_hash=$(curl -s -H "Range: bytes=0-$((read_size - 1))" "$host/$bucket/$filename" | sha256sum | awk '{print $1}') if [ "$local_hash" != "$remote_hash" ]; then echo "❌ 验证失败:远程文件未被覆盖或延迟更新" echo "本地 hash: $local_hash" echo "远程 hash: $remote_hash" exit 1 fi encoded=$(python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$filename") echo "🎉 文件上传并验证成功:$host/$bucket/$encoded?ts=$(date +%s)"