user181218
user181218

Reputation: 1685

AWS S3 GetObjectResponse.ResponseStream length different than requested

I'm having a serious problem with reading object data from the Amazon S3. My code for reading a certain range of bytes is the following.

If you check response.contentLength you find that it has the correct value, i.e. end - start. However, the length of the response stream is less than what I've asked and what it says in the ContentLength. I Don't get all the data I've asked for back, and I have no idea why and what part is trimmed.

                GetObjectRequest request = new GetObjectRequest()
                {
                    BucketName = m_BucketName,
                    Key = m_FileName,
                    ByteRangeLong = new Amazon.S3.Model.Tuple<long, long>(start, end)
                };
                GetObjectResponse response = m_AwsClient.GetObject(request);
                using (Stream responseStream = response.ResponseStream)
                {
                    using (StreamReader reader =
                        new StreamReader(responseStream))
                    {
                        strResponse = reader.ReadToEnd();
                    }
                }

Upvotes: 1

Views: 2180

Answers (1)

user181218
user181218

Reputation: 1685

It's an encoding problem. The StreamReader class defaults to UTF-8 encoding, which ignores whichever chars it cannot understand. If I set the encoding to Default in the StreamReader constructor it gets resolved.

Upvotes: 1

Related Questions