1
votes

I'm trying to build out a project where users can upload small videos. I've created the below class to handle my Cloudinary code on how to upload a video. It works fine for videos that are very short like 0.3 sec videos but when I try to upload more than 0.10 secs - I get an exception:

System.Net.Http.HttpRequestException: Error while copying content to a stream.

The videos are being uploaded to Cloudinary. Here's the code:

 public class VideoAccessor : IVideoAccessor
    {
        private readonly Cloudinary _cloudinary;
        public VideoAccessor(IConfiguration config)
        {
            // instantiate a new instance of cloudinary using details provided
            _cloudinary = new Cloudinary(config["cloudinary"]);
        }
        
        public VideoUploadResult UploadClip(IFormFile videoFile)
        {
            var uploadResult = new CloudinaryDotNet.Actions.VideoUploadResult();
            if (videoFile.Length > 0)
            {
                // create a new file stream to upload the video to cloudinary
                using (var filestream = videoFile.OpenReadStream())
                {
                    var uploadParams = new VideoUploadParams
                    {
                        File = new FileDescription(videoFile.FileName, filestream),
                        Transformation = new Transformation().StartOffset("0").EndOffset("120").Crop("fill")

                    };
                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }
            
            // checks if error occurs when uploading video
            if (uploadResult.Error != null)
            {
                throw new Exception(uploadResult.Error.Message);
            }

            return new VideoUploadResult
            {
                PublicId = uploadResult.PublicId,
                Url = uploadResult.SecureUrl.AbsoluteUri
            };
        }
}
}

When trying to upload I get an exception. Does anyone know what I can add to this or how to make it more acceptable for larger files?

The stack trace:

System.Threading.Tasks.TaskCanceledException: The operation was canceled.

System.Net.Http.HttpRequestException: Error while copying content to a stream.

System.IO.IOException: Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request

System.Net.Sockets.SocketException (995): The I/O operation has been aborted because of either a thread exit or an application request.

1
Is there an inner exception that says what the actual error was? - stuartd
i uploaded the stack trace - DBankx
it works fine for videos that are very short like 0.3 sec videos but when i try to upload more than 0.10 secs worth - did you mean "10 secs"? - stuartd
I see there's a TaskCanceledException at the top of the stack. Does the library support async calls? - stuartd
no i mean literally 0.10secs worth. and yes the library does support it - DBankx

1 Answers

0
votes

Your upload request was probably timing out. You should probably need to upload your videos in chunks using the UploadLarge method. See -https://github.com/cloudinary/CloudinaryDotNet/blob/a3a4d38c4fd7c1b03beeb3eed7d8ea725866a36c/Shared/Cloudinary.cs#L1229

For more information: https://cloudinary.com/documentation/upload_images#chunked_asset_upload