将大型zip文件作为块上载到Azure blob

时间:2017-02-17 09:41:51

标签: azure file-upload azure-storage-blobs

我正在尝试在块中的天蓝色blob上传大文件。该代码适用于其他文件,但每当我上传zip或rar文件时,我都会收到错误。我的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.RetryPolicies;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using System.IO;
using Ionic.Zip;
using System.Text;
using System.Globalization;

namespace zip_unzip
{
    public partial class _default : System.Web.UI.Page
    {
    public static string accountname = "xxx";
    public static string accesskey = "xxxxxxxxxxxxxxxxx";
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string Msg = string.Empty;
        try
        {
            StorageCredentials creden = new StorageCredentials(accountname, accesskey);
            CloudStorageAccount acc = new CloudStorageAccount(creden, useHttps: true);
            CloudBlobClient client = acc.CreateCloudBlobClient();
            CloudBlobContainer cont = client.GetContainerReference("zipfile");
            cont.CreateIfNotExists();
            cont.SetPermissions(new BlobContainerPermissions

            {
                PublicAccess = BlobContainerPublicAccessType.Blob

            });

            CloudBlockBlob cblob = cont.GetBlockBlobReference(upldfile.PostedFile.FileName.ToString());
            int maxSize = 1 * 1024 * 1024; // 1 MB

            if (upldfile.PostedFile.ContentLength > maxSize)
            {
                byte[] data = upldfile.FileBytes;
                byte[] bId;
                int id = 0;
                int byteslength = data.Length;
                int bytesread = 0;
                int index = 0;
                List<string> blocklist = new List<string>();
                int numBytesPerChunk = 250*1024; //250KB per block

                do
                {
                    byte[] buffer = new byte[numBytesPerChunk];
                    int limit = index + numBytesPerChunk;
                    for (int loops = 0; index < limit; index++)
                    {
                        buffer[loops] = data[index];
                        loops++;
                    }
                    bytesread = index; 
                    bId = Guid.NewGuid().ToByteArray();
                    string blockIdBase64 = Convert.ToBase64String(bId);                        
                    cblob.PutBlock(blockIdBase64, new MemoryStream(buffer, true), null);
                    blocklist.Add(blockIdBase64);
                    id++;
                } while (byteslength - bytesread > numBytesPerChunk);

                int final = byteslength - bytesread;
                byte[] finalbuffer = new byte[final];
                for (int loops = 0; index < byteslength; index++)
                {
                    finalbuffer[loops] = data[index];
                    loops++;
                }
                string blockId = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
                cblob.PutBlock(blockId, new MemoryStream(finalbuffer, true), null);
                //blockIdBase64
                blocklist.Add(blockId);

                cblob.PutBlockList(blocklist);
            }
            else
                cblob.UploadFromStream(upldfile.FileContent);


        }
        catch (Exception ex)
        {
            //Label1.Text = "Error: " + ex.Message;
            throw;
        }

    }




}
}

上传Zip文件时出错如下:     远程服务器返回错误:(400)错误请求。

**Stack Trace:**

[WebException: The remote server returned an error: (400) Bad Request.]
   System.Net.HttpWebRequest.GetResponse() +1390
   Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync(RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:677

[StorageException: The remote server returned an error: (400) Bad Request.]
   Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync(RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:604
   Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.PutBlock(String blockId, Stream blockData, String contentMD5, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlockBlob.cs:1549
   zip_unzip._default.btnUpload_Click(Object sender, EventArgs e) in C:\Users\kartikeyap\Documents\Visual Studio 2015\Projects\ToBlob\ToBlob\default.aspx.cs:97
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9690930
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +108
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3562

0 个答案:

没有答案
相关问题