proxy: redirect 302 на pre-signed S3 URL вместо стриминга (v0.0.3)
This commit is contained in:
+1
-1
@@ -24,7 +24,7 @@ var (
|
||||
s3Prefix = os.Getenv("S3_PREFIX")
|
||||
)
|
||||
|
||||
const VERSION = "0.0.2"
|
||||
const VERSION = "0.0.3"
|
||||
|
||||
func main() {
|
||||
if hostname == "" {
|
||||
|
||||
+3
-30
@@ -1,15 +1,9 @@
|
||||
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) {
|
||||
@@ -24,32 +18,11 @@ func proxyHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 60*time.Second)
|
||||
defer cancel()
|
||||
|
||||
obj, err := s3Client.GetObject(ctx, bucketName, key, s3.GetObjectOptions{})
|
||||
presigned, err := s3Client.PresignedGetObject(r.Context(), bucketName, key, 15*time.Minute, nil)
|
||||
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)
|
||||
http.Error(w, "Failed to sign object", http.StatusInternalServerError)
|
||||
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)
|
||||
}
|
||||
http.Redirect(w, r, presigned.String(), http.StatusFound)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user