尝试将文件上传到S3存储桶时任务被取消异常

时间:2018-10-04 13:20:42

标签: amazon-s3 asp.net-core

我尝试调用 fileTransferUtility.UploadAsync 以在S3中上传文件时,引发了任务被取消的异常。我正在使用dot net core 2.0并尝试将文件上传到S3。在下面的代码中我在做什么错? 与超时有关吗?如果是这样,如何为S3存储桶设置时间?还是我必须在S3存储桶上设置一些属性?

下面是我的控制器代码:

 public class UploadController : Controller
    {
        private IHostingEnvironment _hostingEnvironment;
        private AmazonS3Client _s3Client = new AmazonS3Client(RegionEndpoint.APSoutheast1);
        private string _bucketName = "fileupload";//this is my Amazon Bucket name
        private static string _bucketSubdirectory = String.Empty;
        private string uploadWithKeyName = "testFile";

        public UploadController(IHostingEnvironment environment)
        {
            _hostingEnvironment = environment;
        }

        [HttpPost("UploadExcelData")]
        public async Task PostExcelData()
        {
            var files = Request.Form.Files;
            var stringVal = Request.Form.Keys;
            long size = files.Sum(f => f.Length);

            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    var filename = ContentDispositionHeaderValue
                            .Parse(formFile.ContentDisposition)
                            .FileName
                            .TrimStart().ToString();
                    filename = _hostingEnvironment.WebRootPath + $@"\uploads" + $@"\{formFile.FileName}";
                    size += formFile.Length;
                    using (var fs = System.IO.File.Create(filename))
                    {
                        formFile.CopyTo(fs);
                        fs.Flush();
                    }//these code snippets saves the uploaded files to the project directory

                  await UploadToS3(filename);//this is the method to upload saved file to S3

                }
            }
          //  return Ok();
        }
        public async Task UploadToS3(string filePath)
        {
            try
            {
                TransferUtility fileTransferUtility = new
                    TransferUtility(_s3Client);

                string bucketName;


                if (_bucketSubdirectory == "" || _bucketSubdirectory == null)
                {
                    bucketName = _bucketName; //no subdirectory just bucket name  
                }
                else
                {   // subdirectory and bucket name  
                    bucketName = _bucketName + @"/" + _bucketSubdirectory;
                }


                // 1. Upload a file, file name is used as the object key name.
                await fileTransferUtility.UploadAsync(filePath, bucketName, uploadWithKeyName).ConfigureAwait(false);
                Console.WriteLine("Upload 1 completed");


            }
            catch (AmazonS3Exception s3Exception)
            {
                Console.WriteLine(s3Exception.Message,
                                  s3Exception.InnerException);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unknown error", ex.Message);
            }

        }
    }

1 个答案:

答案 0 :(得分:1)

我忘了通过凭据: 私有AmazonS3Client _s3Client =新的AmazonS3Client(DynamoDbCRUD.Credentials.AccessKey,DynamoDbCRUD.Credentials.SecretKey,RegionEndpoint.APSoutheast1);

这行工作正常。