我有一个基本的Web应用程序在C#MVC(我是新来的MVC)连接到数据库。在那个数据库中有一个包含文件名列表的表格。这些文件存储在Azure存储Blob容器中。C#MVC Web应用服务连接到Azure存储Blob
我用Scaffolding(创建一个控制器和视图)来显示我的文件名表中的数据,并且工作正常。
现在我想将这些文件名连接到blob存储,以便用户可以点击并打开它们。我如何实现这一目标?
我是否编辑索引视图?我是否让用户单击文件名,然后连接到Azure存储以打开该文件?这是如何完成的?
请注意,存储上的文件是私人的,并使用存储密钥进行访问。文件不能公开。
感谢您的任何建议。
[更新]
我已经使用下面的代码实现的共享访问签名(SAS)。
public static string GetSASUrl(string containerName)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
containerPermissions.SharedAccessPolicies.Add("twominutepolicy", new SharedAccessBlobPolicy()
{
SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-1),
SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(2),
Permissions = SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.Read
});
containerPermissions.PublicAccess = BlobContainerPublicAccessType.Off;
container.SetPermissions(containerPermissions);
string sas = container.GetSharedAccessSignature(new SharedAccessBlobPolicy(), "twominutepolicy");
return sas;
}
public static string GetSasBlobUrl(string containerName, string fileName, string sas)
{
// Create new storage credentials using the SAS token.
StorageCredentials accountSAS = new StorageCredentials(sas);
// Use these credentials and the account name to create a Blob service client.
CloudStorageAccount accountWithSAS = new CloudStorageAccount(accountSAS, [Enter Account Name], endpointSuffix: null, useHttps: true);
CloudBlobClient blobClientWithSAS = accountWithSAS.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClientWithSAS.GetContainerReference(containerName);
// Retrieve reference to a blob named "photo1.jpg".
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
return blockBlob.Uri.AbsoluteUri + sas;
}
非常感谢这些信息,帮助我理清了我的问题。最后一个链接稍微过时了,尽管我已经用我认为正确的更新后的代码更新了我的问题。 – SliderUK