Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Can GCS "PATCH" method completely replace metadata in one request

According to the documentation here:

https://cloud.google.com/storage/docs/json_api#patch

setting:

"metadata": null

will clear the metadata, whereas setting:

{
"metadata": {"NEW_KEY" : "NEW_VALUE"}
}

will actually add to the metadata, like so:

"metadata": {
"EXISTING_KEY" : "EXISTING_VALUE",
"NEW_KEY" : "NEW_VALUE"
}

Lets say I didn't want to clear the metadata, nor add key/value pairs, but instead replace the metadata field with a new set of key/value pairs. For example, lets say my current metadata was:

"metadata": {
  "EXISTING_KEY" : "EXISTING_VALUE"
}

and I sent a PATCH request like so:

PATCH ...

"metadata": {
  "KEY1" : "VALUE1",
  "KEY2" : "VALUE2",
  "NO_OTHER_KEYS" : "NO_OTHER_VALUES"
},


that should result in the metadata being:

"metadata": {
  "KEY1" : "VALUE1",
  "KEY2" : "VALUE2",
  "NO_OTHER_KEYS" : "NO_OTHER_VALUES"
},

not:

"metadata": {
  "KEY1" : "VALUE1",
  "KEY2" : "VALUE2",
  "NO_OTHER_KEYS" : "NO_OTHER_VALUES",
  "EXISTING_KEY" : "EXISTING_VALUE"
},

i.e. the old keys are gone.

Now I could achieve this in two PATCH requests, first sending:

"metadata": null

and then:

"metadata": {
  "KEY1" : "VALUE1",
  "KEY2" : "VALUE2",
  "NO_OTHER_KEYS" : "NO_OTHER_VALUES"
},

would presumably result in all the old keys being removed, but could I achieve this in one patch request?

1 1 93
1 REPLY 1

Greetings @clinton987,

Welcome to the Google Cloud Community!

According to the Semantics of a Patch Request, the patch requests include only the resource fields you want to modify.

  • Add: To add a field that doesn't already exist. Here you can specify the new field and its value.
  • Modify: To change the value of an existing field. Here you can specify the field and set the new value
  • Delete: To delete a field, you can specify the field and set it to null. Setting "metadata": null will delete the metadata

What you were saying that:

"metadata": {
  "EXISTING_KEY" : "EXISTING_VALUE"
}
PATCH ...

"metadata": {
  "KEY1" : "VALUE1",
  "KEY2" : "VALUE2",
  "NO_OTHER_KEYS" : "NO_OTHER_VALUES"
},

 results:

"metadata": {
  "KEY1" : "VALUE1",
  "KEY2" : "VALUE2",
  "NO_OTHER_KEYS" : "NO_OTHER_VALUES"
},

not:

"metadata": {
  "KEY1" : "VALUE1",
  "KEY2" : "VALUE2",
  "NO_OTHER_KEYS" : "NO_OTHER_VALUES",
  "EXISTING_KEY" : "EXISTING_VALUE"
},

It is impossible because, PATCH doesn't have the ability to update an entire object based on the request. Rather, it follows what it can add, modify, or delete. That is why you have to do "metadata": null first so that you can totally update an entire metadata.

You can also contact Google Cloud Support to further look into your case. Let me know if it helped, thanks!