2015-04-23 63 views
3

我想上传文件到谷仓云存储中的存储桶。谷歌云存储插入媒体上传服务BaseUri ArgumentNullException

我写了下面的代码来创建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,说:“值不能为空参数名:基本URI”呼上传方法时抛出。

有谁知道如何解决这个问题?

+0

你可以检查是否在开发人员的API和身份验证窗格中为云存储启用JSON API安慰。我已经阅读过其他文章,即使您使用.NET,JSON API仍需要启用。 – Vaqar

+0

JSON API在开发人员控制台中启用。 –

+0

我在StackOverflow上找到了另一个[case](http://stackoverflow.com/questions/26204080/big-query-exception-is-value-cannot-be-null-parameter-name-baseuri)这个错误是一个默认错误。也许你可以尝试打开一个新的服务帐户,看看你是否得到相同的错误。 – Vaqar

回答

0

我也遇到了这个错误,这是因为有一个空间作为我的流对象中的第一个字符。一旦我做了一个非空间角色的上传工作。由于Vaqar上面说这似乎是一个默认错误,所以不知道这个答案是否有帮助,但也许会帮助其他人...