datastore 使用 BlobKey 下载

datastore use BlobKey to download

在 google appengine 数据存储中,有一个 BlobKey(标记为 csv)。密钥采用以下格式:encoded_gs_file:we1o5o7klkllfekomvcndhs345uh5pl31l。我想提供一个下载按钮来保存这些信息。我的问题是,我可以用来访问它的端点是什么。有关 BlobKey 的更多信息如下。

Web 应用程序正在 运行 使用 dev_appserver.py 并使用 python 2.7 (Django) 作为后端。当前,存在一个按钮,但单击它时,它 returns 出现 404 错误。按钮提供的下载link是:

    https://localhost:8080/data?key=encoded_gs_file:dwndjndwamwljioihkm

我的问题是,如何使用 blobkey 生成可以下载的 url;或者我如何检查我的代码库以了解我可以使用的 url 是如何生成的?

class BlobstoreDataServer(blobstore_handlers.BlobstoreDownloadHandler):
    def get(self):
        k = str(urllib.unquote(self.request.params.get('key','')))
        logging.debug(k)
        blob_info = blobstore.BlobInfo.get(k)
        logging.debug(blob_info)
        if (not blob_info) or (not blob_info.size):
            self.error(404)
            return
        self.response.headers['X-Blob-Size'] = str(blob_info.size)
        self.response.headers['Content-Type'] = blob_info.content_type
        self.response.headers['Content-Disposition'] = (u'attachment; filename=%s' % blob_info.filename).encode('utf-8')
        self.send_blob(blob_info)

编辑:新图像

你有路由 /data 的请求处理程序吗?

from google.appengine.ext import blobstore

class DisplayBlob(blobstore_handlers.BlobstoreDownloadHandler):

    def get(self):
        blob_key = self.request.GET['key']

        self.send_blob(ndb.BlobKey(blob_key))

        self.response.headers['Content-Type'] = 'text/plain'

编辑:

好的,所以 404 可能是你通过这条线抛出的:self.error(404) 对吗?在前面添加一个 logging.warn('BlobstoreDataServer is throwing 404') 以确保。您是否也看到 logging.debug(k) 这一行打印(我想确认 BlobstoreDataServer 甚至被击中)?您可能需要执行 logging.getLogger().setLevel(logging.DEBUG) 才能看到它。

所以这意味着 blobstore.BlobInfo.get(k) 返回 None。专注于确保它首先起作用,您可以在交互式控制台中执行此操作。

  1. 转到http://localhost:8000/blobstore

  1. 打开其中一个并复制密钥(encoded_gs_file:dwndjndwamwljioih...)

  1. 转到交互式控制台 (http://localhost:8000/console) 并输入此代码并点击 'EXECUTE' 并确保它能够找到它:

如果该步骤不起作用,则说明您的 dev_appserver.py 的 blobstore 模拟器出现问题

  1. 如果可行,则只需在下载结束时手动粘贴相同的密钥 link: https://localhost:8080/data?key=<paste_encoded_gs_file_key_here>

如果此步骤不起作用,则说明您的下载处理程序出了问题,也许这一行正在以某种方式转换密钥 str(urllib.unquote(self.request.params.get('key','')))

如果此步骤有效,那么生成此 link https://localhost:8080/data?key=... 的代码出现问题,也许您实际上写入的 gcs_filename 不同于您正在构建的 gcs_filename不同 BlobKey for.