Google Storage: Deleting a folder if it is empty

左心房为你撑大大i 提交于 2019-12-11 07:15:23

问题


There is a folder in a Google Storage bucket.

I need to delete (with JSON API) the folder (let denote it xxx/) if and only if it is empty.

The most obvious way is to check if it is empty and delete if it is empty. This requires an additional request and also may be subject to race conditions.

However, what about deleting the xxx/ object unconditionally? If it is empty deleting will do what we need, and if it is nonempty xxx/ will continue to behave like a folder because there are files with prefix xxx/.

Are there any troubles with this way (that is with removing the xxx/ object unconditionally on whether it has or doesn't have subobjects)?


回答1:


Deleting the xxx/ object will not delete any objects "inside" the folder.

Note that that object is created by tools (like the Cloud Storage web console) to support the illusion of a folder inside what's actually a flat namespace in GCS (i.e., to the service there are no folders, and the "/" character is used by tools to mimic there being folders inside the bucket).

The only way to do what you want is to get a list of the objects under the xxx/ prefix and not delete the xxx/ object if that list is non-empty. However, doing that has an inherent race condition, since someone could write a new object after you make the check.



来源:https://stackoverflow.com/questions/46475656/google-storage-deleting-a-folder-if-it-is-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!