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 {