简体   繁体   English

无法将文件上传到Azure存储C#?

[英]Cannot upload a file to Azure Storage C#?

I am trying a simple upload to Azure Storage in C#. 我正在尝试使用C#简单上传到Azure存储。 It works for small files, but when I try to upload a file of 40mb, it just wont upload. 它适用于小文件,但是当我尝试上传40mb的文件时,它不会上传。 I am stuck in upload for about 20 minutes and then I end up cancelling the call. 我在上传中停留了大约20分钟,然后最终取消了通话。

var storageConnectionString = "XXXXXXX";
            var storageAccount = CloudStorageAccount.Parse(storageConnectionString);
            var blobClient = storageAccount.CreateCloudBlobClient();

            //Gets the folder with the MediaID
            CloudBlobContainer videoscontainer = blobClient.GetContainerReference("TestFolder");

            CloudBlockBlob videofile = videoscontainer.GetBlockBlobReference(("test" + ".mp4"));


            //Save blob contents to a temp video file.
            var videostream = new FileStream(@"XXXXXX.mp4", FileMode.Open, FileAccess.Read);
            videofile.UploadFromStream(videostream);
            videostream.Close();

I have a decent internet so I am sure an upload of just 40mb cant takes more than 20 minutes to upload. 我有一个不错的互联网,所以我敢肯定,只有40mb的上传时间不能超过20分钟。 The hardest part is, I don't know what error I am getting. 最难的部分是,我不知道遇到什么错误。 I tried a manual upload in the storage and it finished in about 2 minutes. 我尝试在存储中进行手动上传,但大约2分钟就完成了。

EDIT: I just kept waiting until i got a callback and got this error. 编辑:我一直在等待直到收到回调并收到此错误。

Error: 错误:

"Message": "An error has occurred.",
    "ExceptionMessage": "The request was aborted: The request was canceled.",
    "ExceptionType": "Microsoft.WindowsAzure.Storage.StorageException",
    "StackTrace": "   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext) in c:\\Program Files (x86)\\Jenkins\\workspace\\release_dotnet_master\\Lib\\ClassLibraryCommon\\Core\\Executor\\Executor.cs:line 604\r\n   at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromStreamHelper(Stream source, Nullable`1 length, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext) in c:\\Program Files (x86)\\Jenkins\\workspace\\release_dotnet_master\\Lib\\ClassLibraryCommon\\Blob\\CloudBlockBlob.cs:line 411\r\n   at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromStream(Stream source, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext) in c:\\Program Files (x86)\\Jenkins\\workspace\\release_dotnet_master\\Lib\\ClassLibraryCommon\\Blob\\CloudBlockBlob.cs:line 309\r\n   at StoneFunctions.Controllers.ValuesController.<UploadSingleFile>d__0.MoveNext() in C:\\Users\\Alan\\source\\repos\\Stone\\StoneFunctions\\Controllers\\ValuesController.cs:line 49\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__3`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()",
    "InnerException": {
        "Message": "An error has occurred.",
        "ExceptionMessage": "The request was aborted: The request was canceled.",
        "ExceptionType": "System.Net.WebException",
        "StackTrace": "   at System.Net.ConnectStream.InternalWrite(Boolean async, Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)\r\n   at System.Net.ConnectStream.Write(Byte[] buffer, Int32 offset, Int32 size)\r\n   at Microsoft.WindowsAzure.Storage.Core.ByteCountingStream.Write(Byte[] buffer, Int32 offset, Int32 count) in c:\\Program Files (x86)\\Jenkins\\workspace\\release_dotnet_master\\Lib\\ClassLibraryCommon\\Core\\ByteCountingStream.cs:line 180\r\n   at Microsoft.WindowsAzure.Storage.Core.Util.StreamExtensions.WriteToSync[T](Stream stream, Stream toStream, Nullable`1 copyLength, Nullable`1 maxLength, Boolean calculateMd5, Boolean syncRead, ExecutionState`1 executionState, StreamDescriptor streamCopyState) in c:\\Program Files (x86)\\Jenkins\\workspace\\release_dotnet_master\\Lib\\Common\\Core\\Util\\StreamExtensions.cs:line 169\r\n   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext) in c:\\Program Files (x86)\\Jenkins\\workspace\\release_dotnet_master\\Lib\\ClassLibraryCommon\\Core\\Executor\\Executor.cs:line 670"
    }

I believe the error is coming because of BlobRequestOptions.SingleBlobUploadThresholdInBytes value. 我相信错误是由于BlobRequestOptions.SingleBlobUploadThresholdInBytesBlobRequestOptions.SingleBlobUploadThresholdInBytes This value essentially specifies the maximum size of the blob that can be uploaded without breaking it into chunks. 此值本质上指定了可以上载而不分解成块的Blob的最大大小。 If you don't specify any value for this property, the default value is 128MB . 如果您没有为此属性指定任何值,则默认值为128MB Since you're not setting this explicitly and your blob size is 40MB , Azure Storage SDK is trying to upload this 40MB data in a single request without breaking it into chunks and that request is somehow timing out. 由于未明确设置,并且blob大小为40MB ,Azure Storage SDK尝试在单个请求中上传此40MB数据,而不将其分成多个块,并且该请求以某种方式超时。

To solve this problem, please explicitly set this value to say 2MB or 4MB . 要解决此问题,请将该值明确设置为2MB4MB Then the SDK will break your 40MB file in 20 (or 10) chunks and upload these chunks. 然后,SDK会将您的40MB文件分成20(或10)个块,并上传这些块。

Here's the code I used: 这是我使用的代码:

        var cred = new StorageCredentials(accountName, accountKey);
        var account = new CloudStorageAccount(cred, true);
        CloudBlobClient client = account.CreateCloudBlobClient();
        client.DefaultRequestOptions = new BlobRequestOptions()
        {
            SingleBlobUploadThresholdInBytes = 2 * 1024 * 1024
        };
        var container = client.GetContainerReference("myblobcontainer");
        container.CreateIfNotExists();
        var filePath = @"Full File Path";
        var blob = container.GetBlockBlobReference("blob name.exe");
        using (var fs = new FileStream(filePath, FileMode.Open))
        {
            blob.UploadFromStream(fs);
        }

I uploaded about 60MB file using this code. 我使用此代码上传了大约60MB的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#Azure将文件上载到“文件存储服务” - 而不是Blob存储 - C# Azure Upload Files to “File Storage Service” - not Blob Storage C#Azure存储-如何将文件上传到已经存在的容器 - C# Azure Storage - How to Upload a File to an already Existing Container 如何使用 C# 将文件上传到 Azure Blob 存储? - How to upload file into Azure Blob Storage using C#? C#Azure存储Blob上载TransactionScope - C# Azure Storage Blob Upload TransactionScope 我们可以使用 c# 将文件上传到数据湖存储到 azure 中的嵌套文件夹中吗? - Can we upload a file to datalake storage into a nested folder in azure with c#? 如何使用 Blazor 将大文件上传到 WebAPI (C#) 并将其存储到 Azure Blob 存储中 - How to upload large file using Blazor to WebAPI (C#) and store it into Azure Blob storage 如何将单个变量上传到Azure表存储C# - How to upload a single variable to Azure table storage c# C# 将文件上传到 azure blob 存储中的文件夹中 - C# Upload files into a folder in azure blob storage 如何使用C#将DataTable上传到Azure Blob存储? - How to upload a DataTable to Azure Blob Storage using C#? C# Azure 在慢速 Internet 连接上上传 Blob 存储失败 - C# Azure Blob Storage upload on slow internet connection fail
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM