proxy: redirect 302 на pre-signed S3 URL вместо стриминга (v0.0.3)

This commit is contained in:
“Naeel”
2026-09-01 16:42:46 +03:00
parent cf2789be76
commit 422736f5fa
2 changed files with 4 additions and 31 deletions
+1 -1
View File
@@ -24,7 +24,7 @@ var (
s3Prefix = os.Getenv("S3_PREFIX") s3Prefix = os.Getenv("S3_PREFIX")
) )
const VERSION = "0.0.2" const VERSION = "0.0.3"
func main() { func main() {
if hostname == "" { if hostname == "" {
+3 -30
View File
@@ -1,15 +1,9 @@
package main package main
import ( import (
"context"
"fmt"
"io"
"log"
"net/http" "net/http"
"strings" "strings"
"time" "time"
s3 "github.com/minio/minio-go/v7"
) )
func proxyHandler(w http.ResponseWriter, r *http.Request) { func proxyHandler(w http.ResponseWriter, r *http.Request) {
@@ -24,32 +18,11 @@ func proxyHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
ctx, cancel := context.WithTimeout(r.Context(), 60*time.Second) presigned, err := s3Client.PresignedGetObject(r.Context(), bucketName, key, 15*time.Minute, nil)
defer cancel()
obj, err := s3Client.GetObject(ctx, bucketName, key, s3.GetObjectOptions{})
if err != nil { if err != nil {
log.Printf("Error getting object %s/%s: %v", bucketName, key, err) http.Error(w, "Failed to sign object", http.StatusInternalServerError)
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 return
} }
w.Header().Set("Content-Length", fmt.Sprintf("%d", stat.Size)) http.Redirect(w, r, presigned.String(), http.StatusFound)
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)
}
} }