“Path too long” when using local storage in an Azure Cloud Service
It is insane that in 2013 we still try to workaround the 260 character limit in a file path, more when the conversations are centered on services, scaling and other meta-cloud philosophies.
But this problem is still here and working with VMs hosted in the cloud is no different, on-premise problems will appear in the same way when online. I would recommend the “Long Paths in .NET” series from Kim Hamilton where this matter is deeply discussed. The post is from more than 6 years ago…and this is not solved yet.
So let’s talk about the problem I had. When using local storage on an Azure web or worker role, the root location for this storage while running on Azure starts with a path that can “eat” almost the half of the MAX_PATH -note that I’m not referring to the problem when playing on DevFabric in your local environment, whose solution is commented on this MSDN blog post. This will be something like:
The problem is that the RoleDeploymentId is 32 char length, and if you want to keep some sanity on your role names and storage names you will use something that has some meaning. In my case, the path was “eating” 108 chars, almost the half of the max path. While creating some recursive folders inside that temp path the “Path too long” error will appear sooner than later.
Techniques from the past to solve problems from the past
After playing a little bit with Subst.exe, didn’t convince me because I was trying to keep distance from mounting and managing drives between different user sessions. But then César Abreu give an interesting idea: why not use old style DOS format for the paths?
Interesting approach, so after some reading on the Fsutil.exe documentation on TechNet, I did some tests on a running instance:
1) Enable the short name behavior for volume C:
2) Add a short name to the local storage folder
I finally created a simple two line startup task to be executed with elevated privileges and that would give me an additional 83 characters.
REM Enable short names on volume C:
fsutil.exe 8dot3name set c: 0
REM Add a short name to the local storage folder
fsutil.exe file setshortname "C:\Resources\Directory\%RoleDeploymentID%.MyWebRole.MyLocalStorage" S
The first line enables the short name behavior for volume C:. I noticed that this is enabled for other volumes on an Azure role, but is not enabled by default on C: drive. The second line simply sets the folder short name as “S”. With this I can use then the path “C:\Resources\Directory\S” instead of the long one, and worked like a charm. Note that you can’t do the same for “Resources” and “Directory” folder names, since they are in use during the role startup, and would give you an error while trying to do the operation.
Hope this helps!