Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C谷歌云存储获取文件夹中的ListObjects_C#_.net_Httpwebrequest_Google Cloud Storage - Fatal编程技术网

C# C谷歌云存储获取文件夹中的ListObjects

C# C谷歌云存储获取文件夹中的ListObjects,c#,.net,httpwebrequest,google-cloud-storage,C#,.net,Httpwebrequest,Google Cloud Storage,我将所有应用程序信息存储在谷歌云存储中。我创建了一个bucket,在这个bucket中我有文件夹。有了这些代码,我可以得到我所有文件夹的列表 public static IList<uFolder> ListFolders(string bucketName) { if (storageService == null) { CreateAuthorizedClient(); } Objects objects = stor

我将所有应用程序信息存储在谷歌云存储中。我创建了一个bucket,在这个bucket中我有文件夹。有了这些代码,我可以得到我所有文件夹的列表

public static IList<uFolder> ListFolders(string bucketName)
{
     if (storageService == null)
     {
          CreateAuthorizedClient();
     }

     Objects objects = storageService.Objects.List(bucketName).Execute();
     if (objects.Items != null)
     {
          return objects.Items.
               Where(x => x.ContentType == "application/x-www-form-urlencoded;charset=UTF-8").
               Select(x => new uFolder(x.Name)).ToList();
      }
      return null;
}
但实际上这段代码,把我所有的文件和文件夹都放在我的桶里。所以我需要把它们提取出来。我的第一个问题,这种方法有捷径吗

我的另一个也是最重要的问题是,我如何才能只获取特定文件夹中的所有文件?例如 我的bucket名称是MyBucket,我想从MyBucket/2/获取所有文件。我该怎么做?这是检查文件的medialink或selflink的唯一方法吗


谢谢你的回答。祝您愉快,工作顺利…

我想您需要的是将list请求的属性设置为/。这将在您的层次结构的顶层返回一个带分隔符的结果。

如果想要在您的Google云存储中获取顶级文件夹,每个人都可以使用

ObjectsResource.ListRequest request = storageService.Objects.List(CurrentBucket);
request.Delimiter = "/";
Google.Apis.Storage.v1.Data.Objects response = request.Execute();
if (response.Prefixes != null)
{
    return response.Prefixes.ToList();
}
如果要获取特定文件夹内的文件夹

ObjectsResource.ListRequest request = storageService.Objects.List(CurrentBucket);
request.Delimiter = "/";
request.Prefix = delimiter; //delimiter is any sub-folder name. E.g : "2010/"
Google.Apis.Storage.v1.Data.Objects response = request.Execute();
if (response.Prefixes != null)
{
    return response.Prefixes.ToList();
}

注意:我正在返回reach文件夹的前缀。

这是为了回答您的问题,您能给我举个C的例子吗?我在互联网上找不到任何例子–Umutïmlekçioğlu。请检查C中的代码以上载对象或获取对象列表-

    using Google.Apis.Auth.OAuth2;
    using Google.Cloud.Storage.V1;

    string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Test.csv");
    File.WriteAllText(file, "test");

    GoogleCredential credential = null;
    BucketConnector bucketConnector = new BucketConnector();
    credential = bucketConnector.ConnectStream();
    var storageClient = StorageClient.Create(credential);
    //Upload object in google bucket 
    string folderPath = ConfigurationParameters.FOLDER_NAME_IN_BUCKET;
    using (FileStream file = File.OpenRead(localPath))
    {
        objectName = folderPath + Path.GetFileName(localPath);
        storage.UploadObject(bucketName, objectName, null, file);           
    }   
 // get list of object from google bucket and specific folder   
ListObjectsOptions listObjectsOptions = new ListObjectsOptions();
                listObjectsOptions.Delimiter = "/";
               foreach (var obj in storageClient.ListObjects(ConfigurationParameters.BUCKET_NAME ,ConfigurationParameters.FOLDER_NAME_IN_BUCKET , listObjectsOptions))
                {
                    string obj_name = obj.Name;
                 }

你能给我举个C的例子吗?我在internetMr jterrace中找不到任何示例,我可以获得顶部目录中所有文件夹和文件的列表吗?设置分隔符应该做什么。另外,你能发布你的解决方案吗?