【问题标题】:Azure CloudFile - "The specifed resource name contains invalid characters."Azure CloudFile - “指定的资源名称包含无效字符。”
【发布时间】:2017-12-04 14:54:20
【问题描述】:

我正在尝试将文件从 Azure File Storage 下载到本地文件并收到此异常:

“指定的资源名称包含无效字符。”

代码如下:

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

saveTo 参数是一个字符串,值是这样的:

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

saveFrom 参数是这样的:

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

我正在使用这个函数创建参数:

var saveTo = Path.GetTempFileName();

我做错了什么?我对 Azure 没有太多经验。

【问题讨论】:

  • “saveFrom”变量的值是多少?
  • @GauravMantri...请看我的更新,谢谢

标签: c# azure azure-storage azure-blob-storage


【解决方案1】:

问题在于您的 saveFrom 变量。它应该只包含文件的名称,而不是整个 URL。所以如果你要下载的文件是tmpA2DA.tmp,你的代码应该是:

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

请进行此更改,然后重试。它应该可以工作。

这是我用来测试的完整代码:

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

【讨论】:

  • 那是因为 'tempDir' 有路径,对吧?谢谢
猜你喜欢
  • 2020-02-04
  • 2020-02-26
  • 1970-01-01
  • 2023-03-03
  • 2014-12-04
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
相关资源
最近更新 更多