【问题标题】:Azure Blob Storage - MVC Web Application - Is there a way to upload directly into Azure Blob Storage without going through the MVC web app?Azure Blob 存储 - MVC Web 应用程序 - 有没有办法直接上传到 Azure Blob 存储而不通过 MVC Web 应用程序?
【发布时间】:2012-12-26 14:16:17
【问题描述】:

有没有办法让 MVC Web 应用程序的用户避免通过 MVC 应用程序上传文件并最终让应用程序将其传输到存储中?

换句话说,是否可以为 Web 客户端提供正确的 SAS 令牌以将其直接上传到 Azure Blob 存储中的正确位置?

我看到了客户端应用程序直接复制到 Blob 存储的示例,但在 Web 应用程序上找不到任何内容。谢谢!

【问题讨论】:

标签: c# azure storage blob


【解决方案1】:

目前不可能,因为 Windows Azure 存储不支持 CORS。然而,在 \Build 会议存储团队的一次演讲中表示它即将到来。实现此目的的一种方法是仅按照@viperguyz 的链接中提到的那样在该存储帐户中托管用于上传的 HTML 页面,并使用 SAS 在该存储帐户中上传 blob。如果需要,您可以将自定义域映射到您的 Blob 存储帐户并使用该域名。自定义域名的问题是您将无法使用 SSL。

【讨论】:

【解决方案2】:

您可以从客户端上传,而无需使用 JavaScript 接触 MVC 站点,我已经写了一篇博客文章,其中包含有关如何执行此操作的示例 http://blog.dynabyte.se/2013/10/09/uploading-directly-to-windows-azure-blob-storage-from-javascript/ code is at GitHub

它基于Gaurav Mantris example,并通过在 Blob 存储本身上托管 JavaScript 来工作。

【讨论】:

    【解决方案3】:

    当然 - 这是一个例子:

    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 "";
        }
    

    【讨论】:

    • 您的代码仍会将文件传递给 web 角色,然后 web 角色需要将该文件传递给 azure blob 存储。这就是我要避免的。我想避免通过 Web 角色传递它,而是将它直接从客户端传递到 azure blob 存储。这可能不可能,但我想我会看看是否有人有任何创意。我将研究节点并查看..
    猜你喜欢
    • 2020-09-21
    • 2017-11-01
    • 2018-05-27
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 2020-06-22
    相关资源
    最近更新 更多