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