A hack to stop false storage connection failures with PHP on Windows Azure

April 20, 2011

What I am about to show you is a terrible hack, however if you are having random issues with storage connection in Windows Azure while using the PHP SDK this just might be the stopgap to keep your app working.

First let me give you a description of the issue:

Your code instantiates an object to connect to Windows Azure storage, Let’s use table storage for example. The connection works great, most of the time. Seemingly randomly you receive a failure to connect/authenticate message which magically disappears when your refresh the page. Somehow there was a hiccup somewhere during the connection creation, which was no fault of your own. To combat this I started creating my connections in a loop. That way if the connection fails it will do an “auto refresh” inside the code. Please remember that this is just a hack, and a brainless one at that. If you were to use this method on a production service I highly recommend you understand and deal with all the possible exceptions that can be thrown instead of lumping them all together as this example does.

do {
   $again = false;

   try {
      $table = new Microsoft_WindowsAzure_Storage_Table (
   catch(Exception $e) { $again = true; }
} while($again);

There you go. I have not had the authentication issues since I have started using this hack. I am going to continue looking into it and as soon as I determine the root cause the PHP SDK will receive the update (Unless of course it is a Windows Azure architecture problem, which I halfway suspect).

Happy hacking!

Learn more about running PHP on Windows Azure at http://azurephp.interopbridges.com