I am trying to generate a zip file and store in App Engine's Blobstore. Right now, I do not get a valid zip file from the Blobstore. Not sure the problem is with zipping, storing, retrieving or downloading.
I have built the code based on snippets from the following questions.
- Is it possible to generate and return a ZIP file with App Engine?
- Zipping dynamic files in App Engine (Python)
After storing in Blobstore, I let users download it through a Flask application.
Here is the gist of what I am trying to do.
def zipit():
zipstream = StringIO.StringIO()
zfile = zipfile.ZipFile(file=zipstream, mode='w')
bytes = "lorem ipsum dolor sit amet"
zfile.writestr('loremipsum', bytes, compress_type=zipfile.ZIP_STORED)
zfile.close()
zipstream.seek(0)
return zipstream.getvalue()
zip_file = files.blobstore.create(mime_type='application/zip')
zip_data = zipit()
with files.open(zip_file, 'a') as f:
f.write(zip_data)
files.finalize(zip_file)
blob_key = files.blobstore.get_blob_key(zip_file)
blob_data = blobstore.BlobReader(blob_key).read()
# http://flask.pocoo.org/docs/api/
response = make_response(blob_data)
response.headers['Content-Type'] = 'application/zip'
response.headers['Content-Disposition'] = 'attachment; filename="loremipsum.zip"'
return response
Any help is much appreciated.