生成 Zip 文件并存储在 GAE BlobStore 中

发布于 2025-01-07 05:38:17 字数 1326 浏览 3 评论 0原文

我正在尝试生成一个 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.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_蜘蛛 2025-01-14 05:38:17

你的大部分代码在 dev_appserver.py 的 webapp 处理程序中为我工作。我的下面的版本直接从 Blobstore 提供 zip 文件,而不是尝试将其读入应用程序实例 RAM 并提供服务。也许这就是你的意图?如果没有,请继续在读取和提供该值的代码中查找问题,因为我相信您正在 Blobstore 中创建有效的 Zip 文件。

#!/usr/bin/env python

import StringIO
import zipfile
from google.appengine.api import files
from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp import util

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()

class MainHandler(blobstore_handlers.BlobstoreDownloadHandler):
    def get(self):
        k = self.request.get('key')
        if k:
            self.send_blob(k)
            return

        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)

        self.response.out.write('<a href="/getzip?key=%s">get zip</a>' % blob_key)


application = webapp.WSGIApplication([('/getzip', MainHandler)])

def main():
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()

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.

#!/usr/bin/env python

import StringIO
import zipfile
from google.appengine.api import files
from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp import util

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()

class MainHandler(blobstore_handlers.BlobstoreDownloadHandler):
    def get(self):
        k = self.request.get('key')
        if k:
            self.send_blob(k)
            return

        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)

        self.response.out.write('<a href="/getzip?key=%s">get zip</a>' % blob_key)


application = webapp.WSGIApplication([('/getzip', MainHandler)])

def main():
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文