Unable to stream xlsx doc from S3 Bucket

I am trying to pull down a xlsx document from a s3 bucket and it keeps failing with an error -
{"Zip exception.Unable to read value at the specified position - end of stream was reached."} Syncfusion.Compression.Zip.ZipException but the item it is pulling down is not a zip document 

        protected void reportfunction_Click(object sender, EventArgs e)
        {
            {
                ExcelEngine excelEngine = new ExcelEngine();
                
                IWorkbook workbook = excelEngine.Excel.Workbooks.Open(getobject());
                IWorksheet sheet = workbook.Worksheets[0];
                sheet.Range["O1"].Text = "Editing the O1";

                //Saves and closes the document instance

                workbook.SaveAs(@"C:\Users\" + Environment.UserName + @"\Desktop\Result.xlsx");

                workbook.Close();

                System.Diagnostics.Process.Start(@"C:\Users\" + Environment.UserName + @"\Desktop\Result.xlsx");

            }
        }
The code I am using to get the xlsx doc is below.  ** Note I was able to use this to successfully pull down a docx file and make changes with SyncFusion. **
        private Stream getobject()
        {
            using (AmazonS3Client client = new AmazonS3Client(RegionEndpoint.USEast1))
            {
                GetObjectResponse getObjRespone = client.GetObject("bucketname", "filename.xlsx");
                MemoryStream stream = new MemoryStream();
                getObjRespone.ResponseStream.CopyTo(stream);
                return stream;
            }
        }

I tried to add in FileStream inputStream = getobject() as FileStream; but it told me that it could not read a null value.

If replace the getobject() in the excelEngine.Excel.Workbooks.Open(getobject()); with a local copy of the file it does work and edit the file correctly.

Anyone have any ideas? 

Thanks! 

3 Replies

AV Abirami Varadharajan Syncfusion Team May 27, 2019 09:35 AM UTC

Hi Ryan, 

Greetings from Syncfusion. 

As the stream reached end position while copying, the exception is raised at your end. You can reset the stream position as `0` to resolve this issue. Please find below code for the same. 

Code Example: 
private Stream getobject() 
        { 
            using (AmazonS3Client client = new AmazonS3Client(RegionEndpoint.USEast1)) 
            { 
                GetObjectResponse getObjRespone = client.GetObject("bucketname", "filename.xlsx"); 
                MemoryStream stream = new MemoryStream(); 
                getObjRespone.ResponseStream.CopyTo(stream); 
                stream.Position = 0; 
                return stream; 
            } 
        } 

Kindly try this and let us know if this resolve the issue. 

Regards, 
Abirami 



RY Ryan May 27, 2019 04:42 PM UTC

Thank you that worked perfectly. 


AV Abirami Varadharajan Syncfusion Team May 28, 2019 06:53 AM UTC

Hi Ryan, 

We are glad that it worked fine at your end.  

Please get back to us know if you need further assistance. 

Regards, 
Abirami. 


Loader.
Up arrow icon