简体   繁体   English

仅获取Google Cloud Storage C#中“文件夹”内的对象

[英]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: 我有一个小的示例代码,应该仅返回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/". 此代码的作用是,它不仅返回secondSubFolder内部的对象,而且还返回文件夹本身:“ 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 用索引而不是foreach列出for中的对象,并从i = 1开始,因为该文件夹将始终是列出的第一个对象

  2. Check if storageObject.Name is equal to firstSubFolder/secondSubFolder/ and discard it from the output. 检查storageObject.Name是否等于firstSubFolder/secondSubFolder/并将其从输出中丢弃。

So it turns out that you can't use wildcards from a Client library, but only with gsutil . 因此,事实证明您不能使用Client库中的通配符,而只能使用gsutil


Original Answer: 原始答案:

Try this "firstSubFolder/secondSubFolder/?*" 试试这个"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. 我使用gsutil而不是C#进行了尝试,但它应该可以工作。

See Wildcard Names for more information. 有关更多信息,请参见通配符名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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