fsouza / fake-gcs-server

Google Cloud Storage emulator & testing library.
https://pkg.go.dev/github.com/fsouza/fake-gcs-server/fakestorage?tab=doc
BSD 2-Clause "Simplified" License
1.05k stars 209 forks source link

Support for preconditions #299

Open charleskorn opened 4 years ago

charleskorn commented 4 years ago

Does this server support preconditions? (https://cloud.google.com/storage/docs/generations-preconditions#_Preconditions)

I've tried adding the following to my upload code:

w := c.bucket.
    Object("myobject.json").
    If(cloudstorage.Conditions{DoesNotExist: true}).
    NewWriter(ctx)

But the server always overwrites the object, even if it exists, instead of returning a HTTP 412 response. I can see that the upload URL contains ifGenerationMatch=0 in the server's logs.

someone1 commented 4 years ago

Yikes, the v.1.20.0 release was a breaking one - all my tests started to fail. Glad to see some progress being made on this but maybe we can keep the original behavior of allowing the request through vs a 501 response?

someone1 commented 4 years ago

A quick follow-up: was the implementation in #300 the correct placement for such a feature? The generation feature seems backend dependent (e.g. the memory backend supports file generations whereas the file backend does not). Would it have made more sense to expand the Storage interface with a CreateObjectIfGenerationMatch or something to be used instead of CreateObject as needed, or expand CreateObject to take in a precondition struct to process? This way, each backend can properly implement the features it supports (with locking) vs the current approach.

Thoughts?

cc @fsouza

fsouza commented 4 years ago

Ugh, sorry about that :(( We should expand our shared tests to cover more behaviors. I'll make sure to keep an eye for that in the future (most of my use cases are using the memory backend :/).

I feel like extending the backend's CreateObject method is the best approach here, the same way we extended CreateBucket to pass versioningEnabled.

bmansoob commented 2 years ago

👋 @fsouza

I see generation support has been added in https://github.com/fsouza/fake-gcs-server/pull/735 at the same layer as in #300.

I added support for this in the memory backend here. I wanted to confirm with you first, before opening a pull request, that do you still plan on tackling this in a backend specific way.