I am using presigned URLs to let our C# application upload directly from the user location to our S3 bucket. I exactly used the .NET sample code from the AWS documentation:
https://docs.aws.amazon.com/AmazonS3/latest/userguide/PresignedUrlUploadObject.html
private static void UploadObject(string url)
{
HttpWebRequest httpRequest = WebRequest.Create(url) as HttpWebRequest;
httpRequest.Method = "PUT";
using (Stream dataStream = httpRequest.GetRequestStream())
{
var buffer = new byte[8000];
using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
dataStream.Write(buffer, 0, bytesRead);
}
}
}
HttpWebResponse response = httpRequest.GetResponse() as HttpWebResponse;
}
This works well for small image files (<1MB) but often fails for larger video files (10-20 MB). I need multiple attempts before the upload succeeds, because I often get a SocketException error, saying that the connection was closed by the remote host.
Does anyone know how to deal with this issue?
Thank you for your help!