Google-cloud-go: storage: GenerationMatch = 0 produces error

Created on 6 Dec 2018  路  4Comments  路  Source: googleapis/google-cloud-go

The google storage documentation suggests:

If you set a generation-match precondition to 0 when uploading an object, Cloud Storage performs the specified request only if there is no live version of the object.

When using the "cloud.google.com/go/storage" package in go, I expect that writing to a new object with the GenerationMatch condition to set 0 will succeed and preventing us from overwriting any existing objects in the bucket.

Instead, I'm getting a storage: NewWriter: empty conditions error when the GenerationMatch condition to 0.

client, _ := storage.NewClient(ctx)
w := client.
    Bucket("mybucket").
    Object("mykey").
    If(storage.Conditions{GenerationMatch: 0}).
    NewWriter(ctx)

io.Copy(w, src)
err := w.Close()
// returns error "storage: NewWriter: empty conditions"

Can you guys find a way to interpret the 0 value as an intentional value, rather than a missing zero value? Thanks

storage feature request

Most helpful comment

Thanks. I missed the 'DoesNotExist' field in the docs, and that's what we need.

All 4 comments

Unfortunately, since the type is int64 and not *int64, there's no way to distinguish between unset and 0. We document that a value of 0 has no effect at https://godoc.org/cloud.google.com/go/storage#Conditions.

@frankyn Do any other languages run into this problem? Is there some other convention besides setting the value to zero?

Thanks. I missed the 'DoesNotExist' field in the docs, and that's what we need.

I learned something new thanks @eriklott for updating the issue!

Ah, I also missed that field. Doh. Thanks for updating the issue. :)

Was this page helpful?
0 / 5 - 0 ratings