【问题标题】:Unable to read files from Azure File Share无法从 Azure 文件共享读取文件
【发布时间】:2017-04-11 02:11:14
【问题描述】:

我的应用程序具有批量上传功能,电子表格中的用户提供文件路径,例如 Client1/Creative Files/File1.jpg,这就是这些文件将存储在云文件共享中的方式。批量上传过程将从该位置读取文件,如果找到,将复制到 blob 存储并在数据库中创建一个条目。最初,我在 blob 存储中拥有所有内容,因此读取文件非常简单。

CloudBlobContainer container = this.GetCloudBlobContainer();
var blob = container.GetBlockBlobReference(filePath);

由于无法映射到 blob 存储帐户的网络驱动器,我创建了一个新的存储帐户并创建了文件服务,因此我可以映射到 windows 上的共享,以便用户直接从他们的文件上传文件机器。现在,我在从该共享中获取文件时遇到问题,因为它总是说文件不存在。

public CloudFileShare GetCloudFileShare()
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(settings.AzureDropBoxConnectionString);
        CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
        CloudFileShare filehare = fileClient.GetShareReference(settings.AzureDropboxFileShare);
        filehare.CreateIfNotExists();

        return filehare;
    }
public CloudFile GetFileFromCloudShare(string filePath)
    {
        CloudFileShare fileShare = this.GetCloudFileShare();
        var file = fileShare.GetRootDirectoryReference().GetFileReference(filePath);

        return file;
    }

当我检查 file.Exists() 时,虽然文件存在,但它返回“False”。

文件路径是 Client1/Creative Files/File1.jpg。我想如果我只是将 File1.jpg 上传到没有文件夹的共享,这应该可以工作,但添加子文件夹会产生问题。我们需要组织文件,可能会有一些基于客户端的嵌套文件夹以及用户如何组织它们,这就是我们要求他们在电子表格中提供路径的原因。我错过了什么?

当我调试应用程序并检查文件 StorageUri 时,我看到 https://storageaccount.file.core.windows.net/sharename/Client%5CCreative%20Files%5CFile1.jpg

在浏览器上复制此网址后,我没有看到该文件。就像我说的,对于 blob 容器,同样的过程只在文件中工作,但是当我将文件移动到文件共享时,我无法返回文件。

【问题讨论】:

  • 您如何确保文件存在于共享中?您是否正在使用某种工具来检查文件是否存在?
  • 我已从 Windows 映射到 azure 文件共享,并以与电子表格中所示相同的结构上传文件,即 Client1/Creative Files/File1.jpg。我还可以通过转到 Azure 门户 -> 存储帐户 -> 文件服务 -> 共享来进行验证。所以,文件存在,但当我使用上面的代码时它不存在。
  • 还有一件事...所以在您的代码中,您将filePath 传递为Client1/Creative Files/File1.jpg。对吗?
  • 没错。调试后,它出现为 Client1\\Creative Files\\File1.jpg。
  • 在电子表格中,我的文件路径为 Client1/Creative Files/File1.jpg

标签: azure asp.net-mvc-4


【解决方案1】:
  1. 我尝试同时指定 Client1\\Creative Files\\File1.jpgClient1/Creative Files/File1.jpg,在这两种情况下我都将 file.Exists() 设置为 true。请您再次确认该文件确实存在。

  2. 您根本无法获取文件 URL 并将其粘贴到浏览器中并期望在浏览器中看到该文件。在 Blob 存储中,您可以将容器的 ACL 设置为 BlobContainer,然后当您将 Blob 的 URL 复制到浏览器的地址窗口中时,该 Blob 将显示在那里。另一方面,文件共享始终为Private,并且需要授权对文件的访问。您可以做的一件事是在文件上创建一个Shared Access Signature (SAS),然后在浏览器的地址窗口中复制 SAS URL,然后您将能够看到该文件。

        var cred = new StorageCredentials(accountName, accountKey);
        var account = new CloudStorageAccount(cred, true);
        var client = account.CreateCloudFileClient();
        var fileShare = client.GetShareReference("temp");
        var rootDirectory = fileShare.GetRootDirectoryReference();
        var file1 = rootDirectory.GetFileReference("Client1\\Creative Files\\test.png");
        Console.WriteLine(file1.Exists());
        var file2 = rootDirectory.GetFileReference("Client1/Creative Files/test.png");
        Console.WriteLine(file2.Exists());
    
        //Create shared access signature on file with read permission valid for 1 hour.
        var sasToken = file2.GetSharedAccessSignature(new SharedAccessFilePolicy()
        {
            Permissions = SharedAccessFilePermissions.Read,
            SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1)
        });
        var fileSasUrl = string.Format("{0}{1}", file2.Uri.AbsoluteUri, sasToken);
        Console.WriteLine(fileSasUrl);
    

【讨论】:

  • 对不起我的愚蠢。我确实检查了我的文件路径,在门户网站上,其中一个文件夹显示“创意证明”,但在我的测试文件中,我将其作为创意文件。抱歉,我只是一直在寻找创意文件作为创意证明。一旦我修复了我的文件路径,它现在可以肯定地工作了。感谢您提供使用共享访问签名的替代解决方案,并增强了我对代码应该工作的信心。这恰好是用户方面的错误。 :)
猜你喜欢
  • 1970-01-01
  • 2013-02-21
  • 2020-11-06
  • 1970-01-01
  • 2021-12-05
  • 2018-06-22
  • 2022-06-30
  • 2021-12-27
  • 1970-01-01
相关资源
最近更新 更多