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

0

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

asked 2 years ago2899 views
4 Answers
1

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 = "file.zip" };
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.

profile pictureAWS
answered 2 years 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.

0

Hello,

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

AWS
answered 2 years ago
  • Hi, Thanks for your reply. I cannot download it to local hard disk, it should be run on memory.....do 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.

0

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 2 years ago
0

I have a similar question here: https://repost.aws/questions/QUh855MWiuRJerSN9fGVjVuQ/aws-s-3-get-object-not-working-getting-corrupt-file. My code downloads the zip file from S3 but is corrupt, and if is corrupt I can't unzip it. Please help!

answered 2 years 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