0
votes

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?

1

1 Answers

1
votes

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.