[英]Accessing the file stored in Azure file storage without SAS token
使用Azure云文件共享API,我能够创建文件共享以及目录,文件夹和文件。 它们都正确保存在文件共享中。 从浏览器中,我可以使用URI + SAS令牌(为文件共享生成)查看内容。 因此,在.NET中,访问Azure文件的最佳方法是什么? 没有SAS令牌,我们能否访问文件(不是blob)? SAS令牌是强制性的吗? 如果是这样,什么时候才是合适的时间生成令牌? (每次访问文件时?)
有3种访问存储的方式:
1)使用主/根密钥。 我不建议使用这种方法,因为主密钥可以完全访问存储帐户。
2)使用SAS密钥。 这是一个很好的方法,因为您可以限制密钥给定的访问量。 请参阅本网站的最佳做法部分。 您需要先生成密钥,然后才能访问存储并将其安全地存储在配置中。 请注意,斑点是您的文件。 Blob被组织到Blob存储容器中,然后这些容器位于您的存储帐户中。
3)如果您要使用新的Azure Data Lake Gen 2存储(ADLS Gen 2),则可以将Azure Active Directory身份验证与服务帐户或托管服务身份(MSI)结合使用。 请注意,ADLS Gen 2仍在预览中。
如果尝试访问/操作c#/。net中的文件共享,则不是必须使用SAS令牌。
您可以仅使用存储帐户名称和帐户密钥进行身份验证,然后按照此官方文档分别操作文件共享,例如分别创建/删除文件共享/目录/文件。
除了将帐户名/密钥存储在上述文档中提到的配置文件中之外,您还可以在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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.