特定文件的 Azure SAS 令牌

Azure SAS Token for a specific file

我正在使用 Azure Data Lake(使用分层命名空间)构建文件存储 POC。我需要为存储在目录结构中的特定文件生成 SAS 令牌。我找不到任何这样做的例子。感谢是否有任何指示或代码片段可以执行此操作。

您可以在您之前的代码中实现如下:

我只是将生成的 Blob SAS URL 写入文件 WriteLines.txt

using Azure;
using Azure.Storage;
using Azure.Storage.Files.DataLake;
using Azure.Storage.Files.DataLake.Models;
using Azure.Storage.Sas;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NUnit.Framework;
using System.IO;

namespace DataLakeHelloWorld
{
    class Program
    {
       

        static void Main(string[] args)
        {
            Console.WriteLine("Starting....");
            try
            {
                Console.WriteLine("Executing...");
                CreateFileClientAsync_DirectoryAsync().Wait();
                Console.WriteLine("Done");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }


        static async Task CreateFileClientAsync_DirectoryAsync()
        {
            // Make StorageSharedKeyCredential to pass to the serviceClient
            string storageAccountName = "kteststorageeadls";
            string storageAccountKey = "6fAe+K8ARe8LH9Ah2HuLuEONpxxxxxxxxxSjLy3oVPAgj04m+zWZuy5X8p4/BaQTY8efzCj/X+On/FwmvgSo6g==";
            string dfsUri = "https://" + "kteststorageeadls" + ".dfs.core.windows.net";

            StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey);

            // Create DataLakeServiceClient using StorageSharedKeyCredentials
            DataLakeServiceClient serviceClient = new DataLakeServiceClient(new Uri(dfsUri), sharedKeyCredential);

            // Create a DataLake Filesystem
            DataLakeFileSystemClient filesystem = serviceClient.GetFileSystemClient("my-filesystem-dfs");
            if (!await filesystem.ExistsAsync())
                await filesystem.CreateAsync();

            //Create a DataLake Directory
            DataLakeDirectoryClient directory = filesystem.CreateDirectory("my-dir");
            if (!await directory.ExistsAsync())
                await directory.CreateAsync();

            // Create a DataLake File using a DataLake Directory
            DataLakeFileClient file = directory.GetFileClient("my-file");
            if (!await file.ExistsAsync())
                await file.CreateAsync();

            // Verify we created one file
            var response = filesystem.GetPathsAsync();
            IAsyncEnumerator<PathItem> enumerator = response.GetAsyncEnumerator();
            Console.WriteLine(enumerator?.Current?.Name);

            //**********Start-add-from-here************

            DataLakeSasBuilder dataLakeSasBuilder = new DataLakeSasBuilder
            {
                StartsOn = DateTimeOffset.UtcNow.AddHours(-1),
                ExpiresOn = DateTimeOffset.UtcNow.AddHours(1),
                FileSystemName = file.FileSystemName.ToString()
            };

            dataLakeSasBuilder.SetPermissions(DataLakeFileSystemSasPermissions.Read);

            DataLakeUriBuilder dataLakeUriBuilder = new DataLakeUriBuilder(file.Uri)
            {
                Sas = dataLakeSasBuilder.ToSasQueryParameters(sharedKeyCredential)
            };

            DataLakeFileSystemClient sasFileSystemClient = new DataLakeFileSystemClient(dataLakeUriBuilder.ToUri());

            // To debug writing the SAS URL to a file in project dir
            Console.WriteLine(sasFileSystemClient);
            string name = sasFileSystemClient.Uri.ToString();
            File.WriteAllTextAsync("WriteLines.txt", name);

            //**********end************

            // Cleanup
            //await filesystem.DeleteAsync();

        }

    }
}

这是单个文件 my-file 的实际 Blob SAS URL 的外观:

https://kteststorageeadls.dfs.core.windows.net/my-filesystem-dfs/my-dir/my-file?sv=2020-08-04&st=2021-10-13T15%3A39%3A26Z&se=2021-10-13T17%3A36%3A26Z&sr=c&sp=r&sig=APeC5UTh7fEkzL5czAM%2B124XXE9zw%2FpTJLuvlqVQEko%3D

要为文件生成 SAS URL,您要调用的方法是 DataLakeFileSystemClient.GenerateSasUri

下面是生成 SAS URI 的示例代码:

public async Task SetPermissions_Filesystem(DataLakeFileSystemSasPermissions permissions)
{
    // Arrange
    await using DisposingFileSystem test = await GetNewFileSystem();

    DataLakeSasBuilder dataLakeSasBuilder = new DataLakeSasBuilder
    {
        StartsOn = Recording.UtcNow.AddHours(-1),
        ExpiresOn = Recording.UtcNow.AddHours(1),
        FileSystemName = test.FileSystem.Name
    };

    dataLakeSasBuilder.SetPermissions(permissions);

    StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(Tenants.TestConfigHierarchicalNamespace.AccountName, Tenants.TestConfigHierarchicalNamespace.AccountKey);

    DataLakeUriBuilder dataLakeUriBuilder = new DataLakeUriBuilder(test.FileSystem.Uri)
    {
        Sas = dataLakeSasBuilder.ToSasQueryParameters(sharedKeyCredential)
    };

    DataLakeFileSystemClient sasFileSystemClient = InstrumentClient(new DataLakeFileSystemClient(dataLakeUriBuilder.ToUri(), GetOptions()));

    // Act
    await foreach (PathItem pathItem in sasFileSystemClient.GetPathsAsync())
    {
        // Just make sure the call succeeds.
    }
}

我从 here 中获取了这段代码。