Slash, Google Cloud Storage & .NET4

On a project I am working on, we were trying to upload and delete files on Google Cloud from a .NET solution.

Well, first downloading from Nuget the Google.Apis.Storage.v1 dll’s and upgrading the solution to .NET4 – the minimal version for the Google API to run.

Running it from a test project, all files were uploaded and deleted fine. However, when implementing the code to the main project, an error was caught on the deleting process.

The project is built using ‘folder’ (object) for every group of files, thus we were running in a loop on all the files calling from the service (Google.Apis.Storage.v1.StorageService), which has all the credentials. the Objects property, and the Delete method, something like this:

The method resulted in the obscure error message: “An Error occurred, but the error response could not be deserialized”.

Very helpful…

Now the investigation has begun.

What the Google API is doing, when is called to put, delete or get a file, is calling the Googleapis domain with a the relevant method – here it is Delete.

Monitoring the traffic, enables one to see what the response was, that led to the error.

For this purpose, the best tool is Fiddler: monitor the action: see the request, see the response.

The response was 404 – not found.

404 is the error that stands behind the exception “An Error occurred, but the error response could not be deserialized”. Well, next time be more informative…

What about the request, is it built correctly?

On Google developers centre one can test and see the url it builds – which performs the Delete action. It should be something like:

OK, let’s try and build the request, instead of using the Google.Apis, so we can properly debug it.

Checking it on debug mode, the HttpWebRequest  does something rather bizarre: although explicitly asked Uri.EscapeDataString, the HttpWebRequest  does an unescaping.

Fiddler confirms, the request contains a ‘/’ (slash) without the url escaping ‘%2F’.

Apparently it is a know Microsoft behaviour.

Solving it is quite simple, adding to the web.config / app.config under configuration:

Keep in mind to have it https and not http, for this matter.

That solved it all.

 

Happy coding!

9 thoughts on “Slash, Google Cloud Storage & .NET4

  1. Thanks for sharing the article.

    i was looking to downloading ‘object’ from Google storage using asp.net but have been unsuccessful.
    Would you have any pointers?

    • You can try this code:
      var getRequest = storageService.Objects.Get([bucket], [objectname]);
      using (var fileStream = new System.IO.FileStream(
      “C:\\PATH\\TO\\NEW\\FILE.xxx”,
      System.IO.FileMode.Create,
      System.IO.FileAccess.Write))
      {

      getRequest.Download(fileStream);
      }

  2. Thanks so much! I was experiencing this bug and no telling how long it would’ve taken me to figure out the fix on my own.

  3. Another huge thank you – this bug was killing me and I spent hours tracking it down. Once I worked out adding a slash to the key caused the problem I was able to google your article, and you’ve saved me hours of frustration – my app works perfectly now, just by adding those config lines.

Leave a Reply

Your email address will not be published. Required fields are marked *

*