diff --git a/README.md b/README.md index 7038f91..3238471 100644 --- a/README.md +++ b/README.md @@ -32,17 +32,23 @@ Usage of gcsproxy: The path to the keyfile. If not present, client will use your default application credentials. -i string The default index file to serve. + -s string + The source bucket name. If not present, bucket name will be extracted from the path. -v Show access log ``` -The gcsproxy routing configuration is shown below. +The default gcsproxy routing configuration is shown below. -`"/{bucket:[0-9a-zA-Z-_.] +}/{object:. *}"` +`"/{bucket:[0-9a-zA-Z-_.]+}/{object:.*}"` If you are running gcsproxy on localhost:8080 and you want to access the file `gs://test-bucket/your/file/path.txt` in GCS via gcsproxy, you can use the URL You can access the file via gcsproxy at the URL `http://localhost:8080/test-bucket/your/file/path.txt`. +Specifying the `-s` option fixes the bucket name to the given value and following routing configuration is used instead. + +`"/{object:.*}"` + If a default index file is specified and the target object does not exist, an attempt is made to retrieve the object specified in the default index file. ``` diff --git a/main.go b/main.go index 489032a..f8f4ee2 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,7 @@ var ( verbose = flag.Bool("v", false, "Show access log") credentials = flag.String("c", "", "The path to the keyfile. If not present, client will use your default application credentials.") defaultIndex = flag.String("i", "", "The default index file to serve.") + sourceBucket = flag.String("s", "", "The source bucket name. If not present, bucket name will be extracted from the path.") ) var client *storage.Client @@ -179,7 +180,16 @@ func main() { r := mux.NewRouter() r.HandleFunc("/_health", wrapper(healthCheck)).Methods("GET", "HEAD") - r.HandleFunc("/{bucket:[0-9a-zA-Z-_.]+}/{object:.*}", wrapper(proxy)).Methods("GET", "HEAD") + if *sourceBucket != "" { + w := func(writer http.ResponseWriter, request *http.Request) { + params := mux.Vars(request) + params["bucket"] = *sourceBucket + wrapper(proxy)(writer, request) + } + r.HandleFunc("/{object:.*}", w).Methods("GET", "HEAD") + } else { + r.HandleFunc("/{bucket:[0-9a-zA-Z-_.]+}/{object:.*}", wrapper(proxy)).Methods("GET", "HEAD") + } log.Printf("[service] listening on %s", *bind) if err := http.ListenAndServe(*bind, r); err != nil {