3
votes

I am trying to upload a file to a bucket in google cloud storage.

I wrote the following code to create the StorageService and upload the file to the bucket.

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;
}

However, an ArgumentNullException that says "Value cannot be null. Parameter name: baseUri" is thrown when call the upload method.

Does anyone know how to solve this problem ?

1
Can you check if the JSON API is enabled for Cloud Storage in the APIs & Auth pane in the Developers Console. I`ve read on other posts that even though you are using .NET, the JSON API still needs to be enabled.vaqars
The JSON API is Enabled in the developers console.Oday Fraiwan
I did find another case on StackOverflow were the person mentions that this error is a default error. Maybe you can try opening a new service account to see if you get the same error.vaqars

1 Answers

0
votes

I ran into this error too and it was due to having a space as the first character in my stream object. Once I made that a non-space character the upload worked. As Vaqar says above this seems to be a default error so not sure if this answer helps but maybe will help others...