Google Cloud Storage - ListObjects, folder are not shown.

帅比萌擦擦* 提交于 2020-06-27 23:11:03

问题


I am trying out Google Cloud Storage, and have a problem with its C# SDK. Specifically, I have created a bucket with folder a/, b/, c/ (with files in folder). When I use:

gsutil ls gs://<mybucket>/root/

The folders correctly show up as:

gs://<mybucket>/root/a
gs://<mybucket>/root/b
gs://<mybucket>/root/c

However, when I use C# SDK to list the folder,

var client = StorageClient.Create();
var opt = new ListObjectsOptions() { Delimiter = "/" };
var ret = client.ListObjects("<mybucket>", "root/", opt);
var lst = new List<Google.Apis.Storage.v1.Data.Object>(); 
foreach (var item in ret )
{
    lst.Add(item);
}

The resultant list is empty (no folder returned). Note if I change the code above to:

var opt = new ListObjectsOptions();

All files in the folder can be successfully listed. What is wrong? Can ListObjects with Delimiter options list the folder in the storage bucket?


回答1:


In API reference Delimiter is described as following:

Used to list in "directory mode". Only objects whose names (aside from the prefix) do not contain delimiter will be returned.

Basically you have to set Delimiter to empty

var opt = new ListObjectsOptions() { Delimiter = "" };

Otherwise it ignores every folder in your bucket. Just set Delimiter to empty and the rest of your code will work.



来源:https://stackoverflow.com/questions/48594558/google-cloud-storage-listobjects-folder-are-not-shown

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