将 zip 从 Blazor WASM 应用程序上传到 AWS-S3 - 遵循教程,没有观察到错误,但存储桶中也没有文件 [英] Uploading a zip from from Blazor WASM app to AWS-S3 - tutorials followed and no errors observed, but also no file in the bucket

查看:12
本文介绍了将 zip 从 Blazor WASM 应用程序上传到 AWS-S3 - 遵循教程,没有观察到错误,但存储桶中也没有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Blazor WASM 应用程序(即客户端),用户可以将一些数据上传到该应用程序.在对数据进行一些完整性和安全性检查后,我想将这些数据上传到 AWS S3 中的 zip 文件中.

目前我已经按如下方式构建了 zip 文件

using (var outStream = new MemoryStream()){使用 (var archive = new ZipArchive(outStream, ZipArchiveMode.Create, true)){foreach(imagelist.Files 中的 var 文件){json_name = file.Name + ".json";var file_in_archive = archive.CreateEntry(file.Name, CompressionLevel.Optimal);使用 (var entryStream = file_in_archive.Open()){file.Data.CopyToAsync(entryStream);}file_in_archive = archive.CreateEntry(json_name, CompressionLevel.Optimal);使用 (var entryStream = file_in_archive.Open()){formMems[file_counter].CopyToAsync(entryStream);}文件计数器 = 文件计数器 + 1;}}Console.WriteLine(完成压缩");uploader.UploadZipFile(outStream, test_title.zip");}

我正在尝试通过这种方法上传 zip 文件 uploader.UploadZipFile - 代码如下

