package handler import ( "encoding/json" "net/http" "os" "github.com/cloudflare/cloudflare-go" ) type R2UsageResponse struct { StorageUsed uint64 `json:"storage_used"` Requests uint64 `json:"requests"` LastUpdated string `json:"last_updated"` } func GetR2Usage(w http.ResponseWriter, r *http.Request) { // 初始化Cloudflare客户端 api, err := cloudflare.NewWithAPIToken(os.Getenv("CLOUDFLARE_API_TOKEN")) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } // 获取账户ID accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID") // 查询R2使用量 usage, err := api.R2Usage(accountID) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } // 返回响应 response := R2UsageResponse{ StorageUsed: usage.StorageUsed, Requests: usage.Requests, LastUpdated: usage.LastUpdated, } w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(response) }