【问题标题】:Replacing a blobstore file替换 blobstore 文件
【发布时间】:2012-01-22 01:17:06
【问题描述】:

我将 blobstore 中的静态 KML 作为我保存到 blobstore 的文件提供

class KMLHandler2(blobstore_handlers.BlobstoreDownloadHandler):

    def get(self):
        resource = 'AMIfv965WtxAc_rWOVjSSx423_oe6f-g5obWYNKX5scg-1gqvISyaZCnv6lRaqro2wOVNOogttyMOylFLsRYZ3Y9UYIe-A69vAt4pdJB2-SHUcdVEM2v0XVLxzT3fTlxwXQVhzmsHPwALH_rCSFIvmYcuV37asVD0Q'
        resource = str(urllib.unquote(resource))
        blob_info = blobstore.BlobInfo.get(resource)
        self.send_blob(blob_info)

这可行,但我想定期更新文件。该文件是从/list.kml 生成的,所以我可以直接从那里读取它,但是它会超时,所以我的计划是让我的第一个appenine 任务从list.kml 读取文件并使用相同的密钥将其写入blobstore,但是当唯一的例子是如何创建一个新文件时,我该怎么做?我在代码中有更新数据层的地方,而且不是很频繁。你能建议我更新文件时应该怎么做吗?我想我希望密钥和 id 相同并替换旧的 blob,而不是编写新的并刷新密钥。你能帮助我吗?应用程序引擎文档说如何编写一个 blobstore 文件,但只是一个新文件,我不知道如何编辑或替换一个文件,因为它是关键,我认为这是我需要做的任务或 cron 工作所以我请求你的帮助。

我可以像文档中那样制作一个处理程序,但那是用于创建文件,而不是修改/替换文件:

class CreateKMLHandler(webapp2.RequestHandler):
    def get(self):
        # Create the file
        file_name = files.blobstore.create(mime_type='application/octet-stream')

        # Open the file and write to it
        with files.open(file_name, 'a') as f:
          f.write('data')

        # Finalize the file. Do this before attempting to read it.
        files.finalize(file_name)

        # Get the file's blob key
        blob_key = files.blobstore.get_blob_key(file_name)

谢谢

更新

我尝试使用此代码创建一个新文件,但它得到一个截止日期错误 applicationerror 5,我认为这是一个超时问题。我怎样才能让它成为一项任务?

class CreateKMLHandler(webapp2.RequestHandler):
    def get(self):
        # Create the file
        file_name = files.blobstore.create(mime_type='application/octet-stream')

        url = 'http://montaoproject.appspot.com/list.kml'

        result = urlfetch.fetch(url)
        if not result.content:
            return

        # Open the file and write to it
        with files.open(file_name, 'a') as f:
          f.write(result.content)

        # Finalize the file. Do this before attempting to read it.
        files.finalize(file_name)

        # Get the file's blob key
        blob_key = files.blobstore.get_blob_key(file_name)
        self.out.write(blob_key)

【问题讨论】:

    标签: python google-app-engine google-cloud-datastore kml python-2.7


    【解决方案1】:

    一旦写入 blob,就无法更改(只能读取或删除)。您可以使用数据存储实体来跟踪与给定 KML“文档”关联的当前 blob 键。

    您可能还对 Cloud Storage API (http://code.google.com/appengine/docs/python/googlestorage/overview.html) 感兴趣,它允许您通过创建具有相同名称的新对象来覆盖“桶”对象。

    【讨论】:

    • 好的,那我就新建一个 blob。谢谢你的信息。
    猜你喜欢
    • 2017-06-19
    • 2011-11-03
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 2011-12-25
    相关资源
    最近更新 更多