Azure CloudFile - "The specifed resource name contains invalid characters."

11,871

The problem is with your saveFrom variable. It should only contain the name of the file and not the whole URL. So if the file you're trying to download is tmpA2DA.tmp, your code should be:

var file = tempDir.GetFileReference("tmpA2DA.tmp");

Please make this change and try again. It should work.

Here's the complete code I used to test:

    static void FileDownloadTest()
    {
        var cred = new StorageCredentials(accountName, accountKey);
        var account = new CloudStorageAccount(cred, true);
        var client = account.CreateCloudFileClient();
        var _cloudFileShare = client.GetShareReference("fileshare");
        if (_cloudFileShare.Exists())
        {
            CloudFileDirectory rootDir = _cloudFileShare.GetRootDirectoryReference();
            CloudFileDirectory tempDir = rootDir.GetDirectoryReference("temp");
            if (tempDir.Exists())
            {
                var saveTo = System.IO.Path.GetTempFileName();
                var file = tempDir.GetFileReference("tmpA2DA.tmp");
                file.DownloadToFile(saveTo, FileMode.Open);
            }
        }
    }
Share:
11,871
Big Daddy
Author by

Big Daddy

Working for The Man.

Updated on September 02, 2022

Comments

  • Big Daddy
    Big Daddy over 1 year

    I'm trying to download a file from Azure File Storage to a local file and get this exception:

    "The specifed resource name contains invalid characters."

    Here's the code:

    if (_cloudFileShare.Exists())
    {
          CloudFileDirectory rootDir = _cloudFileShare.GetRootDirectoryReference();    
          CloudFileDirectory tempDir = rootDir.GetDirectoryReference("temp");
          if (tempDir.Exists())
          {
            var file = tempDir.GetFileReference(saveFrom);
            file.DownloadToFile(saveTo, FileMode.Open);// OFFENDING LINE
          }
     }
    

    The saveTo argument is a string and the value is something like this:

    "C:\Users\Me\AppData\Local\Temp\tmpF2AD.tmp"
    

    The saveFrom argument is something like this:

    https://storageaccount.file.core.windows.net:443/fileshare/temp/tmpA2DA.tmp

    I'm creating the argument using this function:

    var saveTo = Path.GetTempFileName();
    

    What am I doing wrong? I don't have much experience with Azure.

  • Big Daddy
    Big Daddy almost 7 years
    That's because 'tempDir' has the path, right? Thanks