无法上传到Google云端存储

时间:2014-08-12 00:09:33

标签: c#-4.0 cloud google-cloud-storage

尝试使用适用于.Net的Google API客户端库将文件上传到Google云端存储。这是我的代码。它运行良好,没有任何错误,但也没有做到这一点。请提出我在这里缺少的建议,或者是解决这个问题的正确方法。 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();
        }

1 个答案:

答案 0 :(得分:1)

您尚未授权在代码中的任何位置访问Google云端存储。请在此处查看我的答案:Uploading objects to google cloud storage in c#以获取如何操作的示例。