package main import ( "context" "fmt" "io" "log" "net/http" "strings" "time" s3 "github.com/minio/minio-go/v7" ) func proxyHandler(w http.ResponseWriter, r *http.Request) { key := r.URL.Query().Get("key") if key == "" { http.Error(w, "Missing key param", http.StatusBadRequest) return } if !strings.HasPrefix(key, s3Prefix+"/") && !strings.HasPrefix(key, "docs/") { http.Error(w, "Forbidden", http.StatusForbidden) return } ctx, cancel := context.WithTimeout(r.Context(), 60*time.Second) defer cancel() obj, err := s3Client.GetObject(ctx, bucketName, key, s3.GetObjectOptions{}) if err != nil { log.Printf("Error getting object %s/%s: %v", bucketName, key, err) http.Error(w, "File not found", http.StatusNotFound) return } defer obj.Close() stat, err := obj.Stat() if err != nil { log.Printf("Error stating object %s/%s: %v", bucketName, key, err) http.Error(w, "File not found or not accessible", http.StatusNotFound) return } w.Header().Set("Content-Length", fmt.Sprintf("%d", stat.Size)) w.Header().Set("Content-Type", "application/octet-stream") w.Header().Set("Last-Modified", stat.LastModified.Format(http.TimeFormat)) written, err := io.Copy(w, obj) if err != nil { log.Printf("Error streaming object: %v (sent %d/%d bytes)", err, written, stat.Size) } else { log.Printf("Proxy: sent %d bytes for %s", written, key) } }