[英]Azure Blob Storage - MVC Web Application - Is there a way to upload directly into Azure Blob Storage without going through the MVC web app?
MVC Web应用程序的用户是否有办法避免必须通过MVC应用程序上传文件,并最终使该应用程序将其传输到存储?
换句话说,可以为Web客户端提供适当的SAS令牌,以将其直接上载到Azure Blob存储中的适当位置吗?
我已经看到了客户端应用程序直接复制到Blob存储的示例,但是在Web应用程序上找不到任何内容。 谢谢!
目前,这是不可能的,因为Windows Azure存储不支持CORS。 但是,在\\ Build会议上的一次演讲中,存储团队表示即将到来。 实现此目的的一种方法是仅按@viperguyz的链接中的说明将HTML页面托管在该存储帐户中进行上传,然后使用SAS在该存储帐户中上传blob。 如果需要,可以将自定义域映射到您的Blob存储帐户并使用该域名。 自定义域名的问题在于您将无法使用SSL。
您可以从客户端上载而无需使用JavaScript来访问MVC网站,我已经写了一篇博客文章,并提供了有关如何执行此操作的示例http://blog.dynabyte.se/2013/10/09/uploading-direct-to- windows-azure-blob-storage-from-javascript / 代码位于GitHub
它基于Gaurav Mantris示例,并且通过将JavaScript托管在Blob存储本身上来工作。
当然-这是一个例子:
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
private string UploadFileToBlob(string file)
{
// Retrieve storage account from connection string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the blob client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container
CloudBlobContainer container = blobClient.GetContainerReference("mydeployments");
// Retrieve reference to a blob named "myblob"
var date = DateTime.UtcNow.ToString("yyyyMMdd-hhmmss-");
var fileinfo = new FileInfo(file);
if (fileinfo.Exists)
{
var fileToUpload = new FileInfo(file).Name;
var filename = date + fileToUpload;
try
{
CloudBlob blob = container.GetBlobReference(filename);
// Create or overwrite the "myblob" blob with contents from a local file
using (var fileStream = System.IO.File.OpenRead(file))
{
blob.UploadFromStream(fileStream);
}
return blob.Uri.AbsoluteUri;
}
catch (Exception ex)
{
LogError("Error uploading file to blog: ", ex.Message);
return "";
}
}
LogError("Error - specified file does not exist: ", file);
return "";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.