当 IsSuccess = true 时,Azure IOT 设备客户端方法 CompleteFileUploadAsync 给出错误 403 作为禁止,适用于 IsSuccess = false

问题描述

我为 iot hub 和 azure 存储帐户启用了私有端点并限制了公共流量,当我上传文件时,它成功上传,但在调用设备客户端的 CompleteFileUploadAsync 方法时,我收到错误 403 为禁止。但是当传递 IsSuccess = false 时,它​​可以使用当前配置,我将允许访问从“所有网络”更改为“所有网络”,然后它就可以工作了。

我想确保它在文件上传成功时在专用网络中通知,当文件上传失败时它可以正常工作。

代码

private static async Task SendToBlobAsync(DeviceClient deviceClient,string fileName)
        {
            Console.WriteLine("Uploading file: {0}",fileName);
            var watch = System.Diagnostics.Stopwatch.StartNew();

           var sas =  await deviceClient.GetFileUploadSasUriAsync(new FileUploadSasUriRequest { BlobName = fileName });
            var blob = new CloudBlockBlob(sas.GetBlobUri());
            await blob.UploadFromFileAsync(fileName);
            await deviceClient.CompleteFileUploadAsync(new FileUploadCompletionNotification { CorrelationId = sas.CorrelationId,IsSuccess = true });

            watch.Stop();
            Console.WriteLine("Time to upload file: {0}ms\n",watch.ElapsedMilliseconds);
        }

抛出以下错误消息的方法

