简体   繁体   English

AWS S3 文件上传 .NET SDK (.NET Core) 无法将数据写入传输连接

[英]AWS S3 File Upload .NET SDK (.NET Core) Unable to write data to the transport connection

I'm writing a function to upload multiple files to AWS S3.我正在编写一个函数来将多个文件上传到 AWS S3。 Even with a single file, the commented code using TransferUtilityUploadRequest below fails with an error:即使使用单个文件,下面使用 TransferUtilityUploadRequest 的注释代码也会失败并显示错误:

System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host. System.IO.IOException:无法将数据写入传输连接:远程主机强行关闭了现有连接。

The uncommented code works, (but I am unable to set ACL permissions).未注释的代码有效(但我无法设置 ACL 权限)。 Having looked at several examples online, I am not sure what I am doing wrong?在网上看了几个例子后,我不确定我做错了什么? The RequestFormLimits are not this issue here, the code fails at the point it tries to upload to S3. RequestFormLimits 不是这里的问题,代码在尝试上传到 S3 时失败。

    [HttpPost("test/raw-file-upload-s3")]
[RequestFormLimits(MultipartBodyLengthLimit = 1073741824)]
    [RequestSizeLimit(1073741824)]
    public async Task<ActionResult<object>> UploadFiles3(IList<IFormFile> files)
    {
        var regionEndpoint = RegionEndpoint.GetBySystemName(_awsS3RegionEndpoint);

        var uploadedFileNames = new List<string>();

        using (var awsS3Client = new AmazonS3Client(new BasicAWSCredentials(_awsAccessKeyId, _awsSecretKey), regionEndpoint))
        {
            using (var fileTransferUtility = new TransferUtility(awsS3Client))
            {
                var dateBasedDirectoryName = GetDateBasedDirectoryName();

                string contentDirectory = $@"example/";

                var tasks = new Task[files.Count];

                var streams = new Stream[files.Count];

                for (int i = 0; i < files.Count; i++)
                {
                    string fileName = $"{Guid.NewGuid()}_{files[i].FileName}";

                    string key = $"{contentDirectory}{fileName}";

                    streams[i] = files[i].OpenReadStream();

                    tasks[i] = fileTransferUtility.UploadAsync(streams[i], _awsS3AssetsBucket, key);

                    // var fileTransferUtilityRequest = new TransferUtilityUploadRequest
                    // {
                    //     BucketName = _awsS3AssetsBucket,
                    //     Key = key,
                    //     CannedACL = S3CannedACL.PublicRead,
                    //     InputStream = streams[i]
                    // };

                    // tasks[i] = fileTransferUtility.UploadAsync(fileTransferUtilityRequest);
                }

                await Task.WhenAll(tasks).ConfigureAwait(false);

                for (int i = 0; i < files.Count; i++)
                {
                    streams[i].Close();
                }
            }
        }

        return new
        {
            uploadedFiles = uploadedFileNames
        };
    }

Can you try below你可以试试下面

// Set Canned ACL (PublicRead) for an existing item
client.PutACL(new PutACLRequest
{
    BucketName = _awsS3AssetsBucket,
    Key = key,
    CannedACL = S3CannedACL.PublicRead
});

Before this line在这条线之前

tasks[i] = fileTransferUtility.UploadAsync(streams[i], _awsS3AssetsBucket, key);

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

相关问题 使用AWS开发工具包将文件上传到.NET Core中的S3 - Using AWS SDK to upload file to S3 in .NET Core System.Net.WebClient“无法将数据写入传输连接” - System.Net.WebClient “Unable to write data to the transport connection” 将大文件上传到.Net中的aws S3服务器(asp.net核心) - Upload large file to aws S3 server in .Net (asp.net core) AWS .NET SDK直接上传到S3-预签名URL - AWS .NET SDK Direct Upload to S3 - Pre Signed URL -AWS C#.Net Core-如何将.jpg图像上传到S3存储桶而不将其另存为文件 - -AWS C# .Net Core- How to upload a .jpg image to S3 bucket without saving it as a file AWS .NET SDK 使用 ASP.NET Core 依赖注入配置 S3 客户端 - AWS .NET SDK configure S3 Client with ASP.NET Core Dependency Injection 在.NET Core中配置AWS S3客户端 - Configuring AWS S3 Client in .NET Core 使用AWS S3 SDK for .NET从Amazon S3下载并行批处理文件 - Parallel batch file download from Amazon S3 using AWS S3 SDK for .NET 无法使用适用于Amazon的.NET SDK从Windows Phone将图像上传到Amazon s3 - Unable to upload image to Amazon s3 from Windows Phone using .NET SDK for Amazon 无法从传输连接中读取数据:net_io_connectionclosed.?8 - Unable to read data from the transport connection: net_io_connectionclosed.?8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM