简体   繁体   中英

Google Cloud Storage - ListObjects, folder are not shown.

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM