[英]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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.