【发布时间】:2015-05-31 12:17:02
【问题描述】:
我想确认我的代码,看看我是否在正确的道路上..
在我看来,我列出了属于该用户的所有 Blob。 用户可以单击其中一个 blob 并立即开始直接从 blob 存储本身下载它。
这里是视图:
@model IEnumerable<Delamapp.CloudStorageServices.UploadEntity>
@foreach (var file in Model)
{
<a href='@Url.Action("DownloadFileTest", "Folder", new { blobName = file.BlobName, fileName = file.FileName, fileExtension = file.FileExtension })'
@file.FileName.PreviewString(file.FileName, file.FileExtension)@file.FileExtension
}
这里是下载功能:
public void DownloadFileTest(string blobName, string fileName, string fileExtension)
{
//Get SAS url
CloudBlobContainer blobContainer = CloudStorageServices.GetCloudBlobsContainer();
CloudBlockBlob blob = blobContainer.GetBlockBlobReference(blobName);
var sas = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
{
SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5),
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(3),
Permissions = SharedAccessBlobPermissions.Read
});
string blobSasUri = (string.Format(CultureInfo.InvariantCulture, "{0}{1}", blob.Uri, sas));
//Download Blob through SAS url
CloudBlockBlob blobSas = new CloudBlockBlob(new Uri(blobSasUri));
using (MemoryStream ms = new MemoryStream())
{
blobSas.DownloadToStream(ms);
byte[] data = new byte[ms.Length];
ms.Position = 0;
ms.Read(data, 0, data.Length);
Response.ContentType = blobSas.Properties.ContentType;
Response.AddHeader("Content-Disposition", "Attachment; filename=" + fileName + fileExtension);
Response.AddHeader("Content-Length", (blobSas.Properties.Length).ToString());
Response.BinaryWrite(ms.ToArray());
Response.End();
}
}
- 我现在是直接从 blob 存储下载吗?
- 在 using (MemoryStream ms = new MemoryStream()) 中,这段代码有什么作用?我在使用不需要的东西吗?我应该使用这些吗? Ms.position、response..等
- 下载方法是公开的,这意味着它可以从 url 调用。如果我想防止这种情况,我可以在开始时使用 linq 方法并检查用户尝试下载的 blob 是否在他/她的帐户中?够了吗?
- 我希望用户能够随时下载自己的 Blob。那么就不需要设置开始时间/结束时间了,对吧?我可以删除那些行吗?
更新
更新存储客户端库后,开始出现以下错误。
发现冲突 -----
1>C:\Program Files (x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets(1635,5): warning MSB3247: Found conflicts between different versions of the same dependent assembly. In Visual Studio, double-click this warning (or select it and press Enter) to fix the conflicts; otherwise, add the following binding redirects to the "runtime" node in the application configuration file: <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="Microsoft.Data.Edm" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-5.6.1.0" newVersion="5.6.1.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="Microsoft.Data.Services.Client" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-5.6.1.0" newVersion="5.6.1.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Spatial" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-5.6.1.0" newVersion="5.6.1.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="Microsoft.Data.OData" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-5.6.1.0" newVersion="5.6.1.0" /></dependentAssembly></assemblyBinding>
【问题讨论】:
标签: azure