简体   繁体   中英

C# Google Cloud Storage Get ListObjects in Folders

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...

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM