【问题标题】:Accessing the file stored in Azure file storage without SAS token在没有 SAS 令牌的情况下访问存储在 Azure 文件存储中的文件
【发布时间】:2019-05-17 19:57:55
【问题描述】:

使用 Azure 云文件共享 API,我能够创建文件共享以及目录、文件夹和文件。它们都正确保存在文件共享中。 从浏览器中,我可以使用 URI + SAS 令牌(为文件共享生成)查看内容。那么,在 .NET 中,访问 Azure 文件的最佳方式是什么?我们可以在没有 SAS 令牌的情况下访问文件(不是 blob)吗? SAS 令牌是强制性的吗?如果是这样,什么时候生成令牌合适? (每次访问文件时?)

【问题讨论】:

  • @IvanYang 我认为 OP 对他必须通过直接链接访问存储中的文件的选项更加好奇。如果存在除 URI + SAS 令牌组合之外的任何其他选项。

标签: c# .net azure fileshare


【解决方案1】:

有3种方式访问​​存储:

1) 使用主/根密钥。我不建议使用这种方法,因为主密钥对存储帐户具有完全访问权限。

2) 使用 SAS 密钥。这是一个很好的方法,因为您可以限制密钥提供的访问量。请参阅this site 的最佳实践部分。您需要在访问存储之前生成密钥并将其安全地存储在配置中。请注意,blob 是您的文件。 Blob 被组织到 Blob 存储容器中,然后位于您的存储帐户中。

3) 如果您选择新的 Azure Data Lake Gen 2 存储 (ADLS Gen 2),则可以将 Azure Active Directory 身份验证与服务帐户或托管服务标识 (MSI) 结合使用。请注意,ADLS Gen 2 仍处于预览阶段。

【讨论】:

    【解决方案2】:

    如果尝试在 c#/.net 中访问/操作文件共享,则不必使用 SAS 令牌。

    您可以只使用存储帐户名称和帐户密钥进行身份验证,然后按照官方doc 分别创建/删除文件共享/目录/文件等操作文件共享。

    除了在上面文档中提到的配置文件中存储帐户名/密钥外,您还可以直接在您的c#代码中使用它们,如下所示(.net框架控制台项目):

    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Auth;
    using Microsoft.WindowsAzure.Storage.File;
    using System;
    
    namespace ConsoleApp1File
    {
        class Program
        {
            static void Main(string[] args)
            {
                string accountname = "xxx";
                string accountkey = "xxxxxxx";
                CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials(accountname, accountkey), true);
    
                // Create a CloudFileClient object for credentialed access to Azure Files.
                CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
    
                // Get a reference to the file share.
                CloudFileShare share = fileClient.GetShareReference("s66");
    
                //if fileshare does not exist, create it.
                share.CreateIfNotExists();
    
                if (share.Exists())
                {
    
                    // Get a reference to the root directory for the share.
                    CloudFileDirectory rootDir = share.GetRootDirectoryReference();
    
                    // Get a reference to the directory.
                    CloudFileDirectory sampleDir = rootDir.GetDirectoryReference("CustomLogs");
    
                    //if the directory does not exist, create it.
                    sampleDir.CreateIfNotExists();
    
                    if (sampleDir.Exists())
                    {
                        // Get a reference to the file.
                        CloudFile file = sampleDir.GetFileReference("Log1.txt");
    
                        // if the file exists, read the content of the file.
                        if (file.Exists())
                        {
                            // Write the contents of the file to the console window.
                            Console.WriteLine(file.DownloadTextAsync().Result);
                        }
                        //if the file does not exist, create it with size == 500bytes
                        else
                        {
                            file.Create(500);
                        }
                    }
                }
    
                Console.WriteLine("--file share test--");
                Console.ReadLine();
    
            }
        }
    }
    

    【讨论】:

    • FWIW,对象存储中文件的存在性检查(HEAD 请求)的往返时间使得尝试打开文件(GET)通常更快,并且捕获丢失时引发的异常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 2020-05-30
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    • 2018-02-19
    相关资源
    最近更新 更多