Search code examples
listgoogle-cloud-platformgoogle-cloud-storagebucket

Google Cloud ListBuckets


I can add a bucket , but I cant list my buckets.

Here is my code ,

var credential = GoogleCredential.FromStream(new FileStream("gcFile.json", FileMode.Open))
                  .CreateScoped(new string[] { StorageService.Scope.DevstorageFullControl });
        var client = StorageClient.Create(credential);
       // var bb = client.CreateBucket("projectId", "king");
        var aa = client.ListBuckets("projectId");

In my service accounts I set project owner for the json file but I dont understand where is the problem

Result View : Children could not be evaluated

Also I tried with this ; ,StorageService.Scope.CloudPlatformReadOnly, StorageService.Scope.DevstorageReadOnly

Framework 4.5.2

ASP.NET

Google.Cloud.Storage.V1 version=2.0.0

Google.Apis.Storage.v1 version=1.27.1.881


Solution

  • Authentication

    The simplest way to invoke Google Cloud APIs using the client libraries is to rely on Application Default Credentials.

    Setting up Application Default credentials using gcloud

    If you already have gcloud installed, you can run the following command to set up application default credentials (this needs to be run only once for your device):

    gcloud auth application-default login
    

    Creating a client using Application Default Credentials and invoking APIs

    After this, in your code you can instantiate the Storage client and invoke the GCS APIs as show below:

    // Creates the storage client (authenticates using the Application Default credentials you configured earlier)
    var client = StorageClient.Create();
    
    // GCS bucket names must be globally unique
    var bucketName = Guid.NewGuid().ToString();
    
    // Bucket defined in Google.Apis.Storage.v1.Data namespace
    var bucket = client.CreateBucket(projectId, bucketName);
    
    // List all buckets associated with a project
    var buckets = client.ListBuckets(projectId);
    

    References