列出存储桶目录中的对象
Listing objects inside the directory of bucket
几天前,我在使用 Google Cloud Storage 列出和下载存储桶中的对象,但现在我很难只读取特定目录中的对象,因为我只想迭代此目录中的对象.我的存储桶是“pubsite_prod_rev_12345”,但我尝试只读“/earnings/”中的元素:
string bucketId = "pubsite_prod_rev_12345/earnings";
所以现在我求助于这件事的专家,这是我的代码:
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);
}
我不想过滤,只是获取 /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 云存储没有目录。 filename 的目录部分称为 prefix.
例如对象名称:tmp/log.txt 具有前缀 tmp/.
下一个概念是定界符。这通常是 / 字符。
要指定目录路径 tmp 使用前缀 tmp/ 和分隔符 /.任何以 tmp/ 开头且不包含定界符(不包括前缀)的对象都作为名称返回。任何以 tmp/ 开头并包含定界符的对象都作为前缀返回。示例 tmp/subdirectory/123 将作为前缀 tmp/subdirectory/ 返回,没有任何附加名称。
复杂的解释在这里是一个例子:
注意:您的 bucketId 行不正确。存储桶名称不包含对象名称的任何部分。
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);
}
几天前,我在使用 Google Cloud Storage 列出和下载存储桶中的对象,但现在我很难只读取特定目录中的对象,因为我只想迭代此目录中的对象.我的存储桶是“pubsite_prod_rev_12345”,但我尝试只读“/earnings/”中的元素:
string bucketId = "pubsite_prod_rev_12345/earnings";
所以现在我求助于这件事的专家,这是我的代码:
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);
}
我不想过滤,只是获取 /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 云存储没有目录。 filename 的目录部分称为 prefix.
例如对象名称:tmp/log.txt 具有前缀 tmp/.
下一个概念是定界符。这通常是 / 字符。
要指定目录路径 tmp 使用前缀 tmp/ 和分隔符 /.任何以 tmp/ 开头且不包含定界符(不包括前缀)的对象都作为名称返回。任何以 tmp/ 开头并包含定界符的对象都作为前缀返回。示例 tmp/subdirectory/123 将作为前缀 tmp/subdirectory/ 返回,没有任何附加名称。
复杂的解释在这里是一个例子:
注意:您的 bucketId 行不正确。存储桶名称不包含对象名称的任何部分。
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);
}