Skip to content

Commit

Permalink
Add HEAD support for storage API object endpoint (#759)
Browse files Browse the repository at this point in the history
* Support HEAD method on storage API

Problem: As reporeted in #667. The storage API is replying 404 for
existing objects when method HEAD is used, although GET replies with
200.

Solution: Update storage API method route and handler to behave as
"donwload" when HEAD method is used.

* Update documentation regarding flag usage

Problem: As a new user, one tends to simply use what comes in the
README.md file without realizing there is plenty more features at hand.

Solution: Update README.md to pointing out there is a -help flag that
lists all the available configuration overrides. I was tempted to copy
and paste its output, but it might add too much noise for the reader
so disregarded that thought.
  • Loading branch information
jossemarGT authored May 10, 2022
1 parent ae04303 commit 6c0ba10
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ It is possible to use fake-gcs-server with signed URLs, although with a few cave
- You need to configure fake-gcs-server to accept this local URL (by setting
`-public-host`)

### Available server flags

fake-gcs-server supports various features that can be configured through flags
upon start. Use the `-help` flag to list all of them alongside their usage
instructions

```shell
docker run --rm fsouza/fake-gcs-server -help
```

## Client library examples

For examples using SDK from multiple languages, check out the
Expand Down
2 changes: 1 addition & 1 deletion fakestorage/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ func (s *Server) listObjects(r *http.Request) jsonResponse {
}

func (s *Server) getObject(w http.ResponseWriter, r *http.Request) {
if alt := r.URL.Query().Get("alt"); alt == "media" {
if alt := r.URL.Query().Get("alt"); alt == "media" || r.Method == http.MethodHead {
s.downloadObject(w, r)
return
}
Expand Down
2 changes: 1 addition & 1 deletion fakestorage/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (s *Server) buildMuxer() {
r.Path("/b/{bucketName}/o/{objectName:.+}/acl").Methods(http.MethodGet).HandlerFunc(jsonToHTTPHandler(s.listObjectACL))
r.Path("/b/{bucketName}/o/{objectName:.+}/acl").Methods(http.MethodPost).HandlerFunc(jsonToHTTPHandler(s.setObjectACL))
r.Path("/b/{bucketName}/o/{objectName:.+}/acl/{entity}").Methods(http.MethodPut).HandlerFunc(jsonToHTTPHandler(s.setObjectACL))
r.Path("/b/{bucketName}/o/{objectName:.+}").Methods(http.MethodGet).HandlerFunc(s.getObject)
r.Path("/b/{bucketName}/o/{objectName:.+}").Methods(http.MethodGet, http.MethodHead).HandlerFunc(s.getObject)
r.Path("/b/{bucketName}/o/{objectName:.+}").Methods(http.MethodDelete).HandlerFunc(jsonToHTTPHandler(s.deleteObject))
r.Path("/b/{sourceBucket}/o/{sourceObject:.+}/copyTo/b/{destinationBucket}/o/{destinationObject:.+}").Methods(http.MethodPost).HandlerFunc(jsonToHTTPHandler(s.rewriteObject))
r.Path("/b/{sourceBucket}/o/{sourceObject:.+}/rewriteTo/b/{destinationBucket}/o/{destinationObject:.+}").Methods(http.MethodPost).HandlerFunc(jsonToHTTPHandler(s.rewriteObject))
Expand Down
7 changes: 7 additions & 0 deletions fakestorage/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,13 @@ func testDownloadObject(t *testing.T, server *Server) {
map[string]string{"accept-ranges": "bytes", "content-length": "9"},
"something",
},
{
"HEAD: using storage api",
http.MethodHead,
"://storage.googleapis.com/storage/v1/b/some-bucket/o/files/txt/text-01.txt",
map[string]string{"accept-ranges": "bytes", "content-length": "9"},
"",
},
{
"HEAD: bucket in the path",
http.MethodHead,
Expand Down

0 comments on commit 6c0ba10

Please sign in to comment.