首页 > 解决方案 > Error using presigned URL for uploading data from Blazor WASM app to AWS S3 bucket

问题描述

I have followed and then modified the code given in the following sources

Lambda code

Front end code

CORS

General walkthrough

But when I try and upload an image using the code below...

public void UploadObject(Stream jpeg_image)
{
    var buffer = new byte[8000];
    HttpWebRequest httpRequest = WebRequest.Create(URL) as HttpWebRequest;
    httpRequest.Method = "PUT";
    using (Stream dataStream = httpRequest.GetRequestStream())
    {
        int bytesRead = 0;
        while ((bytesRead = jpeg_image.Read(buffer, 0, buffer.Length)) > 0)
        {
            dataStream.Write(buffer, 0, bytesRead);
        }
    }
    HttpWebResponse response = httpRequest.GetResponse() as HttpWebResponse;
}

I get hit with the following error from the console in my Blazor web page

blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
          Unhandled exception rendering component: Operation is not supported on this platform.
    System.PlatformNotSupportedException: Operation is not supported on this platform.
      at System.Net.WebProxy.CreateDefaultProxy () <0x366ff48 + 0x00008> in <filename unknown>:0 
      at System.Net.Configuration.DefaultProxySectionInternal.GetSystemWebProxy () <0x366fe28 + 0x00000> in <filename unknown>:0 
      at System.Net.Configuration.DefaultProxySectionInternal.GetDefaultProxy_UsingOldMonoCode () <0x366fd80 + 0x00000> in <filename unknown>:0 
      at System.Net.Configuration.DefaultProxySectionInternal.GetSection () <0x366fb80 + 0x0002a> in <filename unknown>:0 
      at System.Net.WebRequest.get_InternalDefaultWebProxy () <0x366f8c8 + 0x00034> in <filename unknown>:0 
      at System.Net.HttpWebRequest..ctor (System.Uri uri) <0x366d198 + 0x000b6> in <filename unknown>:0 
      at System.Net.HttpRequestCreator.Create (System.Uri uri) <0x366c7d8 + 0x00004> in <filename unknown>:0 
      at System.Net.WebRequest.Create (System.Uri requestUri, System.Boolean useUriBase) <0x366b498 + 0x00116> in <filename unknown>:0 
      at System.Net.WebRequest.Create (System.String requestUriString) <0x366b0e0 + 0x00024> in <filename unknown>:0 
      at ----.UploadPresigned.UploadObject (System.IO.Stream jpeg_image) [0x0000c] in C:\Users\----\Documents\----\----\DataForm.cs:78 
      at ----.Pages.DataUpload.HandleValidSubmit () [0x002b6] in C:\Users\----\Documents\----\----\Pages\DataUpload.razor:252 
      at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion (System.Threading.Tasks.Task task) <0x36b85b0 + 0x000da> in <filename unknown>:0 
      at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask (System.Threading.Tasks.Task taskToHandle) <0x30d7510 + 0x000b6> in <filename unknown>:0 

For clarity, line 78 is the following line

HttpWebRequest httpRequest = WebRequest.Create(URL) as HttpWebRequest;

Note that I've put dashes in the stacktrace to preserve personal information.

I've previously been notified that multi-threaded things don't work will in Blazor (see comments to an answer in this question as well as this thread from GitHub so I'm hypothesising that the problem might be coming from the httpRequest having some multi-threaded backend, since the final two lines in the stack trace mention the System.Threading library.

This link here seems to have a similar issue, but I'm not sure how to port the fix to my code. I have included the project file below

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
    <RazorLangVersion>3.0</RazorLangVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="AWSSDK.Core" Version="3.5.3.8" />
    <PackageReference Include="AWSSDK.S3" Version="3.5.9.7" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.1" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.1" PrivateAssets="all" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.1" PrivateAssets="all" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
    <PackageReference Include="System.IO.Compression.ZipFile" Version="4.3.0" />
    <PackageReference Include="System.Net.Http.Json" Version="3.2.0" />
    <PackageReference Include="BlazorInputFile" Version="0.2.0" />
  </ItemGroup>

</Project>

Any advice on how to fix this error? Please let me know if I need to provide more information on how my app works.

标签: amazon-web-servicesamazon-s3aws-lambdablazorblazor-webassembly

解决方案


推荐阅读