共享访问从 windowsazure.storage 移动到 Azure.Storage.Files

Shared Access moving from windowsazure.storage to Azure.Storage.Files

我这辈子都不知道如何在新的 sdk 中设置共享访问文件策略。 使用 ShareFileClient 上传后,我需要生成一个 url 供另一个未经身份验证的客户端下载。 使用旧的 SDK,我做了类似

的事情
        var sasConstraints = new SharedAccessFilePolicy() { 
            SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5) , 
            SharedAccessExpiryTime = DateTime.UtcNow.AddDays(10) , 
            Permissions = SharedAccessFilePermissions.Read | SharedAccessFilePermissions.List };
        return myCloudFile.Uri + myCloudFile.GetSharedAccessSignature(sasConstraints);

非常感谢在新 SDK 中完成此操作的任何和所有指示。

据我了解,您想要一个用于 Azure 文件共享的 sas 令牌与新的 Azure 文件 sahre sdk。关于如何创建,请参考以下代码

  1. 安装包
dotnet add package Azure.Storage.Files.Shares
  1. 代码
# create sas token
 string accountname = "<account name>";
 string key = "<storage ket>";
 var creds = new StorageSharedKeyCredential(accountname, key);
var builder = new ShareSasBuilder
 {
    ShareName = "file share name",
    Protocol = SasProtocol.None,
    StartsOn = DateTimeOffset.UtcNow.AddHours(-1),
    ExpiresOn = DateTimeOffset.UtcNow.AddHours(+1)              
};
    builder.SetPermissions(ShareSasPermissions.All);
    var sas = builder.ToSasQueryParameters(creds).ToString();

// test (Upload file)
// Get a reference to a share 
ShareClient share = new ShareClient(connectionString, shareName);

// Get a reference to a directory and create it
ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
directory.Create();

// Get a reference to a file and upload it
ShareFileClient file = directory.GetFileClient(fileName);
using (FileStream stream = File.OpenRead(localFilePath))
{
     await file.CreateAsync(stream.Length);
     var response =await file.UploadRangeAsync(
                    ShareFileRangeWriteType.Update,
                    new HttpRange(0, stream.Length),
                    stream);

     Console.WriteLine(response.GetRawResponse().Status);
}

详情请参考sample