2017-05-08 23:07:07 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
2018-01-02 14:37:37 +01:00
|
|
|
"github.com/gorilla/handlers"
|
2017-05-08 23:07:07 +02:00
|
|
|
"github.com/gorilla/mux"
|
2017-08-26 10:49:41 +02:00
|
|
|
"github.com/mastertinner/s3manager"
|
2017-05-08 23:07:07 +02:00
|
|
|
minio "github.com/minio/minio-go"
|
2017-05-25 18:33:44 +02:00
|
|
|
"github.com/pkg/errors"
|
2017-05-08 23:07:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var (
|
|
|
|
port = flag.String("port", "8080", "the port the app should listen on")
|
|
|
|
endpoint = flag.String("endpoint", "s3.amazonaws.com", "the s3 endpoint to use")
|
|
|
|
accessKeyID = flag.String("access-key-id", "", "your s3 access key ID")
|
|
|
|
secretAccessKey = flag.String("secret-access-key", "", "your s3 secret access key")
|
|
|
|
v2Signing = flag.Bool("v2-signing", false, "set this flag if your S3 provider still uses V2 signing")
|
|
|
|
)
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *accessKeyID == "" || *secretAccessKey == "" {
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up S3 client
|
|
|
|
var s3 *minio.Client
|
|
|
|
var err error
|
|
|
|
if *v2Signing {
|
|
|
|
s3, err = minio.NewV2(*endpoint, *accessKeyID, *secretAccessKey, true)
|
|
|
|
} else {
|
|
|
|
s3, err = minio.New(*endpoint, *accessKeyID, *secretAccessKey, true)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2017-05-25 18:33:44 +02:00
|
|
|
log.Fatalln(errors.Wrap(err, "error creating s3 client"))
|
2017-05-08 23:07:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set up router
|
|
|
|
r := mux.NewRouter().StrictSlash(true)
|
|
|
|
r.
|
|
|
|
Methods(http.MethodGet).
|
|
|
|
Path("/").
|
2017-09-19 19:00:57 +02:00
|
|
|
Handler(http.RedirectHandler("/buckets", http.StatusPermanentRedirect))
|
2017-05-08 23:07:07 +02:00
|
|
|
r.
|
|
|
|
Methods(http.MethodGet).
|
|
|
|
Path("/buckets").
|
2017-09-19 19:00:57 +02:00
|
|
|
Handler(s3manager.BucketsViewHandler(s3))
|
2017-05-08 23:07:07 +02:00
|
|
|
r.
|
|
|
|
Methods(http.MethodGet).
|
|
|
|
Path("/buckets/{bucketName}").
|
2017-09-19 19:00:57 +02:00
|
|
|
Handler(s3manager.BucketViewHandler(s3))
|
2017-05-08 23:07:07 +02:00
|
|
|
r.
|
|
|
|
Methods(http.MethodPost).
|
|
|
|
Path("/api/buckets").
|
2017-09-19 19:00:57 +02:00
|
|
|
Handler(s3manager.CreateBucketHandler(s3))
|
2017-05-08 23:07:07 +02:00
|
|
|
r.
|
|
|
|
Methods(http.MethodDelete).
|
|
|
|
Path("/api/buckets/{bucketName}").
|
2017-09-19 19:00:57 +02:00
|
|
|
Handler(s3manager.DeleteBucketHandler(s3))
|
2017-05-08 23:07:07 +02:00
|
|
|
r.
|
|
|
|
Methods(http.MethodPost).
|
2017-08-26 10:49:41 +02:00
|
|
|
Headers(s3manager.HeaderContentType, s3manager.ContentTypeJSON).
|
2017-05-08 23:07:07 +02:00
|
|
|
Path("/api/buckets/{bucketName}/objects").
|
2017-09-19 19:00:57 +02:00
|
|
|
Handler(s3manager.CopyObjectHandler(s3))
|
2017-05-08 23:07:07 +02:00
|
|
|
r.
|
|
|
|
Methods(http.MethodPost).
|
2017-08-26 10:49:41 +02:00
|
|
|
HeadersRegexp(s3manager.HeaderContentType, s3manager.ContentTypeMultipartForm).
|
2017-05-08 23:07:07 +02:00
|
|
|
Path("/api/buckets/{bucketName}/objects").
|
2017-09-19 19:00:57 +02:00
|
|
|
Handler(s3manager.CreateObjectHandler(s3))
|
2017-05-08 23:07:07 +02:00
|
|
|
r.
|
|
|
|
Methods(http.MethodGet).
|
|
|
|
Path("/api/buckets/{bucketName}/objects/{objectName}").
|
2017-09-19 19:00:57 +02:00
|
|
|
Handler(s3manager.GetObjectHandler(s3))
|
2017-05-08 23:07:07 +02:00
|
|
|
r.
|
|
|
|
Methods(http.MethodDelete).
|
|
|
|
Path("/api/buckets/{bucketName}/objects/{objectName}").
|
2017-09-19 19:00:57 +02:00
|
|
|
Handler(s3manager.DeleteObjectHandler(s3))
|
2017-05-08 23:07:07 +02:00
|
|
|
|
2018-01-02 14:37:37 +01:00
|
|
|
log.Fatal(http.ListenAndServe(":"+*port, handlers.LoggingHandler(os.Stdout, r)))
|
2017-05-08 23:07:07 +02:00
|
|
|
}
|