简体   繁体   English

列出存储桶目录中的对象

[英]Listing objects inside the directory of bucket

Several days ago, I was working with Google Cloud Storage listing and downloading objects inside buckets but now I am struggling to read only objects inside the specific directory because I want only iterate over the objects inside this directory.几天前,我正在使用 Google Cloud Storage 列出和下载存储桶中的对象,但现在我正在努力只读取特定目录中的对象,因为我只想迭代此目录中的对象。 My bucket is "pubsite_prod_rev_12345" but I try to read-only elements inside "/earnings/":我的存储桶是“pubsite_prod_rev_12345”,但我尝试在“/earnings/”中使用只读元素:

 string bucketId = "pubsite_prod_rev_12345/earnings";

So now I turn to the help of experts in this matter, this is my code:所以现在我求助于这件事上的专家,这是我的代码:

 string[] scopes = new string[] { "https://www.googleapis.com/auth/devstorage.read_only" };
var credential = GoogleCredential.FromFile(@"C:\Data\Jorgesys\jorgesys-8aa7ab2343daa.json").CreateScoped(scopes);
            var storage = StorageClient.Create(credential);
      
            var bucket_name = "jorgesys.appspot.com";            
            //string bucketId = "pubsite_prod_rev_12345";

            //Trying to make a list only inside earnings directoy!
            string bucketId = "pubsite_prod_rev_12345/earnings";

            StringBuilder sb = new StringBuilder();
            try
            {
                var bucketObjects = storage.ListObjects(bucketId);
                foreach (var bucketObject in bucketObjects)
                {
                    sb.AppendLine("Object Name: " + bucketObject.Name);
                    sb.AppendLine("Id: " + bucketObject.Id);
                }
            }
            catch (Exception ex)
            {             
                sb.AppendLine("Exception: " + ex.Message);
            }

I don´t want to filter, just get the elements inside /earnings/ directory, how could achieve this task.我不想过滤,只是获取/earnings/目录中的元素,怎么能完成这个任务。

> miroslava/earnings_202102_2138916436889858-1.zip
> miroslava/earnings_202103_2138916436889858-2.zip
> miroslava/earnings_202104_2138916436889858-3.zip
> miroslava/earnings_202105_2138916436889858-4.zip
> earning/earnings_202102_2138916436889858-1.zip
> earnings/earnings_202103_2138916436889858-2.zip
> earnings/earnings_202104_2138916436889858-3.zip
> earnings/earnings_202105_2138916436889858-4.zip
> financial-stats/subscriptions/subscriptions_com.jorgesys_mensual1_201607_country.csv
> financial-stats/subscriptions/subscriptions_com.jorgesys_mensual1_201607_device.csv
> financial-stats/subscriptions/subscriptions_com.jorgesys_mensual1_201607_country.csv
> financial-stats/subscriptions/subscriptions_com.jorgesys_week1_201607_device.csv

Google Cloud Storage does not have directories. Google Cloud Storage 没有目录。 The directory portion of the filename is called a prefix .文件名的目录部分称为前缀

For example the object name: tmp/log.txt has the prefix tmp/ .例如 object 名称: tmp/log.txt具有前缀tmp/

The next concept is the delimiter .下一个概念是定界符 This is typically the / character.这通常是/字符。

To specify the directory path tmp use a prefix of tmp/ and a delimiter of / .要指定目录路径tmp ,请使用前缀tmp/和分隔符/ Any objects that start with tmp/ and do not contain the delimiter (excluding the prefix) are returned as names.任何以tmp/开头且不包含定界符(不包括前缀)的对象都作为名称返回。 Any objects that start with tmp/ and contain a delimiter are returned as prefixes.任何以tmp/开头并包含定界符的对象都将作为前缀返回。 Example tmp/subdirectory/123 would be returned as the prefix tmp/subdirectory/ with nothing additional as a name.示例tmp/subdirectory/123将作为前缀tmp/subdirectory/返回,没有任何额外的名称。

With that complicated explanation here is an example:有了这个复杂的解释,这里有一个例子:

Note: Your line with bucketId is not correct.注意:您的bucketId行不正确。 The bucket name does not include any part of the object name.存储桶名称不包含 object 名称的任何部分。

var bucketName = "pubsite_prod_rev_12345";

// Specify the delimter.
var delimeter = "/";

// Specify the prefix
var prefix = "earnings/";

var storage = StorageClient.Create();

var options = new ListObjectsOptions { Delimiter = delimiter };

var storageObjects = storage.ListObjects(bucketName, prefix, options);

foreach (var storageObject in storageObjects)
{
    sb.AppendLine("Object Name: " + storageObject.Name);
}

Class StorageClient Class 存储客户端

Class ListObjectOptions Class 列表对象选项

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

相关问题 使用 boto3 列出存储桶的内容 - Listing contents of a bucket with boto3 GCS:我们可以在一个桶中有不同的 Storage Class 对象吗? - GCS: Can we have different Storage Class objects inside a bucket? 仅列出 s3 存储桶中的子文件夹 - Listing just the sub folders in an s3 bucket 使用云存储选项从谷歌存储桶中的目录中删除所有对象 - Delete all objects from a directory in google storage bucket using cloud storage options 使用 S3AsynClient 和 SdkPublisher 返回 S3 存储桶内的对象列表 - Returning list of objects inside S3 bucket using S3AsynClient and SdkPublisher 如何停止列出 Firebase 存储公共存储桶中的所有项目? - How to stop listing all items in a Firebase Storage public bucket? 在同一项目中列出 Cloud Function 中的 GCS 存储桶 blob - Listing GCS bucket blobs from Cloud Function in the same project 从 Azure 存储中的目录列表中隐藏文件夹 - Hide folder from directory listing in Azure Storage 无法检索 Amazon 工作区上的目录列表 - Failed to retrieve directory listing on Amazon workspace Google Cloud Storage 对存储桶中的对象进行分页 (PHP) - Google Cloud Storage paginate objects in a bucket (PHP)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM