如何从网页上传大文件到Azure Blob?

11

在内部,ASP.NET拥有2 GB的寻址空间,但实际上您只有不到1 GB的可用空间用于上传(请参见http://support.microsoft.com/?id=295626)。此外,IIS 7有一个30 MB的限制(请参见http://www.iislogs.com/steveschofield/iis7-post-40-adjusting-file-upload-size-in-iis7),而且你可能需要运行

appcmd set config "My Site/MyApp" -section:requestFiltering -requestLimits.maxAllowedContentLength:104857600 -commitpath:apphost

如何在我的Azure服务器上运行以超越30MB限制的内容?根据http://support.microsoft.com/?id=295626,在上传过程中,ASP.NET会将整个文件加载到内存中,然后用户才能将文件保存到磁盘中。因此,如果许多用户同时上传大文件,我很快就会耗尽内存限制。在下面的代码中,我使用流,但是我猜想整个文件仍然会被先上传到内存中。这是正确的吗?

using System;
using System.Web.Security;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace WebPages
{
    public partial class Upload : System.Web.UI.Page
    {
        CloudBlobClient BlobClient = null;
        CloudBlobContainer BlobContainer = null;

        void InitBlob()
        {
            // Setup the connection to Windows Azure Storage
            var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
            BlobClient = storageAccount.CreateCloudBlobClient();

            // Get and create the container
            BlobContainer = BlobClient.GetContainerReference("publicfiles");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (Membership.GetUser() == null) return;   // Only allow registered users to upload files

            InitBlob();

            try
            {
                var file = Request.Files["Filedata"];

                var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
                BlobClient = storageAccount.CreateCloudBlobClient();

                // Make a unique blob name
                var extension = System.IO.Path.GetExtension(file.FileName);

                // Create the Blob and upload the file
                var blobAddressUri = String.Format("{0}{1}", Guid.NewGuid(), extension);
                var blob = BlobContainer.GetBlobReference(blobAddressUri);

                blob.UploadFromStream(file.InputStream);

                // Set the metadata into the blob
                blob.Metadata["FileName"] = file.FileName;
                //blob.Metadata["Submitter"] = Membership.GetUser().UserName;
                blob.Metadata["Type"] = "Video";
                blob.Metadata["Description"] = "Test";
                blob.SetMetadata();

                // Set the properties
                blob.Properties.ContentType = file.ContentType;
                blob.SetProperties();
            }
            catch(Exception ex)
            {
                System.Diagnostics.Trace.TraceError("Upload file exception: {0}", ex.ToString());
                // If any kind of error occurs return a 500 Internal Server error
                Response.StatusCode = 500;
                Response.Write("An error occured while uploading the file");
                Response.End();
            }
        }
    }
}

我知道非网页上传工具,例如http://azureblobuploader.codeplex.com/,但我确实需要从网页上载。

所以,我的问题是:

  1. 如何从网页上传大于2 GB的文件到 Blob
  2. 如何从网页上传大型文件流而不会占用所有内存
  3. 如果解决方案是编写自己的 HttpModule 或 HttpHandler 来处理上传,如何在 Azure 服务器上安装它?我可以在 Azure 上使用像http://neatupload.codeplex.com/这样的 HttpHandlers 吗?
  4. 此项目不在 SharePoint 上,但是我知道在 SharePoint 中有一种称为 Blob 提供程序的东西,您可以编写自己的提供程序,ASP.NET 是否也有 Blob Providers?

我还可以提到,我上面的代码对小于30 MB的文件默认有效,客户端使用 SWFUpload V2.2.0。

更新 6月19日19:09: Twitter 上的 @YvesGoeleven 给了我一个提示,即使用共享访问签名 (Shared Access Signature),直接将文件上传到 Azure Blob 存储,完全不经过 ASP.NET。我创建了一个返回有效 SAS ut 到我的 Blob 存储的 JSON WCF。

using System.ServiceModel;
using System.ServiceModel.Web;

namespace WebPages.Interfaces
{
    [ServiceContract]
    public interface IUpload
    {
        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json)]
        string GetUploadUrl();
    }
}

--------

using System;
using System.IO;
using System.Runtime.Serialization.Json;
using System.ServiceModel.Activation;
using System.Text;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace WebPages.Interfaces
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class UploadService : IUpload
    {
        CloudBlobClient BlobClient;
        CloudBlobContainer BlobContainer;

        public UploadService()
        {
            // Setup the connection to Windows Azure Storage
            var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
            BlobClient = storageAccount.CreateCloudBlobClient();

            // Get and create the container
            BlobContainer = BlobClient.GetContainerReference("publicfiles");
        }

        string JsonSerialize(string url)
        {
            var serializer = new DataContractJsonSerializer(url.GetType());
            var memoryStream = new MemoryStream();

            serializer.WriteObject(memoryStream, url);

            return Encoding.Default.GetString(memoryStream.ToArray());
        }

        public string GetUploadUrl()
        {
            var sasWithIdentifier = BlobContainer.GetSharedAccessSignature(new SharedAccessPolicy()
            {
                Permissions = SharedAccessPermissions.Write,
                SharedAccessExpiryTime =
                    DateTime.UtcNow.AddMinutes(60)
            });
            return JsonSerialize(BlobContainer.Uri.AbsoluteUri + "/" + Guid.NewGuid() + sasWithIdentifier);
        }
    }
}

它可以工作,但我不能与SWFUpload一起使用,因为它使用HTTP POST动词而不是Azure Blob Storage在创建新的blob项目时所需的HTTP PUT动词。有人知道如何解决这个问题,而不用制作自己的自定义Silverlight或Flash客户端组件来使用HTTP PUT动词吗?我想要一个上传文件时的进度条,因此使用PUT提交的表单不是最佳选择。

