By using AWS re:Post, you agree to the Terms of Use
/How do we unzip a file in S3 bucket using C#.NET/

How do we unzip a file in S3 bucket using C#.NET


There is a ZIP file my S3 bucket, I just want to Un-ZIP using C#.NET

asked 16 days ago38 views
3 Answers

The Amazon S3 SDK offers you a way to download a file in-memory. By using the ResponseStream property of the response object, you obtain access to the downloaded object data. Refer to the documentation about downloading objects from S3.
The System.IO.Compression.ZipArchive has a constructor accepting a stream as an input parameter. You can use this constructor to create an object for working with the archive in-memory.

Here is an example code snippet that should give you an idea. Please note that this code is not production-ready and should not be used as-is.

var client = new AmazonS3Client();
var request = new GetObjectRequest { BucketName = "your-bucket", Key = "" };
using var response = await client.GetObjectAsync(request);
using var zip = new ZipArchive(response.ResponseStream, ZipArchiveMode.Read);
foreach (var entry in zip.Entries)
    using var stream = entry.Open();
    // stream will contain decompressed data, do whatever you want with it

UPDATE As mentioned in the comment below, you should be aware of the caveats of this approach. You might need to copy the ResponseStream to a MemoryStream first. Also, you have to ensure the zip file itself and its extracted content fit in the available memory. Otherwise, your application will terminate with an OutOfMemoryException.

answered 13 days ago
  • If you don't want to create a temporary file, this is the correct approach, but with a caveat. There is a possibility the backing stream will have to be hydrated into memory in order to jump to different entries. If the file is very large, there is a chance for OutOfMemoryException. Only you know the about system resources available, but if the file is over - say 100MB, you might consider writing it to disk first. On disk, it's possible to use random access to move backward and forward within the stream using buffering. Network streams simply don't have this option; they are forward only.



You'll need to download it first and then unzip - and upload it back if you need it in the same s3 bucket

answered 16 days ago
  • Hi, Thanks for your reply. I cannot download it to local hard disk, it should be run on we need to use here lamda functions?

  • Yes, a lambda function will work. You need to be aware of any sizing requirements, and not exceed memory of the lambda.


Is there any code snippet please, I did not find the answers in internet. I just need to unzip the file in S3 bucket on the fly in c#, no physical storage

answered 15 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions