【发布时间】:2017-11-01 21:22:21
【问题描述】:
我有一个连接到数据库的 C# MVC 基本 Web 应用程序(我是 MVC 新手)。在该数据库中有一个包含文件名列表的表。这些文件存储在 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;
}
【问题讨论】:
标签: c# asp.net-mvc azure azure-storage azure-blob-storage