公共类 UploadZip{public BasicAWSCredentials 凭证 = new BasicAWSCredentials("access key", "secret key");public void UploadZipFile(Stream zip_file, string file_name){zip_file.Seek(0, SeekOrigin.Begin);var config = 新的 AmazonS3Config{RegionEndpoint = Amazon.RegionEndpoint.Region};使用 var client = new AmazonS3Client(credentials, config);var uploadRequest = new TransferUtilityUploadRequest{InputStream = zip_file,键 = 文件名,BucketName = "Bucket-Name",CannedACL = S3CannedACL.Private};var fileTransferUtility = new TransferUtility(client);fileTransferUtility.UploadAsync(uploadRequest);Console.WriteLine(完成上传");}}

(上传器是 UploadZip 的一个实例).出于显而易见的原因,我删除了存储桶名称、区域、访问密钥和密钥,但这些都是正确的.

访问密钥和秘密密钥属于我专门为此目的创建的自定义 IAM 用户.政策摘要如下

<代码>{版本":2012-10-17",声明":[{Sid":VisualEditor0",效果":允许",动作":s3:PutObject",资源":arn:aws:s3::::Bucket-Name/*"}]

}

并且 S3 存储桶已Block *all* public access关闭,CORS 如下

<预><代码>[{允许的标题":[*"],允许的方法":[放置",邮政"],允许的起源":[*"],ExposeHeaders":[]}

]

ACL 授予 Bucket 所有者(您的 AWS 账户)ListWrite 用于 ObjectsRead存储桶ACL编写,其他3个受赠者没有其他内容.没有桶策略.

我只是不明白为什么这不起作用.当我运行程序时,我看不到任何错误,并且两个 Console.write() 调用运行良好.

编辑

为了回应评论中的建议,我尝试使用 Upload 而不是 UploadAsync 运行代码 - 见下文

公共类 UploadZip{public BasicAWSCredentials 凭证 = new BasicAWSCredentials(AKIAZWFJ7CFNER3WG5EP", EE9eMD/vz8G3Ui3/MZdGDNBY9xv4+/3y0VQAhNzn");public void UploadZipFile(Stream zip_file, string file_name){zip_file.Seek(0, SeekOrigin.Begin);var config = 新的 AmazonS3Config{RegionEndpoint = Amazon.RegionEndpoint.EUWest2};使用 var client = new AmazonS3Client(credentials, config);var uploadRequest = new TransferUtilityUploadRequest{InputStream = zip_file,键 = 文件名,BucketName = "web-pupil-data-store",CannedACL = S3CannedACL.Private};var fileTransferUtility = new TransferUtility(client);fileTransferUtility.Upload(uploadRequest);Console.WriteLine(完成上传");}}

这仍然完成了文件的压缩,但是在打印完成上传之前,它现在抛出一个错误,其中包含大量且非常难以穿透(对我而言)的堆栈跟踪,复制到这里

blazor.webassembly.js:1 暴击:Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]未处理的异常呈现组件:无法在此运行时启动线程.System.NotSupportedException:无法在此运行时启动线程.at (wrapper managed-to-native) System.Threading.Thread.Thread_internal(System.Threading.Thread,System.MulticastDelegate)在 System.Threading.Thread.StartInternal(System.Object 主体,System.Threading.StackCrawlMark& stackMark)<0x47bb958 + 0x00008>在<文件名未知>:0在 System.Threading.Thread.Start (System.Threading.StackCrawlMark& stackMark) <0x47bb818 + 0x0004e>在<文件名未知>:0在 System.Threading.Thread.Start () <0x47bb6d8 + 0x0000e>在<文件名未知>:0在 Amazon.Runtime.Internal.Util.BackgroundDispatcher`1[T]..ctor (System.Action`1[T] action) [0x0005c] 在 D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Internal\Util\Dispatcher.cs:55在 Amazon.Runtime.Internal.Util.BackgroundInvoker..ctor () [0x00000] 在 D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Internal\Util\Dispatcher.cs:180在 Amazon.Util.AWSSDKUtils.get_Dispatcher () [0x00007] 在 D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Util\AWSSDKUtils.cs:674在 Amazon.Util.AWSSDKUtils.InvokeInBackground[T] (System.EventHandler`1[TEventArgs] handler, T args, System.Object sender) [0x0004c] 在 D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Util\AWSSDKUtils.cs:661在 Amazon.Runtime.Internal.StreamReadTracker.ReadProgress (System.Int32 bytesRead) [0x00047] 在 D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Internal\StreamReadTracker.cs:55在 Amazon.Runtime.Internal.Util.EventStream.ReadAsync(System.Byte[] 缓冲区,System.Int32 偏移量,System.Int32 计数,System.Threading.CancellationToken 取消令牌)[0x0009c] 在 D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Internal\Util\EventStream.cs:138在 Amazon.Runtime.Internal.Util.ChunkedUploadWrapperStream.FillInputBufferAsync (System.Threading.CancellationToken cancellingToken) [0x00090] 在 D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Internal\Util\ChunkedUploadWrapperStream.cs:189在 Amazon.Runtime.Internal.Util.ChunkedUploadWrapperStream.ReadAsync(System.Byte[] 缓冲区,System.Int32 偏移量,System.Int32 计数,System.Threading.CancellationToken 取消令牌)[0x00053] 在 D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Internal\Util\ChunkedUploadWrapperStream.cs:163在 System.Threading.Tasks.ValueTask`1[TResult].get_Result() <0x370ff80 + 0x00034>在<文件名未知>:0在 System.IO.Stream.CopyToAsyncInternal(System.IO.Stream 目标,System.Int32 bufferSize,System.Threading.CancellationToken 取消令牌)<0x36ee098 + 0x00134>在<文件名未知>:0在 System.Net.Http.HttpContent.LoadIntoBufferAsyncCore (System.Threading.Tasks.Task serializeToStreamTask, System.IO.MemoryStream tempBuffer) <0x49ea648 + 0x00110>在<文件名未知>:0在 System.Net.Http.HttpContent.WaitAndReturnAsync[TState,TResult] (System.Threading.Tasks.Task waitTask, TState state, System.Func`2[T,TResult] returnFunc) <0x49eba60 + 0x000c2>在<文件名未知>:0在 System.Net.Http.WebAssemblyHttpHandler.doFetch (System.Threading.Tasks.TaskCompletionSource`1[TResult] tcs, System.Net.Http.HttpRequestMessage 请求, System.Threading.CancellationToken 取消令牌) <0x47cfe90 + 0x0038c>在<文件名未知>:0在 System.Net.Http.WebAssemblyHttpHandler.SendAsync(System.Net.Http.HttpRequestMessage 请求,System.Threading.CancellationToken 取消令牌)<0x47af880 + 0x00174>在<文件名未知>:0在 System.Net.Http.HttpClient.FinishSendAsyncUnbuffered (System.Threading.Tasks.Task`1[TResult] sendTask, System.Net.Http.HttpRequestMessage 请求, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) <0x49ef528 +0x00134>在<文件名未知>:0在 D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\HttpHandler\_netstandard\HttpRequestMessageFactory 中的 Amazon.Runtime.HttpWebRequestMessage.GetResponseAsync (System.Threading.CancellationToken cancelationToken) [0x0003d].cs:520在 Amazon.Runtime.Internal.HttpHandler`1[TRequestContent].InvokeAsync[T] (Amazon.Runtime.IExecutionContext executionContext) [0x00201] 在 D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\HttpHandler\HttpHandler.cs:183在 Amazon.Runtime.Internal.RedirectHandler.InvokeAsync[T] (Amazon.Runtime.IExecutionContext executionContext) [0x00036] 在 D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\处理程序\重定向处理程序.cs:59在 Amazon.Runtime.Internal.Uf.printErr @ blazor.webassembly.js:1f.preRun.push.window.Blazor._internal.dotNetCriticalError @ blazor.webassembly.js:1_mono_wasm_invoke_js_unmarshalled @ dotnet.3.2.0.js:1do_call @ 00755c3a:0x10f924do_call_wrapper @ 00755c3a:0x50b6ainterp_exec_method @ 00755c3a:0x2588einterp_runtime_invoke @ 00755c3a:0xf7391mono_jit_runtime_invoke @ 00755c3a:0xddb3ddo_runtime_invoke @ 00755c3a:0x3ba85mono_runtime_try_invoke @ 00755c3a:0xcfdbtry_invoke_perform_wait_callback @ 00755c3a:0xb62e0worker_callback @ 00755c3a:0x9a7a3fire_tp_callback @ 00755c3a:0x105481mono_background_exec @ 00755c3a:0xeb5eeModule._mono_background_exec @ dotnet.3.2.0.js:1pump_message @ dotnet.3.2.0.js:1设置超时(异步)_schedule_background_exec @ dotnet.3.2.0.js:1mono_threads_schedule_background_job @ 00755c3a:0x14dbdves_call_System_Threading_ThreadPool_RequestWorkerThread @ 00755c3a:0xa0025ves_call_System_Threading_ThreadPool_RequestWorkerThread_raw @ 00755c3a:0x9fecfdo_call @ 00755c3a:0x10f790do_call_wrapper @ 00755c3a:0x50b6ainterp_exec_method @ 00755c3a:0x2588einterp_runtime_invoke @ 00755c3a:0xf7391mono_jit_runtime_invoke @ 00755c3a:0xddb3ddo_runtime_invoke @ 00755c3a:0x3ba85mono_runtime_try_invoke @ 00755c3a:0xcfdbmono_runtime_invoke @ 00755c3a:0x44b39mono_wasm_invoke_method @ 00755c3a:0xca6a7Module._mono_wasm_invoke_method @ dotnet.3.2.0.js:1call_method @ dotnet.3.2.0.js:1(匿名)@ dotnet.3.2.0.js:1endInvokeJSFromDotNet @ blazor.webassembly.js:1(匿名)@blazor.webassembly.js:1Promise.then(异步)beginInvokeJSFromDotNet @ blazor.webassembly.js:1_mono_wasm_invoke_js_marshalled @ dotnet.3.2.0.js:1do_call @ 00755c3a:0x10f8b1do_call_wrapper @ 00755c3a:0x50b6ainterp_exec_method @ 00755c3a:0x2588einterp_runtime_invoke @ 00755c3a:0xf7391mono_jit_runtime_invoke @ 00755c3a:0xddb3ddo_runtime_invoke @ 00755c3a:0x3ba85mono_runtime_invoke_checked @ 00755c3a:0x96a2mono_runtime_try_invoke_array @ 00755c3a:0x66c26ves_icall_InternalInvoke @ 00755c3a:0xb44ffves_icall_InternalInvoke_raw @ 00755c3a:0xb3ffddo_call @ 00755c3a:0x10f8b1do_call_wrapper @ 00755c3a:0x50b6ainterp_exec_method @ 00755c3a:0x2588einterp_runtime_invoke @ 00755c3a:0xf7391mono_jit_runtime_invoke @ 00755c3a:0xddb3ddo_runtime_invoke @ 00755c3a:0x3ba85mono_runtime_try_invoke @ 00755c3a:0xcfdbmono_runtime_invoke @ 00755c3a:0x44b39mono_wasm_invoke_method @ 00755c3a:0xca6a7Module._mono_wasm_invoke_method @ dotnet.3.2.0.js:1call_method @ dotnet.3.2.0.js:1(匿名)@ dotnet.3.2.0.js:1beginInvokeDotNetFromJS @ blazor.webassembly.js:1s@blazor.webassembly.js:1e.invokeMethodAsync @ blazor.webassembly.js:1(匿名)@blazor.webassembly.js:1t.dispatchEvent @ blazor.webassembly.js:1(匿名)@blazor.webassembly.js:1(匿名)@blazor.webassembly.js:1e.onGlobalEvent @ blazor.webassembly.js:1

请告知我需要做哪些进一步的测试来确定问题所在,以及如何解决.

编辑和更新

根据以下答案之一,我转而使用 PutObjectRequest.代码如下.这现在给出了一个新错误,我认为这与 S3 存储桶的权限有关.任何人都可以指导我需要更改的内容吗?

代码

公共类 UploadData{private readonly BasicAWSCredentials credentials = new BasicAWSCredentials("access key", "secret Key");公共异步任务 UploadZipFile(Stream zip_file, string file_name){zip_file.Seek(0, SeekOrigin.Begin);var config = 新的 AmazonS3Config{RegionEndpoint = Amazon.RegionEndpoint.EUWest2};使用 var client = new AmazonS3Client(credentials, config);var putRequest1 = 新 PutObjectRequest{BucketName = "Bucket_name",键 = 文件名,输入流 = zip_file};PutObjectResponse response1 = await client.PutObjectAsync(putRequest1);Console.WriteLine(完成上传");}}

这是错误代码

在服务器上遇到未知.消息:'TypeError:无法在'Headers'上执行'append':放置对象时的无效值'

解决方案

我对 blazor 和 webassembly 不太了解,但我可以尝试并给您建议:

  1. 上面代码中的问题似乎是 webassembly 中缺乏多线程支持,另见 [1].
  2. aws-sdk-net 中的 TransferUtility 类使用线程进行分段上传,请参阅 [2]:

<块引用>

TransferUtility 提供了一个简单的 API,用于将内容上传到 Amazon S3 和从 Amazon S3 下载内容.它广泛使用 Amazon S3 分段上传来提高吞吐量、性能和可靠性.

通过指定文件路径而不是流上传大文件时,TransferUtility 使用多个线程一次上传单个上传的多个部分.在处理大型内容和高带宽时,这可以显着提高吞吐量.

因此,我会尝试使用一些低级结构来完成使用 PutObjectRequest 的标准上传,请参阅:https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html

如果要上传的文件不是太大,这应该可以正常工作,因为它是单线程方法.

另一个问题是在 Blazor WASM 中启用多线程,但我不确定这是否已经是一个选项,请参阅 [3].

[1] https://www.meziantou.net/don-t-freeze-ui-while-executing-cpu-integrated-work-in-blazor-webassembly.htm
[2] https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/S3/TTransferUtility.html
[3] https://github.com/dotnet/aspnetcore/issues/17730

I have a Blazor WASM app (i.e., client side) that users upload some data to. After some sanity and security checks on the data, I would like to upload these to a zip file in AWS S3.

Currently I have constructed the zip file as follows

using (var outStream = new MemoryStream())
{
    using (var archive = new ZipArchive(outStream, ZipArchiveMode.Create, true))
    {
        foreach (var file in imagelist.Files)
        {
            json_name = file.Name + ".json";
            var file_in_archive = archive.CreateEntry(file.Name, CompressionLevel.Optimal);
            using (var entryStream = file_in_archive.Open())
            {
                file.Data.CopyToAsync(entryStream);
            }
            file_in_archive = archive.CreateEntry(json_name, CompressionLevel.Optimal);
            using (var entryStream = file_in_archive.Open())
            {
                formMems[file_counter].CopyToAsync(entryStream);
            }
            file_counter = file_counter + 1;
        }

    }
    Console.WriteLine("Finished zipping");
    uploader.UploadZipFile(outStream, "test_title.zip");
}

And I am trying to upload the zip file via this method uploader.UploadZipFile - the code is given below

public class UploadZip    
{
    public BasicAWSCredentials credentials = new BasicAWSCredentials("access key", "secret key");
 
    public void UploadZipFile(Stream zip_file, string file_name)
    {
        zip_file.Seek(0, SeekOrigin.Begin);
        var config = new AmazonS3Config
        {
            RegionEndpoint = Amazon.RegionEndpoint.Region
        };
        using var client = new AmazonS3Client(credentials, config);
        var uploadRequest = new TransferUtilityUploadRequest
        {
            InputStream = zip_file,
            Key = file_name,
            BucketName = "Bucket-Name",
            CannedACL = S3CannedACL.Private
        };
        var fileTransferUtility = new TransferUtility(client);
        fileTransferUtility.UploadAsync(uploadRequest);
        Console.WriteLine("finshed uploading");
    }
}

(uploader is an instance of UploadZip). I have removed the bucket name, region, access key and secret key for obvious reasons, but these are correct.

The Access Key and the Secret Key belong to a custom IAM user I made just for this purpose. The Policy summary is given below

{
"Version": "2012-10-17",
"Statement": [
    {
        "Sid": "VisualEditor0",
        "Effect": "Allow",
        "Action": "s3:PutObject",
        "Resource": "arn:aws:s3:::Bucket-Name/*"
    }
]

}

And the S3 bucket has Block *all* public access turned off, the CORS is given below

[
{
    "AllowedHeaders": [
        "*"
    ],
    "AllowedMethods": [
        "PUT",
        "POST"
    ],
    "AllowedOrigins": [
        "*"
    ],
    "ExposeHeaders": []
}

]

The ACL grants the Bucket owner (your AWS account) List and Write for Objects, and Read and Write for Bucket ACL, with nothing else for the other 3 grantees. There is no bucket ploicy.

I simply don't understand why this isn't working. When I run the program I see no errors and the two Console.write() calls run fine.

edit

In response to advice from a comment, I tried running the code with Upload rather than UploadAsync - see below

public class UploadZip    
{
    public BasicAWSCredentials credentials = new BasicAWSCredentials("AKIAZWFJ7CFNER3WG5EP", "EE9eMD/vz8G3Ui3/MZdGDNBY9xv4+/3y0VQAhNzn");
 
    public void UploadZipFile(Stream zip_file, string file_name)
    {
        zip_file.Seek(0, SeekOrigin.Begin);
        var config = new AmazonS3Config
        {
            RegionEndpoint = Amazon.RegionEndpoint.EUWest2
        };
        using var client = new AmazonS3Client(credentials, config);
        var uploadRequest = new TransferUtilityUploadRequest
        {
            InputStream = zip_file,
            Key = file_name,
            BucketName = "web-pupil-data-store",
            CannedACL = S3CannedACL.Private
        };
        var fileTransferUtility = new TransferUtility(client);
        fileTransferUtility.Upload(uploadRequest);
        Console.WriteLine("finshed uploading");
    }
}

This still completes the zipping of the files, but before printing finished uploading, it now throws an error with a large and quite inpenetrable (for me) stack trace, copied here

blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Cannot start threads on this runtime.
System.NotSupportedException: Cannot start threads on this runtime.
  at (wrapper managed-to-native) System.Threading.Thread.Thread_internal(System.Threading.Thread,System.MulticastDelegate)
  at System.Threading.Thread.StartInternal (System.Object principal, System.Threading.StackCrawlMark& stackMark) <0x47bb958 + 0x00008> in <filename unknown>:0 
  at System.Threading.Thread.Start (System.Threading.StackCrawlMark& stackMark) <0x47bb818 + 0x0004e> in <filename unknown>:0 
  at System.Threading.Thread.Start () <0x47bb6d8 + 0x0000e> in <filename unknown>:0 
  at Amazon.Runtime.Internal.Util.BackgroundDispatcher`1[T]..ctor (System.Action`1[T] action) [0x0005c] in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Internal\Util\Dispatcher.cs:55 
  at Amazon.Runtime.Internal.Util.BackgroundInvoker..ctor () [0x00000] in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Internal\Util\Dispatcher.cs:180 
  at Amazon.Util.AWSSDKUtils.get_Dispatcher () [0x00007] in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Util\AWSSDKUtils.cs:674 
  at Amazon.Util.AWSSDKUtils.InvokeInBackground[T] (System.EventHandler`1[TEventArgs] handler, T args, System.Object sender) [0x0004c] in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Util\AWSSDKUtils.cs:661 
  at Amazon.Runtime.Internal.StreamReadTracker.ReadProgress (System.Int32 bytesRead) [0x00047] in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Internal\StreamReadTracker.cs:55 
  at Amazon.Runtime.Internal.Util.EventStream.ReadAsync (System.Byte[] buffer, System.Int32 offset, System.Int32 count, System.Threading.CancellationToken cancellationToken) [0x0009c] in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Internal\Util\EventStream.cs:138 
  at Amazon.Runtime.Internal.Util.ChunkedUploadWrapperStream.FillInputBufferAsync (System.Threading.CancellationToken cancellationToken) [0x00090] in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Internal\Util\ChunkedUploadWrapperStream.cs:189 
  at Amazon.Runtime.Internal.Util.ChunkedUploadWrapperStream.ReadAsync (System.Byte[] buffer, System.Int32 offset, System.Int32 count, System.Threading.CancellationToken cancellationToken) [0x00053] in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Internal\Util\ChunkedUploadWrapperStream.cs:163 
  at System.Threading.Tasks.ValueTask`1[TResult].get_Result () <0x370ff80 + 0x00034> in <filename unknown>:0 
  at System.IO.Stream.CopyToAsyncInternal (System.IO.Stream destination, System.Int32 bufferSize, System.Threading.CancellationToken cancellationToken) <0x36ee098 + 0x00134> in <filename unknown>:0 
  at System.Net.Http.HttpContent.LoadIntoBufferAsyncCore (System.Threading.Tasks.Task serializeToStreamTask, System.IO.MemoryStream tempBuffer) <0x49ea648 + 0x00110> in <filename unknown>:0 
  at System.Net.Http.HttpContent.WaitAndReturnAsync[TState,TResult] (System.Threading.Tasks.Task waitTask, TState state, System.Func`2[T,TResult] returnFunc) <0x49eba60 + 0x000c2> in <filename unknown>:0 
  at System.Net.Http.WebAssemblyHttpHandler.doFetch (System.Threading.Tasks.TaskCompletionSource`1[TResult] tcs, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) <0x47cfe90 + 0x0038c> in <filename unknown>:0 
  at System.Net.Http.WebAssemblyHttpHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) <0x47af880 + 0x00174> in <filename unknown>:0 
  at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered (System.Threading.Tasks.Task`1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) <0x49ef528 + 0x00134> in <filename unknown>:0 
  at Amazon.Runtime.HttpWebRequestMessage.GetResponseAsync (System.Threading.CancellationToken cancellationToken) [0x0003d] in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\HttpHandler\_netstandard\HttpRequestMessageFactory.cs:520 
  at Amazon.Runtime.Internal.HttpHandler`1[TRequestContent].InvokeAsync[T] (Amazon.Runtime.IExecutionContext executionContext) [0x00201] in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\HttpHandler\HttpHandler.cs:183 
  at Amazon.Runtime.Internal.RedirectHandler.InvokeAsync[T] (Amazon.Runtime.IExecutionContext executionContext) [0x00036] in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\Handlers\RedirectHandler.cs:59 
  at Amazon.Runtime.Internal.U
f.printErr  @   blazor.webassembly.js:1
f.preRun.push.window.Blazor._internal.dotNetCriticalError   @   blazor.webassembly.js:1
_mono_wasm_invoke_js_unmarshalled   @   dotnet.3.2.0.js:1
do_icall    @   00755c3a:0x10f924
do_icall_wrapper    @   00755c3a:0x50b6a
interp_exec_method  @   00755c3a:0x2588e
interp_runtime_invoke   @   00755c3a:0xf7391
mono_jit_runtime_invoke @   00755c3a:0xddb3d
do_runtime_invoke   @   00755c3a:0x3ba85
mono_runtime_try_invoke @   00755c3a:0xcfdb
try_invoke_perform_wait_callback    @   00755c3a:0xb62e0
worker_callback @   00755c3a:0x9a7a3
fire_tp_callback    @   00755c3a:0x105481
mono_background_exec    @   00755c3a:0xeb5ee
Module._mono_background_exec    @   dotnet.3.2.0.js:1
pump_message    @   dotnet.3.2.0.js:1
setTimeout (async)      
_schedule_background_exec   @   dotnet.3.2.0.js:1
mono_threads_schedule_background_job    @   00755c3a:0x14dbd
ves_icall_System_Threading_ThreadPool_RequestWorkerThread   @   00755c3a:0xa0025
ves_icall_System_Threading_ThreadPool_RequestWorkerThread_raw   @   00755c3a:0x9fecf
do_icall    @   00755c3a:0x10f790
do_icall_wrapper    @   00755c3a:0x50b6a
interp_exec_method  @   00755c3a:0x2588e
interp_runtime_invoke   @   00755c3a:0xf7391
mono_jit_runtime_invoke @   00755c3a:0xddb3d
do_runtime_invoke   @   00755c3a:0x3ba85
mono_runtime_try_invoke @   00755c3a:0xcfdb
mono_runtime_invoke @   00755c3a:0x44b39
mono_wasm_invoke_method @   00755c3a:0xca6a7
Module._mono_wasm_invoke_method @   dotnet.3.2.0.js:1
call_method @   dotnet.3.2.0.js:1
(anonymous) @   dotnet.3.2.0.js:1
endInvokeJSFromDotNet   @   blazor.webassembly.js:1
(anonymous) @   blazor.webassembly.js:1
Promise.then (async)        
beginInvokeJSFromDotNet @   blazor.webassembly.js:1
_mono_wasm_invoke_js_marshalled @   dotnet.3.2.0.js:1
do_icall    @   00755c3a:0x10f8b1
do_icall_wrapper    @   00755c3a:0x50b6a
interp_exec_method  @   00755c3a:0x2588e
interp_runtime_invoke   @   00755c3a:0xf7391
mono_jit_runtime_invoke @   00755c3a:0xddb3d
do_runtime_invoke   @   00755c3a:0x3ba85
mono_runtime_invoke_checked @   00755c3a:0x96a2
mono_runtime_try_invoke_array   @   00755c3a:0x66c26
ves_icall_InternalInvoke    @   00755c3a:0xb44ff
ves_icall_InternalInvoke_raw    @   00755c3a:0xb3ffd
do_icall    @   00755c3a:0x10f8b1
do_icall_wrapper    @   00755c3a:0x50b6a
interp_exec_method  @   00755c3a:0x2588e
interp_runtime_invoke   @   00755c3a:0xf7391
mono_jit_runtime_invoke @   00755c3a:0xddb3d
do_runtime_invoke   @   00755c3a:0x3ba85
mono_runtime_try_invoke @   00755c3a:0xcfdb
mono_runtime_invoke @   00755c3a:0x44b39
mono_wasm_invoke_method @   00755c3a:0xca6a7
Module._mono_wasm_invoke_method @   dotnet.3.2.0.js:1
call_method @   dotnet.3.2.0.js:1
(anonymous) @   dotnet.3.2.0.js:1
beginInvokeDotNetFromJS @   blazor.webassembly.js:1
s   @   blazor.webassembly.js:1
e.invokeMethodAsync @   blazor.webassembly.js:1
(anonymous) @   blazor.webassembly.js:1
t.dispatchEvent @   blazor.webassembly.js:1
(anonymous) @   blazor.webassembly.js:1
(anonymous) @   blazor.webassembly.js:1
e.onGlobalEvent @   blazor.webassembly.js:1

Please advise on what further tests I need to do to determine what the problem is, and how I can fix.

Edit and update

Following from one of the answers below, I switched to using a PutObjectRequest. The code is below. This now gives a new error, which I assume is to do with the permissions of the S3 bucket. Can anyone guide on what I need to change?

Code

public class UploadData
{
    private readonly BasicAWSCredentials credentials = new BasicAWSCredentials("access key", "secret Key");

    public async Task UploadZipFile(Stream zip_file, string file_name)
    {
        zip_file.Seek(0, SeekOrigin.Begin);
        var config = new AmazonS3Config
        {
            RegionEndpoint = Amazon.RegionEndpoint.EUWest2
        };
        using var client = new AmazonS3Client(credentials, config);

        var putRequest1 = new PutObjectRequest
        {
            BucketName = "Bucket_name",
            Key = file_name,
            InputStream = zip_file
        };
        PutObjectResponse response1 = await client.PutObjectAsync(putRequest1);

        Console.WriteLine("finshed uploading");
    }
}

And here is the error code

Unknown encountered on server. Message:'TypeError: Failed to execute 'append' on 'Headers': Invalid value' when putting an object

解决方案

I do not know much about blazor and webassembly in particular, but I can give try and give you an advice what I would try:

  1. It looks like the issue in the code above is a lack of multithreading support in webassembly, see also [1].
  2. The TransferUtility class in the aws-sdk-net makes use of threads for multipart uploads, see [2]:

TransferUtility provides a simple API for uploading content to and downloading content from Amazon S3. It makes extensive use of Amazon S3 multipart uploads to achieve enhanced throughput, performance, and reliability.

When uploading large files by specifying file paths instead of a stream, TransferUtility uses multiple threads to upload multiple parts of a single upload at once. When dealing with large content sizes and high bandwidth, this can increase throughput significantly.

Thus, I would try to use some of the low-level constructs to accomplish a standard upload using PutObjectRequest, see: https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html

If the files to be uploaded are not too large, this should work without any issues because it is a single-threaded approach.

Another issue would be to enable multi-threading in Blazor WASM, but I am not sure if that is already an option, see [3].

[1] https://www.meziantou.net/don-t-freeze-ui-while-executing-cpu-intensive-work-in-blazor-webassembly.htm
[2] https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/S3/TTransferUtility.html
[3] https://github.com/dotnet/aspnetcore/issues/17730

这篇关于将 zip 从 Blazor WASM 应用程序上传到 AWS-S3 - 遵循教程,没有观察到错误,但存储桶中也没有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