[英]How to provide security for blob storage in Windows Azure
我想提供对Windows Azure
Blob存储的访问,这样,只有一个域可以访问container
的Blob。 我发现有对容器的公共,私有和共享访问。 但是它们不符合我的要求。 在公共场合,我们可以随时通过url访问blob。 私下里,如果我们提供存储帐户凭据,则不能从url访问,但可以从代码访问。 在共享访问中,我们可以根据策略访问一段时间。
但是我需要我的网站应该访问来自blob-storage
任何Blob(可能来自url或来自代码)。 但是我不应该仅通过粘贴blob的URL从其他浏览器访问它。 因此,除非并且直到我未登录我的应用程序,否则我将无法访问blob存储URL。 请让我知道我们如何实现这一目标。
实现此目的的最简单方法是利用有效期很短(30秒或更短)的Shared Access Signature (SAS)
)。 如您所知,SAS提供了对存储资源的基于时间限制/权限的访问。 因此,您可以做的是将blob容器的ACL保持Private
而不是Blob/Container
。
假设您正在使用.Net客户端库并构建MVC应用程序,则在控制器中要做的就是在具有Read
权限的容器上创建一个SAS
,该权限在很短的时间内有效,并将该SAS令牌传递给您的视图。 然后,您可以将该SAS令牌附加到视图中的图像URL。
控制器代码:
var cloudStorageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
var container = cloudStorageAccount.CreateCloudBlobClient().GetContainerReference("container");
var sas = container.GetSharedAccessSignature(new SharedAccessBlobPolicy()
{
SharedAccessExpiryTime = DateTime.UtcNow.AddSeconds(30),
Permissions = SharedAccessBlobPermissions.Read
});
ViewBag.SasToken = sas;
查看代码:
<img src="https://myaccount.blob.core.windows.net/container/myimage.png@Html.Raw(ViewBag.SasToken)" />
请注意,这并不是百分百的万无一失,因为在SAS有效的时间内,任何人都可以复制URL并获取图像,但是可以通过保持SAS的有效期限来缓解此问题,因为一旦SAS过期,即使有人具有无法访问Blob的URL。
我找到了解决方法。 我将容器访问级别设置为“ 私有”,并在我的应用程序中编写HTTP处理程序以检索blob文件,在其中我在ProcessRequest方法中编写了如下所示的逻辑。
处理程序
public void ProcessRequest(HttpContext context)
{
// Get the file name.
string fileName = string.Empty;
string blobContainerName = string.Empty;
if (HttpContext.Current.Session["UserName"] != null)
{
if (context.Request.QueryString["filename"] != null)
{
fileName = context.Request.QueryString["filename"];
}
if (context.Request.QueryString["source"] != null)
{
blobContainerName = context.Request.QueryString["source"];
}
// Get the blob from blob storage.
var storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
var blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(blobContainerName);
// Retrieve reference to a blob named "FlexpathBlob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
// Read blob content to response.
context.Response.Clear();
try
{
blockBlob.FetchAttributes();
context.Response.ContentType = blockBlob.Properties.ContentType;
blockBlob.DownloadToStream(context.Response.OutputStream);
}
catch (Exception ex)
{
context.Response.Write(ex.ToString());
}
context.Response.End();
}
else {
context.Response.Write("You are not authorized to view this file.");
}
}
并从类似的视图访问
视图
<img id="imgVehicleImage1790" src="/Handlers/FileReciever.ashx?source=dealerinventory&filename=1790_3b733b4e-b692-4650-95a6-855eb55145c4.png" style="border-width: 0px;width:208px;height:126px;">
这对我来说很好。 我可以访问所有图像和其他Blob文件。 当我尝试从Blob网址进行访问时,显示未经授权的访问。 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.