Recently, we were working on a task where we need to give an option to a user to download individual files or a zip of all files.

You can create a zip file using the following piece of code:

Here, we import ByteIO from io package of python to read and write byte streams.

We need to import the zip file to create zip files.

We need to import os for a store and delete file operations.

Here, we create an object of ByteIO. We passed byte object to zip file object.

Here, we are looping through a list of files and writing them into a zip file and removing from memory.

But in our task, we need to get those files from AWS and return downloadable zip file in HttpResponse.

We can do by following the piece of code:

In the above piece of code, we are using boto to access files from AWS. In order to get files from AWS first, you need to connect to AWS.

bucket.lookup() will return key of a specified path or false if there is no file exist on that path.

If you want to know more about how to upload and download more content from AWS, I have published another blog, you can look at here.

Here, we created an object resp of HttpResponse.

We defined content-type and bytes that we have written in byte object.

At last, we need to dispose of your content using Content-Disposition of the response object.

Click here for more details…

At BoTree Technologies, we build enterprise applications with our Django team of 20+ engineers.

We also specialize in RPA, AI, Python, Ruby on Rails, JavaScript and ReactJS.

Consulting is free – let us help you grow!