1

Trying to upload a file to Google Cloud Storage using Google APIs Client Library for .Net. Here is my code. It runs fine without any errors but doesn't do the job either. Please advice what I'm missing here, or is it the right way to approach this problem. Apreciate it!

        try
        {
            Google.Apis.Services.BaseClientService.Initializer init = new Google.Apis.Services.BaseClientService.Initializer();
            init.ApiKey = "server-apps-API-KEY-HERE";
            init.ApplicationName = "Project Default Service Account";

            Google.Apis.Storage.v1.StorageService ss = new Google.Apis.Storage.v1.StorageService(init);

            Google.Apis.Storage.v1.Data.Object fileobj = new Google.Apis.Storage.v1.Data.Object();
            fileobj.ContentType = "image/jpeg";

            ///READ FILE
            string file = @"C:\Photos\TEST.jpg";

            System.IO.Stream j = new System.IO.FileStream(file,
                                  System.IO.FileMode.Open,
                                   System.IO.FileAccess.Read);

            //New File Name
            fileobj.Name = "TEST.jpg";

            Google.Apis.Storage.v1.ObjectsResource.InsertMediaUpload insmedia;
            insmedia = new Google.Apis.Storage.v1.ObjectsResource.InsertMediaUpload(ss, fileobj, "test-common", j, "image/jpeg");
            insmedia.Upload();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.ReadLine();
        }
Luke Paan
  • 11
  • 4

1 Answers1

1

You have not authorized access to your Google Cloud Storage anywhere in your code. See my answer here: Uploading objects to google cloud storage in c# to get example how to do it.

Community
  • 1
  • 1
Marcin Zablocki
  • 10,171
  • 1
  • 37
  • 47