【发布时间】:2015-04-23 10:29:19
【问题描述】:
我正在尝试将文件上传到谷歌云存储中的存储桶。
我编写了以下代码来创建 StorageService 并将文件上传到存储桶。
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer() {
ClientSecrets = new ClientSecrets { ClientId = _googleSettings.ClientID, ClientSecret = _googleSettings.ClientSecret },
DataStore = new FileDataStore(System.Web.HttpContext.Current.Server.MapPath("/App_Data")),
Scopes = new string[] { StorageService.Scope.DevstorageReadWrite },
});
UserCredential userCredentials = new UserCredential(flow, Environment.UserName, new TokenResponse());
StorageService service = new StorageService(new BaseClientService.Initializer()
{
HttpClientInitializer = userCredentials,
ApplicationName = "GCS Sample"
});
Google.Apis.Storage.v1.Data.Object fileobj = new Google.Apis.Storage.v1.Data.Object() { Name = fileDetails.FileName };
ObjectsResource.InsertMediaUpload uploadService = service.Objects.Insert(fileobj, _googleSettings.BucketName, fileDetails.Stream, fileDetails.MimeContentType);
IUploadProgress progress = uploadService.Upload();
if (progress.Exception != null) //The exception always exists.
{
throw progress.Exception;
}
但是,在调用上传方法时会抛出一个 ArgumentNullException 说“Value cannot be null. Parameter name: baseUri”。
有谁知道如何解决这个问题?
【问题讨论】:
-
您能否在 Developers Console 的 APIs & Auth 窗格中检查是否为 Cloud Storage 启用了 JSON API。我读过其他帖子,即使您使用的是 .NET,仍然需要启用 JSON API。
-
在开发者控制台中启用了 JSON API。
-
我确实在 StackOverflow 上找到了另一个 case,有人提到此错误是默认错误。也许您可以尝试打开一个新的服务帐户,看看是否会遇到同样的错误。
标签: c# .net google-api google-oauth google-cloud-storage