Google存储空间:如果文件夹为空,则将其删除 [英] Google Storage: Deleting a folder if it is empty

查看:183
本文介绍了Google存储空间:如果文件夹为空,则将其删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google存储桶中有一个文件夹。

There is a folder in a Google Storage bucket.

我需要使用JSON API删除该文件夹(用 xxx来表示它) / ),并且仅当它为空时。

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.

但是,删除 xxx / 怎么样?无条件反对?如果为空,则删除将满足我们的需要;如果为非空,则 xxx / 将继续像文件夹一样工作,因为有些文件的前缀为 xxx /

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/.

这种方式是否有麻烦(即删除 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)?

推荐答案

删除xxx /对象不会删除

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

请注意,该对象是由工具(例如Cloud Storage Web控制台)创建的,目的是支持文件夹内的错觉GCS中的平面命名空间(即,服务中没有文件夹,工具使用 /字符来模拟存储桶中有文件夹)。

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).

进行所需操作的唯一方法是获取xxx /前缀下的对象列表,并且如果该列表不是空的,则不删除xxx /对象。但是,这样做有一个固有的竞争条件,因为有人可以在进行检查后写一个新对象。

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.

这篇关于Google存储空间:如果文件夹为空,则将其删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