生成 Zip 文件并存储在 GAE BlobStore 中
我正在尝试生成一个 zip 文件并将其存储在 App Engine 的 Blobstore 中。目前,我没有从 Blobstore 获得有效的 zip 文件。不确定问题出在压缩、存储、检索或下载方面。
我根据以下问题的片段构建了代码。
存储在 Blobstore 中后,我让用户通过 Flask 应用程序下载它。
这是我想做的事情的要点。
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
非常感谢任何帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的大部分代码在 dev_appserver.py 的 webapp 处理程序中为我工作。我的下面的版本直接从 Blobstore 提供 zip 文件,而不是尝试将其读入应用程序实例 RAM 并提供服务。也许这就是你的意图?如果没有,请继续在读取和提供该值的代码中查找问题,因为我相信您正在 Blobstore 中创建有效的 Zip 文件。
Most of your code works for me in a webapp handler in dev_appserver.py. My version below serves the zip file directly out of the Blobstore, vs. trying to read it into app instance RAM and serve it. Maybe this is what you intended? If not, continue looking for the problem in your code that reads and serves the value, because I believe you're creating a valid Zip file in the Blobstore.