【问题标题】:TaskCanceledException on large file upload to AWS S3将大文件上传到 AWS S3 时出现 TaskCanceledException
【发布时间】:2019-02-20 18:23:25
【问题描述】:

我正在尝试将一个大型视频文件 (800mb) 上传到我的 S3 存储桶,但它似乎超时。它适用于较小的文件。我的项目是一个 ASP.Net Core 2.1 应用程序。

这是抛出的异常:

处理请求时发生未处理的异常。 SocketException:由于线程退出或应用程序请求,I/O 操作已中止。 位置不明

IOException:无法从传输连接中读取数据:I/O 操作已因线程退出或应用程序请求而中止。 System.Net.Sockets.Socket+AwaitableSocketAsyncEventArgs.ThrowException(SocketError 错误)

TaskCanceledException:操作已取消。 AWSS3Helper.cs 中的 GamerPilot.Video.AWSS3Helper.UploadFileAsync(流流,字符串键,S3CannedACL acl,bool useReducedRedundancy,bool throwOnError,CancellationToken cancelToken),第 770 行

我的源代码如下所示:

  public async Task<IVideo> AddVideoAsync(int instructorId, int lectureId, string videoName, string filePath, CancellationToken cancellationToken = default(CancellationToken))
    {
        if (string.IsNullOrEmpty(filePath)) { throw new ArgumentNullException("filePath", "Video filepath is missing"); }
        if (!File.Exists(filePath)) { throw new ArgumentNullException("filePath", "Video filepath does not exists"); }

        //Test video file upload and db row insertion
        using (var stream = File.OpenRead(filePath))
        {
            return await AddVideoAsync(instructorId, lectureId, videoName, stream, cancellationToken);
        }
    }

 public async Task<IVideo> AddVideoAsync(int instructorId, int lectureId, string videoName, Stream videoFile, CancellationToken cancellationToken = default(CancellationToken))
    {
        var video = (Video) await GamerPilot.Video.Helper.Create(_awsS3AccessKey, _awsS3SecretKey, _awsS3BucketName, _awsS3Region)
            .AddVideoAsync(instructorId, lectureId, videoName, videoFile, cancellationToken);

        using (var db = new DbContext(_connectionString))
        {
            db.Videos.Add(video);
            var count = await db.SaveChangesAsync();
        }

        return video;
    }`

 public async Task<IVideo> AddVideoAsync(int instructorId, int lectureId, string videoName, Stream videoFile, CancellationToken cancellationToken = default(CancellationToken))
    {
        if (string.IsNullOrEmpty(videoName)) { throw new ArgumentNullException("videoName", "Video name cannot be empty or null"); }
        if (videoFile == null) { throw new ArgumentNullException("video", "Video stream is missing"); }

        var videoNameCleaned = videoName.Replace(" ", "-").ToLower().Replace(".mp4", "");
        var videoKey = string.Join('/', "videos", instructorId, lectureId, videoNameCleaned + ".mp4");

        using (var aws = new AWSS3Helper(_awsS3AccessKey, _awsS3SecretKey, _awsS3BucketName, _awsS3Region))
        {
            try
            {
                //THIS FAILS ------
                await aws.UploadFileAsync(videoFile, videoKey, Amazon.S3.S3CannedACL.PublicRead, true, true, cancellationToken);

            }
            catch (Exception ex)
            {

                throw;
            }
        }

        return new Video
        {
            InstructorId = instructorId,
            LectureId = lectureId,
            Name = videoName,
            S3Key = videoKey,
            S3Region = _awsS3Region.SystemName,
            S3Bucket = _awsS3BucketName,
            Created = DateTime.Now
        };
    }

我该如何解决这个问题?

【问题讨论】:

    标签: amazon-web-services amazon-s3 asp.net-core task


    【解决方案1】:

    S3 本身没有一般限制,可以防止您上传 800MB 的文件。但是,在使用 AWS 时处理重试和超时有一些要求。从您的问题中不清楚您是否使用亚马逊的 SDK,(我找不到 GamerPilot.Video.AWSS3Helper.UploadFileAsync 的来源)。但是,如果您按照以下方式使用 Amazon 的 .NET SDK 应该会为您处理此问题:

    Programming with the AWS SDK for .NET - Retries and Timeouts

    Using the AWS SDK for .NET for Multipart Upload (High-Level API)

    【讨论】:

      猜你喜欢
      • 2016-07-24
      • 2011-06-09
      • 2015-04-04
      • 1970-01-01
      • 2020-05-07
      • 2022-06-16
      • 1970-01-01
      • 2018-07-13
      • 2020-08-26
      相关资源
      最近更新 更多