Ejemplo n.º 1
0
        private void OnNextFragment(BlockFragment blockFragment)
        {
            Task.Factory.StartNew(async() =>
            {
                try
                {
                    Console.WriteLine(blockFragment.FileFragmentId);
                    var uploadNextFragment = _blockBlobRepository.UploadNextFragment(blockFragment);
                    while (!_blockingCollection.TryAdd(uploadNextFragment))
                    {
                    }

                    if (blockFragment.IsLastFragment)
                    {
                        _manualResetEventSlim.Wait();
                        await Task.WhenAll(_blockingCollection.ToArray());
                        await _blockBlobRepository.CommitFragments();
                    }
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception);
                    throw;
                }
            });
        }
 public void FragmentGeneratedOnTheFly(BlockFragment blockFragment)
 {
     try
     {
         _fragmentGenerated.OnNext(blockFragment);
         if (blockFragment.IsLastFragment)
         {
             _fragmentGenerated.OnCompleted();
         }
     }
     catch (Exception exception)
     {
         _fragmentGenerated.OnError(exception);
     }
 }
        public async Task <Response <BlockInfo> > UploadNextFragment(BlockFragment blockFragment)
        {
            await using var stream = new MemoryStream(blockFragment.Buffer);

            using var md5 = MD5.Create();
            var computeHash = md5.ComputeHash(stream);

            stream.Position = 0;
            var base64BlockId = blockFragment.FileFragmentId.ToBase64();
            Response <BlockInfo> stageBlock = null;

            try
            {
                var stageBlockAsync = _blockBlobClient.StageBlockAsync(base64BlockId, stream, computeHash);
                stageBlock = await stageBlockAsync;
                Console.WriteLine($"{stageBlock.GetRawResponse().Status}", "{blockFragment.FileFragmentId:d3}");
            }
            catch (Exception exception)
            {
                Console.WriteLine($"Error : {_blobName}, {blockFragment.FileFragmentId:d3} : {base64BlockId}");
            }

            return(stageBlock);
        }