From d9258d492441c25315c5ea06b4202e8bb0a45b9f Mon Sep 17 00:00:00 2001 From: DullJZ <79080562+DullJZ@users.noreply.github.com> Date: Sat, 31 Jan 2026 20:20:48 +0800 Subject: [PATCH] fix: Add CORS Option --- VERSION | 2 +- internal/api/s3_handler.go | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/VERSION b/VERSION index b78946b..3512a85 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v0.2.3 \ No newline at end of file +v0.2.4 \ No newline at end of file diff --git a/internal/api/s3_handler.go b/internal/api/s3_handler.go index b12ea4a..df40f28 100644 --- a/internal/api/s3_handler.go +++ b/internal/api/s3_handler.go @@ -1,6 +1,7 @@ package api import ( + "net/http" "sync/atomic" "github.com/DullJZ/s3-balance/internal/balancer" @@ -73,6 +74,7 @@ func (h *S3Handler) initSettings(accessKey, secretKey string, proxyMode, authReq func (h *S3Handler) RegisterS3Routes(router *mux.Router) { // 公共路由(不需要认证) router.HandleFunc("/", h.handleListBuckets).Methods("GET") + router.HandleFunc("/", h.handleOptions).Methods("OPTIONS") // 带认证/虚拟主机的路由 protected := router.NewRoute().PathPrefix("/{bucket}").Subrouter() @@ -80,7 +82,9 @@ func (h *S3Handler) RegisterS3Routes(router *mux.Router) { // Bucket operations protected.HandleFunc("", h.handleBucketOperations).Methods("GET", "HEAD", "PUT", "DELETE") + protected.HandleFunc("", h.handleOptions).Methods("OPTIONS") protected.HandleFunc("/", h.handleBucketOperations).Methods("GET", "HEAD", "PUT", "DELETE") + protected.HandleFunc("/", h.handleOptions).Methods("OPTIONS") // Multipart upload operations - must be registered before generic object operations protected.HandleFunc("/{key:.*}", h.handleUploadPart).Methods("PUT").Queries("partNumber", "{partNumber:[0-9]+}", "uploadId", "{uploadId}") @@ -92,6 +96,7 @@ func (h *S3Handler) RegisterS3Routes(router *mux.Router) { // Object operations - must be registered after multipart operations to avoid conflicts protected.HandleFunc("/{key:.*}", h.handleObjectOperations).Methods("GET", "HEAD", "PUT", "DELETE") + protected.HandleFunc("/{key:.*}", h.handleOptions).Methods("OPTIONS") // 添加中间件 protected.Use(h.accessLogMiddleware) @@ -110,6 +115,10 @@ func (h *S3Handler) RegisterS3Routes(router *mux.Router) { })) } +func (h *S3Handler) handleOptions(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) +} + func (h *S3Handler) loadSettings() handlerSettings { if v := h.settings.Load(); v != nil { return v.(handlerSettings)