1
0
Fork 0
photos/cmd/control/control.go

67 lines
2.2 KiB
Go

package main
import (
"fmt"
"net/http"
"net/url"
"os"
"time"
"github.com/minio/minio-go/v6/pkg/s3utils"
"github.com/minio/minio-go/v6/pkg/signer"
)
var accessKey string
var secretKey string
func main() {
// Read configuration
accessKey = os.Getenv("MINIO_ACCESS_KEY")
secretKey = os.Getenv("MINIO_SECRET_KEY")
http.HandleFunc("/list", hello)
http.HandleFunc("/write", hello2)
http.ListenAndServe(":8090", nil)
}
func hello(w http.ResponseWriter, req *http.Request) {
// Based upon https://github.com/minio/minio-go/blob/337bb00bc3c832292b36681c7bde1b56a185c310/api.go#L873
expires := 60 * time.Second
reqValues := make(url.Values)
reqValues.Set("list-type", "2")
reqValues.Set("metadata", "true")
reqValues.Set("encoding-type", "url")
reqValues.Set("prefix", "")
reqValues.Set("delimiter", "")
reqValues.Set("max-keys", fmt.Sprintf("%d", 500))
reqValues.Set("start-after", "")
// Change path accordingly https://github.com/minio/minio-go/blob/337bb00bc3c832292b36681c7bde1b56a185c310/api.go#L911
// Request URL MUST not have a port
reqString := "https://rock.six.six.six.six.six.eurica.eu.org/?" + s3utils.QueryEncode(reqValues)
req, err := http.NewRequest("GET", reqString, nil)
if err != nil {
panic(err)
}
signedReq := signer.PreSignV4(*req, accessKey, secretKey, "", "sgp1", int64(expires/time.Second))
w.Header().Add("Location", signedReq.URL.String())
w.WriteHeader(http.StatusTemporaryRedirect)
}
func hello2(w http.ResponseWriter, req *http.Request) {
// Based upon https://github.com/minio/minio-go/blob/337bb00bc3c832292b36681c7bde1b56a185c310/api.go#L873
expires := 60 * time.Second
reqValues := make(url.Values)
// Change path accordingly https://github.com/minio/minio-go/blob/337bb00bc3c832292b36681c7bde1b56a185c310/api.go#L911
// Request URL MUST not have a port
reqString := "https://rock.six.six.six.six.six.eurica.eu.org/rock.txt?" + s3utils.QueryEncode(reqValues)
req, err := http.NewRequest("PUT", reqString, nil)
if err != nil {
panic(err)
}
signedReq := signer.PreSignV4(*req, accessKey, secretKey, "", "sgp1", int64(expires/time.Second))
w.Header().Add("Location", signedReq.URL.String())
w.WriteHeader(http.StatusTemporaryRedirect)
}