python - 是否可以使用 App Engine 生成并返回 ZIP 文件?

标签 python google-app-engine zip in-memory

我有一个非常适合 Google App Engine 的小项目。实现它取决于生成 ZIP 文件并将其返回的能力。

由于 App Engine 的分布式特性,据我所知,ZIP 文件无法在传统意义上的“内存中”创建。它基本上必须在单个请求/响应周期中生成和发送。

Python zip 模块是否存在于 App Engine 环境中?

最佳答案

zipfile可在 appengine 获得并重新设计 example如下:

from contextlib import closing
from zipfile import ZipFile, ZIP_DEFLATED

from google.appengine.ext import webapp
from google.appengine.api import urlfetch

def addResource(zfile, url, fname):
    # get the contents      
    contents = urlfetch.fetch(url).content
    # write the contents to the zip file
    zfile.writestr(fname, contents)

class OutZipfile(webapp.RequestHandler):
    def get(self):
        # Set up headers for browser to correctly recognize ZIP file
        self.response.headers['Content-Type'] ='application/zip'
        self.response.headers['Content-Disposition'] = \
            'attachment; filename="outfile.zip"'    

        # compress files and emit them directly to HTTP response stream
        with closing(ZipFile(self.response.out, "w", ZIP_DEFLATED)) as outfile:
            # repeat this for every URL that should be added to the zipfile
            addResource(outfile, 
                'https://www.google.com/intl/en/policies/privacy/', 
                'privacy.html')
            addResource(outfile, 
                'https://www.google.com/intl/en/policies/terms/', 
                'terms.html')

关于python - 是否可以使用 App Engine 生成并返回 ZIP 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/583791/

相关文章:

python - 如何通过从一列中选择 NaN 值来创建数据框的子集?

Python:解析 INI 风格的配置文件?

python - 索引搜索 : trade accuracy for performance

google-app-engine - 如何在 Google App Engine 中使用 sbt?

java - GAE 上的非交互式用户身份验证

matlab - 如何在matlab中压缩目录同时排除文件/文件夹

python - 如何使用 MultiIndex 的相关级别对 MultiIndex DataFrame 进行切片

java - 使用 maven 的带有 struts2、spring 和 JPA 的谷歌应用程序引擎

Java 从 ZIP 中间检索内容的最有效方法

c# - DotNetZip 中大量文件的压缩问题