简体   繁体   中英

Get only objects inside “folder” in Google Cloud Storage C#

I have a small sample code that is supposed to return only objects inside "folder" in Google Cloud Storage:

            var storage = StorageClient.Create();
            var listObjectOptions = new ListObjectsOptions(){ Delimiter = ""};
            try
            {
                foreach (var storageObject in storage.ListObjects(bucketName, "firstSubFolder/secondSubFolder/", listObjectOptions))
                {
                    Console.WriteLine(storageObject.Name);
                }
            }
            catch (Exception e)
            {
                //
            }

What this code does, it returns not only objects inside secondSubFolder but it also returns the folder itself :"firstSubFolder/secondSubFolder/". I have tried many combinations using delimiter and prefixes, but can't really get it to return only objects from folder. Am I missing something or is this the way how it normally works?

Edit:

There are 2 workarounds:

  1. List the objects in a for with an index instead of a foreach and start from i=1 as the folder will always be the first object listed

  2. Check if storageObject.Name is equal to firstSubFolder/secondSubFolder/ and discard it from the output.

So it turns out that you can't use wildcards from a Client library, but only with gsutil .


Original Answer:

Try this "firstSubFolder/secondSubFolder/?*"

The '?' wildcard character means "match exactly one character" and '*' matches everything after it.

I tried it with gsutil not with C# but it should work.

See Wildcard Names for more information.

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