对于那些对客户端代码感兴趣(该代码不起作用,因为SWFUpload使用HTTP POST而不是Azure Blob Storage所期望的PUT):

    <div id="header">
        <h1 id="logo"><a href="/">SWFUpload</a></h1>
        <div id="version">v2.2.0</div>
    </div>
    <div id="content">
        <h2>Application Demo (ASP.Net 2.0)</h2>
        <div id="swfu_container" style="margin: 0px 10px;">
            <div>
                <span id="spanButtonPlaceholder"></span>
            </div>
            <div id="divFileProgressContainer" style="height: 75px;"></div>
            <div id="thumbnails"></div>
        </div>
    </div>

 <script type="text/javascript" language="javascript">
        $(document).ready(function () {

            $.ajax({
                url: '/Interfaces/UploadService.svc/GetUploadUrl',
                success: function (result) {
                    var parsedResult = $.parseJSON(result);
                    InitUploadFile(parsedResult);
                }
            });


            function InitUploadFile(uploadUrl) {
                //alert(uploadUrl);
                var swfu = new SWFUpload({
                    // Backend Settings
                    upload_url: uploadUrl,
                    post_params: {
                        "ASPSESSID": "<%=Session.SessionID %>"
                    },

                    // File Upload Settings
                    file_size_limit: "100 MB",
                    file_types: "*.*",
                    file_types_description: "All file types",
                    file_upload_limit: "0",    // Zero means unlimited

                    // Event Handler Settings - these functions as defined in Handlers.js
                    //  The handlers are not part of SWFUpload but are part of my website and control how
                    //  my website reacts to the SWFUpload events.
                    file_queue_error_handler: fileQueueError,
                    file_dialog_complete_handler: fileDialogComplete,
                    upload_progress_handler: uploadProgress,
                    upload_error_handler: uploadError,
                    upload_success_handler: uploadSuccess,
                    upload_complete_handler: uploadComplete,

                    // Button settings
                    button_image_url: "Images/swfupload/XPButtonNoText_160x22.png",
                    button_placeholder_id: "spanButtonPlaceholder",
                    button_width: 160,
                    button_height: 22,
                    button_text: '<span class="button">Select files <span class="buttonSmall">(2 MB Max)</span></span>',
                    button_text_style: '.button { font-family: Helvetica, Arial, sans-serif; font-size: 14pt; } .buttonSmall { font-size: 10pt; }',
                    button_text_top_padding: 1,
                    button_text_left_padding: 5,

                    // Flash Settings
                    flash_url: "Js/swfupload-2.2.0/swfupload.swf", // Relative to this file

                    custom_settings: {
                        upload_target: "divFileProgressContainer"
                    },

                    // Debug Settings
                    debug: false
                });
            }
       });
    </script>

更新时间:6月19日 21:07

我发现SWFUpload是开源的,所以我下载了源代码并将动词从POST更改为PUT,但是很遗憾,Flash Player URLRequestMethod不支持除GET和POST之外的其他动词。我找到了一个据说可以解决问题的方法。

private function BuildRequest():URLRequest {
   // Create the request object
   var request:URLRequest = new URLRequest();
   request.method = URLRequestMethod.POST;
   request.requestHeaders.push(new URLRequestHeader("X-HTTP-Method-Override", "PUT"));

只有在Adobe Air中才能使用,而不能与Flash Player一起使用。

我读过SilverLight 3及以上版本支持HTTP PUT动词,所以我认为我需要编写一些SilverLight代码来实现我的目标。我找到了这个博客文章系列,它可能会对我有所帮助:http://blog.smarx.com/posts/uploading-windows-azure-blobs-from-silverlight-part-1-shared-access-signatures

更新 @ 2011年6月27日:

我现在已经成功地通过使用我基于http://blog.smarx.com/posts/uploading-windows-azure-blobs-from-silverlight-part-1-shared-access-signatures 项目的自定义Silverlight客户端从网页上传大文件(用4.5 GB文件进行了测试)。由于Silverlight同时支持Azure Blob Storage所需的HTTP PUT动词并支持渐进式上传,因此我现在有可能将大文件直接上传到Azure Blob Storage,而不必经过ASP.NET解决方案,我还可以得到一些漂亮的进度条,如果用户希望可以在上传过程中取消。服务器上的内存使用量很小,因为整个文件在放置到Azure Blob Storage之前不会被完全上传。我使用一个共享访问签名(请参见msdn.microsoft.com/en-us/library/ee395415.aspx),该签名是根据请求从WCF RESTful服务提供的。我认为这个解决方案是我们发现的最好的解决方案。谢谢。

更新@ 2011年7月18日:

我创建了一个开源项目,其中包含我在此处找到的内容:

http://azureslfileuploader.codeplex.com/

4个回答

3

我最近也做了同样的事情。我创建了一个Silverlight客户端应用程序来处理数据的切割和发送到Azure。

这个是一个工作示例,我按照它做了。基本上,按照这个示例操作,你的工作几乎就完成了。


3
无论您使用什么代码模式,如果您编写服务器端代码,那么文件将会被发送到您的 webrole 中,然后会出现一些问题,例如角色回收和重试失败的上传。通过客户端 Silverlight 控件,我解决了这些问题,它不仅能够实现容错上传,而且速度很快。您可以下载我的示例并阅读我是如何构建它的:选择您的 Azure 文件上传控件:Silverlight 和 TPL 或 HTML5 和 AJAX

2

-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接