下面的代码仅用于读取存储桶中所有文件夹中的所有对象。
string bucketName = "testbucket";
// Explicitly use service account credentials by specifying the private key file.
// The service account should have Object Manage permissions for the bucket.
GoogleCredential credential = null;
using (var jsonStream = new FileStream("credentials.json", FileMode.Open,
FileAccess.Read, FileShare.Read))
{
credential = GoogleCredential.FromStream(jsonStream);
}
var storageClient = StorageClient.Create(credential);
// List objects
foreach (var obj in storageClient.ListObjects(bucketName, ""))
{
//Console.WriteLine(obj.Name);
var fileStream = File.Create("Program-copy.xml");
storageClient.DownloadObject(bucketName, obj.Name, fileStream);
break;
}发布于 2019-12-15 17:10:49
您当前正在指定一个""前缀,该前缀确实会匹配所有对象。
如果你想指定一个文件夹,比如"foo/bar",只需指定前缀foo/bar/即可。
为了确保不会同时检索子目录中的项,需要指定Delimiter选项。例如,您可以使用:
var options = new ListObjectsOptions { Delimiter = "/" };
var items = storageClient.ListObjects(bucketName, "foo/bar/", options);如果要包括子目录的项目,但不包括该子目录的所有内容,请将IncludeTrailingDelimeter = true添加到选项中。
https://stackoverflow.com/questions/59341198
复制相似问题