我正在尝试使用 C# 简单上传到 Azure 存储。它适用于小文件,但当我尝试上传 40mb 的文件时,它就无法上传。我在上传过程中卡住了大约 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();
我有一个不错的互联网,所以我确信上传 40mb 的文件需要 20 多分钟才能上传。最困难的部分是,我不知道我遇到了什么错误。我尝试在存储中手动上传,大约 2 分钟就完成了。
编辑:我一直在等待,直到收到回调并收到此错误。
错误:
"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"
}
最佳答案
我相信错误是因为 BlobRequestOptions.SingleBlobUploadThresholdInBytes
出现的值(value)。该值本质上指定了可以上传的 blob 的最大大小,而无需将其分成 block 。如果您没有为此属性指定任何值,则默认值为 128MB
。由于您没有明确设置此项,并且您的 blob 大小为 40MB
,因此 Azure 存储 SDK 尝试在单个请求中上传此 40MB 数据,而不将其分解为 block ,并且该请求不知何故超时。
要解决此问题,请明确将此值设置为 2MB
或 4MB
。然后,SDK 会将您的 40MB 文件分成 20(或 10)个 block 并上传这些 block 。
这是我使用的代码:
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);
}
我使用此代码上传了大约 60MB 的文件。
关于c# - 无法将文件上传到 Azure 存储 C#?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50198037/