From b89e415690514f3a2022a5ee473eeffab135581c Mon Sep 17 00:00:00 2001 From: Ambrose Chua Date: Sun, 23 May 2021 18:14:27 +0800 Subject: [PATCH] Implement random prefix --- handlers-s3.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/handlers-s3.go b/handlers-s3.go index 8e1ff56..b9e1b0d 100644 --- a/handlers-s3.go +++ b/handlers-s3.go @@ -7,10 +7,20 @@ import ( "net/http" "net/url" "strconv" + "strings" "github.com/gorilla/mux" + gonanoid "github.com/matoous/go-nanoid/v2" ) +func formatKey(prefix, filename string) string { + for strings.Contains(prefix, "{random}") { + random := gonanoid.MustGenerate(idAlphabet, 16) + prefix = strings.Replace(prefix, "{random}", random, 1) + } + return prefix + filename +} + /* createMultipartUpload */ type createMultipartUploadReq struct { @@ -59,7 +69,7 @@ func handleCreateMultipartUpload(w http.ResponseWriter, req *http.Request) { } // Derive the object key - key := cred.Prefix + r.Filename + key := formatKey(cred.Prefix, r.Filename) result, err := initiateMultipartUpload(key, cred) if err != nil {