Amazon S3 TransferUtility.Upload hangs in C#

后端 未结 2 703
故里飘歌
故里飘歌 2020-12-31 11:34

So I\'m writing a migration application, to take some data from our local storage and upload it to Amazon. Everything is working fine, except once I get into files that are

相关标签:
2条回答
  • 2020-12-31 11:51

    Is DisplayFileProgress thread-safe? I believe (looking at some older code) that the callback is called by each upload thread independently.

    This code below is part of a small utility that we use to upload files ranging from 5 MB up to 1-2 GB or so. It's not remarkably different than yours, but perhaps it might help.

    var writerlock = new object();
    
    using (var tu = new TransferUtility(amazonS3Client, tuconfig))
    {
        var turequest = new TransferUtilityUploadRequest()
            .WithBucketName(bucket)
            .WithFilePath(file)
            .WithKey(Path.GetFileName(file))
            .WithStorageClass(S3StorageClass.ReducedRedundancy)
            .WithPartSize(5 * 1024 * 1024)
            .WithAutoCloseStream(true)
            .WithCannedACL(S3CannedACL.PublicRead);
    
        tuconfig.NumberOfUploadThreads = Environment.ProcessorCount - 1;
    
        // show progress information if not running batch
        if (interactive)
        {
            turequest.UploadProgressEvent += (s, e) =>
                                              {
                                                  lock (writerlock)
                                                  {
                                                      ... our progress routine ...
                                                  }
                                              };
        }
    
        tu.Upload(turequest);
    }
    
    0 讨论(0)
  • 2020-12-31 12:01

    Try using BeginUpload method instead of Upload.

            transferUtility.BeginUpload(request, new AsyncCallback(uploadComplete), null );
        }
        private void uploadComplete(IAsyncResult result)
        {
            var x = result;
        }
    

    Setup your transfer utility and UploadProgressEvent as you did before. Use the Invoke method within the progress handler as you did. If you use BeginUpdate() instead of Update() it will prevent the app from hanging on the first update to your form. I couldn't find this solution anywhere so I hope it works for you.

    0 讨论(0)
提交回复
热议问题