1

在我的应用程序中,我正在创建一个在 App Engine 仪表板中正确上传的 blob。但是,这个创建的文件需要通过电子邮件发送给相应的人。为此,我需要文件本身作为附件或此人可以下载的静态 URL。我无法弄清楚如何从 blobkey 获取静态 URL。

这是创建文件的代码,但没什么特别的:

    file_name = files.blobstore.create(mime_type='text/csv')
with files.open(file_name, 'a') as f:
  f.write(dataset)
files.finalize(file_name)
blob_key = files.blobstore.get_blob_key(file_name)
blob_info = blobstore.BlobInfo.get(blob_key)

new_url = blob_key.urlsafe()
4

3 回答 3

5

如果要提供文件,请查看Blobstore 概述 - 提供 Blob

如果您想将其作为附件发送,请参阅附件文档。您将需要获取 blob 的内容,然后将其附加到邮件中。

from google.appengine.ext import blobstore

# blob_key = ...

# Instantiate a BlobReader for a given Blobstore value.
blob_reader = blobstore.BlobReader(blob_key)

# Read the entire value into memory. This may take a while depending
# on the size of the value and the size of the read buffer, and is not
# recommended for large values.
blob_contents = blob_reader.read()
于 2012-09-17T17:55:30.460 回答
0

查看AppEngine Blobstore 文档,他们很好地解释了如何检索和使用 Blobstore 条目。这是文档中的示例。

from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
  def get(self, resource):
    resource = str(urllib.unquote(resource))
    blob_info = blobstore.BlobInfo.get(resource)
    self.send_blob(blob_info)
于 2012-09-17T17:55:43.690 回答
0

我的用例不同,但我通过查找 url 路径从 blobstore 提供静态内容。这是模型和get函数。

class StaticContent(db.Model):
    body = db.BlobProperty()
    content_type = db.StringProperty()
    last_modified = db.DateTimeProperty(required=True, auto_now=True)
    etag = aetycoon.DerivedProperty(lambda x: hashlib.sha1(x.body).hexdigest())

def get(path):  
    return StaticContent.get_by_key_name(path)

您可以在我的 git hub 存储库中的 master 分支的 step1 标记上看到我的 wepapp2 处理程序

有关详细说明,您还可以查看Nick Johnson 关于通过 blogstore 提供静态内容的博客文章

于 2012-09-18T04:34:19.463 回答