C# MVC Web 应用服务连接到 Azure 存储 Blob
C# MVC Web App Service Connect to Azure Storage Blob
我有一个用 C# MVC 编写的基本 Web 应用程序(我是 MVC 的新手),它连接到一个数据库。在那个数据库中有一个 table 和一个文件名列表。这些文件存储在 Azure 存储 Blob 容器中。
我已经使用脚手架(创建控制器和视图)来显示来自我的 table 个文件名的数据并且工作正常。
现在我想将这些文件名连接到 blob 存储,以便用户可以单击并打开它们。我该如何实现?
我要编辑索引视图吗?我是否让用户单击文件名然后连接到 Azure 存储以打开该文件?这是怎么做到的?
请注意,存储上的文件是私有的,可以使用存储密钥访问。无法制作文件 public.
感谢任何建议。
[更新]
我已经使用下面的代码实现了共享访问签名 (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;
}
为了访问不是 public 的 blob,您需要使用共享访问签名,这样,您将创建在一段时间内有效的访问令牌(您将选择)并且您还可以通过IP地址进行限制。
更多信息在这里:
https://docs.microsoft.com/en-us/azure/storage/storage-dotnet-shared-access-signature-part-1
因为它们不是 public,您需要在将数据传递到您的视图之前添加一个额外的步骤,即将 SAS 令牌连接到 blob Uri。你可以在这里找到一个很好的例子:http://www.dotnetcurry.com/windows-azure/901/protect-azure-blob-storage-shared-access-signature
我有一个用 C# MVC 编写的基本 Web 应用程序(我是 MVC 的新手),它连接到一个数据库。在那个数据库中有一个 table 和一个文件名列表。这些文件存储在 Azure 存储 Blob 容器中。
我已经使用脚手架(创建控制器和视图)来显示来自我的 table 个文件名的数据并且工作正常。
现在我想将这些文件名连接到 blob 存储,以便用户可以单击并打开它们。我该如何实现?
我要编辑索引视图吗?我是否让用户单击文件名然后连接到 Azure 存储以打开该文件?这是怎么做到的?
请注意,存储上的文件是私有的,可以使用存储密钥访问。无法制作文件 public.
感谢任何建议。
[更新]
我已经使用下面的代码实现了共享访问签名 (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;
}
为了访问不是 public 的 blob,您需要使用共享访问签名,这样,您将创建在一段时间内有效的访问令牌(您将选择)并且您还可以通过IP地址进行限制。
更多信息在这里:
https://docs.microsoft.com/en-us/azure/storage/storage-dotnet-shared-access-signature-part-1
因为它们不是 public,您需要在将数据传递到您的视图之前添加一个额外的步骤,即将 SAS 令牌连接到 blob Uri。你可以在这里找到一个很好的例子:http://www.dotnetcurry.com/windows-azure/901/protect-azure-blob-storage-shared-access-signature