locked
stream.ReadTimeout' threw an exception of type 'System.InvalidOperationException RRS feed

  • Question

  • User1052662409 posted

    Hi All,

    I am using below code to upload file.

    if (FileUpload1.HasFile)
                    {
                            Stream st = FileUpload1.PostedFile.InputStream;
    
                            string name = Path.GetFileName(FileUpload1.FileName); 
                            string myBucketName = "*******"; //your s3 bucket name goes here  
                            string s3DirectoryName = "";
                            string s3FileName = name;
                            bool uploaded;
                            UploadFiles myUploader = new UploadFiles();
                            uploaded = myUploader.sendMyFileToS3(st, myBucketName, s3DirectoryName, s3FileName);
                    }

    I got the error  st.ReadTimeout' threw an exception of type 'System.InvalidOperationException  on the below line

    Stream st = FileUpload1.PostedFile.InputStream;

    Please suggest

    Tuesday, June 18, 2019 6:08 PM

Answers

All replies

  • User475983607 posted

    File upload in Web Forms is well documented with wonderful examples that work perfectly.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.fileupload?view=netframework-4.8

    Can you explain why you are accessing the InputStream and the problem you are trying to solve at a high level so we are not guessing?  Seems like you are trying to forward the stream?  If that's the case then you'll need to read the stream into a buffer and forward the buffer.

    Tuesday, June 18, 2019 6:41 PM
  • User1052662409 posted

    mgebhard

    Can you explain why you are accessing the InputStream and the problem you are trying to solve at a high level so we are not guessing?

    Sir, because my class takes stream as a parameter.

    public bool sendMyFileToS3(System.IO.Stream localFilePath, string bucketName, string subDirectoryInBucket, string fileNameInS3)
        {
    
            IAmazonS3 client = new AmazonS3Client(RegionEndpoint.APSouth1);
            TransferUtility utility = new TransferUtility(client);
            TransferUtilityUploadRequest request = new TransferUtilityUploadRequest();
    
            if (subDirectoryInBucket == "" || subDirectoryInBucket == null)
            {
                request.BucketName = bucketName; //no subdirectory just bucket name  
            }
            else
            {   // subdirectory and bucket name  
                request.BucketName = bucketName + @"/" + subDirectoryInBucket;
            }
            request.Key = fileNameInS3; //file name up in S3  
            request.InputStream = localFilePath;
            utility.Upload(request); //commensing the transfer   
    
    
            return true; //indicate that the file was sent  
        }

    Tuesday, June 18, 2019 7:11 PM
  • User475983607 posted

    demoninside9

    Sir, because my class takes stream as a parameter.

    That's a a design detail not the high-level problem you are trying to solve.

    I'm pretty sure in ASP.NET you need to read the file stream into a buffer first.  Then pass the buffer to the sendMyfilesToS3() method. 

    https://docs.microsoft.com/en-us/dotnet/api/system.web.httppostedfile.inputstream?view=netframework-4.8

    Have you verified the sendMyfilesToS3() works?  I would expect a content-length so the receiver know how many bytes to expect.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 18, 2019 7:40 PM
  • User1052662409 posted

    Have you verified the sendMyfilesToS3() works?

    Now it is working fine, without any issue.

    private string bucketName = "********";
            private string keyName = "New Microsoft Word Document.docx";
            private string filePath = "D:\\MyData\\*****\\New Microsoft Word Document.docx";
    
    
           
    
            public void UploadFile(string bucketName,string keyName, string filePath )
            {
    
                try
                {
                    var client = new AmazonS3Client(RegionEndpoint.APSouth1);
                    PutObjectRequest putRequest = new PutObjectRequest
                    {
                        BucketName = bucketName,
                        Key = keyName,
                        FilePath = filePath,
                        ContentType = "text/plain"
                    };
                    PutObjectResponse response = client.PutObject(putRequest);
                }
                catch (AmazonS3Exception amazonS3Exception)
                {
                    if (amazonS3Exception.ErrorCode != null &&(amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") ||amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                    {
                       WebMsgBox.Show("Check the provided AWS Credentials.");
                    }
                    else
                    {
                        WebMsgBox.Show("Error occurred: " + amazonS3Exception.Message);
                    }
                }

    Only the issue is that I am saving first it on my root's local folder that uploading to bucket. like below

    FileUpload1.SaveAs(Server.MapPath("MyFolders/" + filename));

    Because without this I am not getting the file path. I was willing to upload it directly without saving on my root's folder

    Wednesday, June 19, 2019 6:52 AM
  • User665608656 posted

    Hi demoninside,

    According to your requirements and the code your provided, it is not permissible that put the inputstream directly into Amazon s3 client.

    I suggest you could change your InputStream into byte[], then proceed to your next steps.

    Here is the mehod about how to make inputStream into byte[] :

     public static byte[] ReadFully(Stream input)
            {
                byte[] buffer = new byte[input.Length];
                //byte[] buffer = new byte[16 * 1024];
                using (MemoryStream ms = new MemoryStream())
                {
                    int read;
                    while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        ms.Write(buffer, 0, read);
                    }
                    return ms.ToArray();
                }
            }

    Best Regards,

    YongQing.

    Wednesday, June 19, 2019 8:04 AM