Howto read bz2 file from AWS S3 and into a StreamReader in .Net

This sample assumes you read the bz2 files as S3 objects (bucketnames etc in app.config as it follows AWS SDK default settings), and that the files are text files (=> using ReadLine.) S3 initialization is not included in the code.

In order to make the bz2 decompression work, I am using the SharepZipLib Nuget package (0.86.0)

public static void ExtractToConsole(S3Object s3Object)
GetObjectRequest requestObject = new GetObjectRequest()
BucketName = s3Object.BucketName,
Key = s3Object.Key
GetObjectResponse objectResponse = new AmazonS3Client().GetObject(requestObject);

BZip2InputStream bZip2InputStream = new BZip2InputStream(objectResponse.ResponseStream);

StreamReader sr  = new StreamReader(bZip2InputStream);
var ldeb = 0;
while (!sr.EndOfStream)
var line = sr.ReadLine();
Console.WriteLine(“Line: ” + line);



Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s