无法使用c#windows应用程序将图像上传到Google云端桶

时间:2018-05-03 11:17:22

标签: c# google-cloud-storage

您好我已经创建了一个Windows应用程序,可将图像从hdd上传到谷歌云服务器。 我的代码工作正常,但更改了存储桶名称后,它无法正常工作。 我的两个桶都在同一个项目中,我已经为我的项目提供了OAuth 2.0。

即使处理过程中没有显示错误。请帮帮我。

            string bucketForImage = ConfigurationManager.AppSettings["BucketName"];
            string projectName = ConfigurationManager.AppSettings["ProjectName"];
            string Accountemail = ConfigurationManager.AppSettings["Email"];
            var clientSecrets = new ClientSecrets();
            clientSecrets.ClientId = ConfigurationManager.AppSettings["ClientId"];
            clientSecrets.ClientSecret = ConfigurationManager.AppSettings["ClientSecret"];

            string gcpPath = @"D:\mrunal\tst_mrunal.png";

            var scopes = new[] { @"https://www.googleapis.com/auth/devstorage.full_control" };
            var cts = new CancellationTokenSource();
            var userCredential = await GoogleWebAuthorizationBroker.AuthorizeAsync(clientSecrets, scopes, Accountemail, cts.Token);
            var service = new Google.Apis.Storage.v1.StorageService();
            var bucketToUpload = bucketForImage;
            var newObject = new Google.Apis.Storage.v1.Data.Object()
            {
                Bucket = bucketToUpload,
                Name = "mrunal.png"
            };

            fileStream = new FileStream(gcpPath, FileMode.Open);
            var uploadRequest = new Google.Apis.Storage.v1.ObjectsResource.InsertMediaUpload(service, newObject,
            bucketToUpload, fileStream, "image/png");
            uploadRequest.OauthToken = userCredential.Token.AccessToken;
            await uploadRequest.UploadAsync();
            //uploadRequest.UploadAsync();
            if (fileStream != null)
            {
                fileStream.Dispose();
            }

1 个答案:

答案 0 :(得分:0)

您是否尝试使用相同的旧版存储桶代码并且有效?在我看来,代码行有一个问题,uploadRequest.OauthToken = userCredential.Token.AccessToken。您正在直接从userCredentials调用Token.AccessToken。应该从userCredentials.Result

调用这些方法