{
   "Message":"{\"errorCode\":400020,\"trackingId\":\"72f35ff2b08f4312bc0e9f9d36be1061-G:0-TimeStamp:02/23/2021 14:24:46\",\"message\":\"BadRequest\",\"timestamputc\":\"2021-02-23T14:24:46.3420546Z\"}","ExceptionMessage":"ErrorCode: InvalidStorageEndpointOrBlob\r\nMessage: Storage endpoint '$default' or blob name device001/image.jpg is invalid\r\nTimestamp: 02/23/2021 14:24:46\r\nTracking ID: 72f35ff2b08f4312bc0e9f9d36be1061-G:0-TimeStamp:02/23/2021 14:24:46\r\nPublic Message: \r\nPublic Info: null\r\nException: Microsoft.Azure.Devices.Common.Core.Exceptions.IotHubException: Storage endpoint '$default' or blob name device001/image.jpg is invalid ---> Microsoft.WindowsAzure.Storage.StorageException: The Remote Server returned an error: (403) Forbidden. ---> System.Net.WebException: The Remote Server returned an error: (403) Forbidden.\r\n   at Microsoft.WindowsAzure.Storage.Shared.Protocol.HttpResponseParsers.ProcessExpectedStatusCodeNoException[T](HttpStatusCode expectedStatusCode,HttpStatusCode actualStatusCode,T retVal,StorageCommandBase`1 cmd,Exception ex)\r\n   at Microsoft.WindowsAzure.Storage.Blob.CloudBlob.<>c__displayClass22.<FetchAttributesImpl>b__21(restcommand`1 cmd,HttpWebResponse resp,Exception ex,OperationContext ctx)\r\n   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.EndGetResponse[T](IAsyncResult getResponseResult)\r\n   --- End of inner exception stack trace ---\r\n   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.EndExecuteAsync[T](IAsyncResult result)\r\n   at Microsoft.WindowsAzure.Storage.Core.Util.AsyncExtensions.<>c__displayClass7.<CreateCallbackVoid>b__5(IAsyncResult ar)\r\n--- End of stack trace from prevIoUs location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.Azure.Devices.Gateway.Runtime.FileUpload.IotHubFileUploadManager.<GenerateFileUploadNotificationAsync>d__10.MoveNext() in C:\\source\\gateway\\runtime\\FileUpload\\IotHubFileUploadManager.cs:line 153\r\n   --- End of inner exception stack trace ---\r\n   at Microsoft.Azure.Devices.Gateway.Runtime.FileUpload.IotHubFileUploadManager.<GenerateFileUploadNotificationAsync>d__10.MoveNext() in C:\\source\\gateway\\runtime\\FileUpload\\IotHubFileUploadManager.cs:line 180\r\n--- End of stack trace from prevIoUs location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.Azure.Devices.Gateway.Runtime.Http.HttpRuntimeProvider.<UpdateAndNotifyFileUploadStatusAsync>d__41.MoveNext() in C:\\source\\gateway\\runtime\\Http\\HttpRuntimeProvider.cs:line 1215\r\n--- End of stack trace from prevIoUs location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.Azure.Devices.Gateway.Runtime.Http.HttpRuntimeActionResult`1.<ExecuteAsync>d__7.MoveNext() in C:\\source\\gateway\\mgmt\\Http\\HttpRuntimeActionResult.cs:line 153\r\n--- End of stack trace from prevIoUs location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__1.MoveNext()\r\n--- End of stack trace from prevIoUs location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.Azure.Devices.Gateway.Management.ActionFilters.GlobalActionFilter.<ExecuteActionFilterasync>d__0.MoveNext() in C:\\source\\gateway\\mgmt\\ActionFilters\\GlobalActionFilter.cs:line 42\r\n--- End of stack trace from prevIoUs location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()\r\n--- End of stack trace from prevIoUs location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.AuthorizationFilterattribute.<ExecuteAuthorizationFilterasyncCore>d__3.MoveNext()\r\n--- End of stack trace from prevIoUs location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__6.MoveNext()","ExceptionType":"Microsoft.Azure.Devices.Common.Core.Exceptions.IotHubException","StackTrace":"   at Microsoft.Azure.Devices.Gateway.Runtime.FileUpload.IotHubFileUploadManager.<GenerateFileUploadNotificationAsync>d__10.MoveNext() in C:\\source\\gateway\\runtime\\FileUpload\\IotHubFileUploadManager.cs:line 180\r\n--- End of stack trace from prevIoUs location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.Azure.Devices.Gateway.Runtime.Http.HttpRuntimeProvider.<UpdateAndNotifyFileUploadStatusAsync>d__41.MoveNext() in C:\\source\\gateway\\runtime\\Http\\HttpRuntimeProvider.cs:line 1215\r\n--- End of stack trace from prevIoUs location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.Azure.Devices.Gateway.Runtime.Http.HttpRuntimeActionResult`1.<ExecuteAsync>d__7.MoveNext() in C:\\source\\gateway\\mgmt\\Http\\HttpRuntimeActionResult.cs:line 153\r\n--- End of stack trace from prevIoUs location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__1.MoveNext()\r\n--- End of stack trace from prevIoUs location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.Azure.Devices.Gateway.Management.ActionFilters.GlobalActionFilter.<ExecuteActionFilterasync>d__0.MoveNext() in C:\\source\\gateway\\mgmt\\ActionFilters\\GlobalActionFilter.cs:line 42\r\n--- End of stack trace from prevIoUs location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()\r\n--- End of stack trace from prevIoUs location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.AuthorizationFilterattribute.<ExecuteAuthorizationFilterasyncCore>d__3.MoveNext()\r\n--- End of stack trace from prevIoUs location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__6.MoveNext()","InnerException":{
      "Message":"An error has occurred.","ExceptionMessage":"The Remote Server returned an error: (403) Forbidden.","ExceptionType":"Microsoft.WindowsAzure.Storage.StorageException","StackTrace":"   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.EndExecuteAsync[T](IAsyncResult result)\r\n   at Microsoft.WindowsAzure.Storage.Core.Util.AsyncExtensions.<>c__displayClass7.<CreateCallbackVoid>b__5(IAsyncResult ar)\r\n--- End of stack trace from prevIoUs location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.Azure.Devices.Gateway.Runtime.FileUpload.IotHubFileUploadManager.<GenerateFileUploadNotificationAsync>d__10.MoveNext() in C:\\source\\gateway\\runtime\\FileUpload\\IotHubFileUploadManager.cs:line 153","InnerException":{
         "Message":"An error has occurred.","ExceptionType":"System.Net.WebException","StackTrace":"   at Microsoft.WindowsAzure.Storage.Shared.Protocol.HttpResponseParsers.ProcessExpectedStatusCodeNoException[T](HttpStatusCode expectedStatusCode,OperationContext ctx)\r\n   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.EndGetResponse[T](IAsyncResult getResponseResult)"
      }
   }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)