C# Google Cloud Storage Get ListObjects in Folders

折月煮酒 提交于 2020-01-24 20:07:05

问题


I'm storing all my application information in Google Cloud Storage. I've created a bucket and inside this bucket I've folders. With that code, I can get list of all my folders.

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;
}

But actually this code, get all my files and folders in my bucket. So I need to be extract them. My first question, Is there a shortcut to this method?

My other and most important question is, How can I get all files just in specific folder? For example; My bucket name is MyBucket and I want to get all files from "MyBucket/2/". How can I do this? Is this the only way check the medialink or selflink of the files?

Thanks for all answer. Have a good day, good works...


回答1:


If want to get top folders in your Google Cloud Storage, everyone can use;

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();
}

If want to get folders inside specific folder;

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();
}

Attention : I'm returning Prefixes for reach folders.




回答2:


I think what you want is to set the Delimiter property of the list request to /. This will return a delimited result at the top level of your hierarchy.




回答3:


This is for your question (Can you show me any example with C#? I cannot found any example in internet – Umut Çömlekçioğlu).Please check code in C# to Upload Object or get the list of objects -

    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;
                 }


来源:https://stackoverflow.com/questions/40134180/c-sharp-google-cloud-storage-get-listobjects-in-folders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!