问题
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