具有 ASP.Net Core 和 Razor 页面的 Azure 存储文件共享

Azure Storage FileShare with ASP.Net Core and Razor Page

初学者问题

我正在使用最新的 ASP.NET Core SDK 构建一个 MVC/Razor 页面来显示用户文件。我们在 Azure 存储文件共享中有这些文件。我 运行 遇到的困难是让文件列出来,但关于如何做到这一点的文档很少。一旦我终于弄明白了,我想在 Medium 或其他地方创建一个 post 来帮助任何其他初学者。

文件共享的文件结构如下:

Azure File Share
  MainShare
    EmployeNumber
      Folder1
        files.pdf
      Folder2
        files.pdf
      Folder3
        files.pdf

我已经能够成功获取要显示的 blob 信息,因为那里有大量信息,但我在 FileShare 上显示任何内容时遇到问题。

起初,我 运行 遇到了一个无效的转换问题,它试图在我的 CloudFile 上转换 CloudFileDirectory,所以我找到了一个解决方案来帮助它确定要转换的内容投哪里。现在页面将尝试 运行 但没有生成任何内容,页面只是加载和加载。

FileController.cs

public async Task<IActionResult> Index()
        {
            string filestorageconnection = _configuration.GetValue<string>("filestorage");
            CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(filestorageconnection);

            //CloudFile cloudFile = null;
            
            CloudFileShare fileShare = null;

            CloudFileClient cloudFileClient = cloudStorageAccount.CreateCloudFileClient();

            fileShare = cloudFileClient.GetShareReference("MainShare");
            

            List<IListFileItem> shareData = new List<IListFileItem>();
            List<FileShareData> fileData = new List<FileShareData>();

            FileContinuationToken token = null;
            do
            {
                FileResultSegment resultSegment =
                    await fileShare.GetRootDirectoryReference().ListFilesAndDirectoriesSegmentedAsync(token);

                foreach (var fileItem in resultSegment.Results)
                {
                    if (fileItem is CloudFile)
                    {
                        var cloudFile = (CloudFile) fileItem;
                        //await cloudFile.FetchAttributesAsync();   <--- Not sure this does what i'm looking for
                        
                        // Add properties to FileShareData List
                        fileData.Add(new FileShareData()
                        {
                            FileName = cloudFile.Name,
                            LastModified = DateTime.Parse(cloudFile.Properties.LastModified.ToString()).ToLocalTime().ToString(),
                            Size = Math.Round((cloudFile.Properties.Length / 1024f) / 1024f, 2).ToString()
                        });
                    }
                    else if (fileItem is CloudFileDirectory)
                    {
                        var cloudFileDirectory = (CloudFileDirectory) fileItem;
                        await cloudFileDirectory.FetchAttributesAsync();
                    }
                }
            } while (token != null);

            return View(fileData);  
        }

FileShareData.cs

namespace FileShareMVC.Models
{
    public class FileShareData
    {
        public string FileName { get; set; }
        public string LastModified { get; set; }
        public string Size { get; set; }
    }
}

ShowAllFiles.cshtml

@model List<FileShareData>
@{
    ViewData["Title"] = "ShowAllFiles";
}

<h1>ShowAllBlobs</h1>
<table class="table table-bordered">
    <thead>
    <tr>
        <th>FileName</th>
        <th>FileSize</th>
        <th>ModifiedOn</th>
        <th>Download</th>
    </tr>
    </thead>
    <tbody>
    @foreach (var data in Model)
    {
        <tr>
            <td>@data.FileName</td>
            <td>@data.Size</td>
            <td>@data.LastModified</td>
            <td> <a href="/File/Download?fileName=@data.FileName">Download</a> </td>
        </tr>
    }
    </tbody>
</table>

我不确定在哪里设置断点以查看什么停滞在哪里。我在加载页面时查看了 chrome 中的网络文件,但也没有填充任何内容。

建议?

关于如何使用方法ListFilesAndDirectoriesSegmentedAsync列出一个Azure文件共享中的所有文件,请参考以下代码

文件共享的文件结构如下:

Azure File Share
  MainShare
     mydirectory
      logs
        STATS.LOG        
      csv
        test.csv
      cert
        examplecert.pfx

我用的SDK

<PackageReference Include="Microsoft.Azure.Storage.File" Version="11.1.7" />

代码

  • FileController.cs
public class FileController : Controller
    {
        public async Task<IActionResult> Index()
        {
            string accountName = "blobstorage0516";
            string key = "eGier5YJBzr5z3xgOJUb+snTGDKhwPBJRFqb2nL5lcacmKZXHgY+LjmYapIHL7Csvgx75NwiOZE7kYLJfLqWBg==";
            var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, key), true);
            var share = storageAccount.CreateCloudFileClient().GetShareReference("mainshare");
            var dir =share.GetRootDirectoryReference();
            //list all files in the directory
            var fileData = await list_subdir(dir);
            return View(fileData);
        }



        private static async Task<List<FileShareData>> list_subdir(CloudFileDirectory fileDirectory)
        {
            var fileData = new List<FileShareData>();
            FileContinuationToken token = null;
            do
            {
                FileResultSegment resultSegment = await fileDirectory.ListFilesAndDirectoriesSegmentedAsync(token);
                foreach (var fileItem in resultSegment.Results) {

                    if (fileItem is CloudFile) {
                        var cloudFile = (CloudFile)fileItem;
                        //get the cloudfile's propertities and metadata 
                        await cloudFile.FetchAttributesAsync();  

                        // Add properties to FileShareData List
                        fileData.Add(new FileShareData()
                        {
                            FileName = cloudFile.Name,
                            LastModified = DateTime.Parse(cloudFile.Properties.LastModified.ToString()).ToLocalTime().ToString(),
                            // get file size as kb
                            Size = Math.Round((cloudFile.Properties.Length / 1024f), 2).ToString()
                        });

                    }

                    if (fileItem is CloudFileDirectory)
                    {
                        var cloudFileDirectory = (CloudFileDirectory)fileItem;
                        await cloudFileDirectory.FetchAttributesAsync();

                        //list files in the directory
                        var result = await list_subdir(cloudFileDirectory);
                        fileData.AddRange(result);
                    }
                }
                // get the FileContinuationToken to check if we need to stop the loop
                token = resultSegment.ContinuationToken;
            }
            while (token != null);

            return fileData;
            

        }
    }
  • FileShareData.cs
public class FileShareData
    {
        public string FileName { get; set; }
        public string LastModified { get; set; }
        public string Size { get; set; }
    }
  • ShowAllFiles.cshtml
@model List<FileShareData>
@{
    ViewData["Title"] = "ShowAllFiles";
}

<h1>ShowAllBlobs</h1>
<table class="table table-bordered">
    <thead>
    <tr>
        <th>FileName</th>
        <th>FileSize</th>
        <th>ModifiedOn</th>
        <th>Download</th>
    </tr>
    </thead>
    <tbody>
    @foreach (var data in Model)
    {
        <tr>
            <td>@data.FileName</td>
            <td>@data.Size</td>
            <td>@data.LastModified</td>
            <td> <a href="/File/Download?fileName=@data.FileName">Download</a> </td>
        </tr>
    }
    </tbody>
</table>

结果

详情请参考here and