任务队列/延迟中的blobstore

时间:2015-03-02 15:01:33

标签: python google-app-engine blobstore task-queue google-app-engine-python

我正在尝试使用taskqueue和deferred将json文件中的大型用户信息列表导入数据存储区。

User包含用户的信息,包括来自其他应用的图片网址。在导入过程中,应该抓取图像并将其上传到blob(测试时效果很好)。

我无法获取上传图片的blob_key。 而且我认为它只发生在一个任务队列/延迟中,因为我在一个正常的'中尝试了它。 GET请求处理程序,它工作正常。

这是我的经纪人:

class MigrationTask(BaseHandler):
    def post(self):
        if not self.request.get('file'):
            return
        json_data = open(self.request.get('file'))
        data = json.load(json_data)
        json_data.close()

        for datum in data['results']:
            deferred.defer(push_user_to_db, datum)

这是我的职能:

@ndb.transactional(xg=True)
def _push_user_to_db(profilePicture=None, ...):
    if profilePicture:
        if 'url' in profilePicture:
            con = urlfetch.fetch(image_url)
            if con.status_code == 200:
                file_name = files.blobstore.create(mime_type='application/octet-stream')
                with files.open(file_name, 'a') as f:
                    f.write(con.content)
                files.finalize(file_name)
                blob_key = files.blobstore.get_blob_key(file_name)  # this part is where it errs
                image_url = images.get_serving_url(file_name)

    # some codes here...

def push_user_to_db(kwargs):
    _push_user_to_db(**kwargs)

回溯的一部分:

    blob_key = files.blobstore.get_blob_key(file_name)

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\files\blobstore.py", line 132, in get_blob_key

    namespace='')])[0]

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\datastore.py", line 654, in Get

    return GetAsync(keys, **kwargs).get_result()

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\datastore.py", line 629, in GetAsync

    return _GetConnection().async_get(config, keys, local_extra_hook)

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\datastore\datastore_rpc.py", line 1574, in async_get

    pbs = [key_to_pb(key) for key in keys]

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\model.py", line 653, in key_to_pb

    return key.reference()

AttributeError: 'Key' object has no attribute 'reference'

PS:我也尝试过taskqueue而不是延迟。

修改(1):

这是追溯:

ERROR    2015-03-03 06:32:44,720 webapp2.py:1552] 'Key' object has no attribute 'reference'

Traceback (most recent call last):

  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1535, in __call__

    rv = self.handle_exception(request, response, e)

  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1529, in __call__

    rv = self.router.dispatch(request, response)

  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1278, in default_dispatcher

    return route.handler_adapter(request, response)

  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1102, in __call__

    return handler.dispatch()

  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 572, in dispatch

    return self.handle_exception(e, self.app.debug)

  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 570, in dispatch

    return method(*args, **kwargs)

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\deferred\deferred.py", line 310, in post

    self.run_from_request()

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\deferred\deferred.py", line 305, in run_from_request

    run(self.request.body)

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\deferred\deferred.py", line 147, in run

    return func(*args, **kwds)

  File "C:\project directory\migration.py", line 141, in push_user_to_db

    _push_user_to_db(**kwargs)

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\utils.py", line 179, in inner_wrapper

    return wrapped_decorator(func, args, kwds, **options)

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\model.py", line 3759, in transactional

    func, args, kwds, **options).get_result()

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\tasklets.py", line 325, in get_result

    self.check_success()

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\tasklets.py", line 371, in _help_tasklet_along

    value = gen.send(val)

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\context.py", line 999, in transaction

    result = callback()

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\model.py", line 3767, in <lambda>

    return transaction_async(lambda: func(*args, **kwds), **options)

  File "C:\project directory\migration.py", line 56, in _push_user_to_db

    blob_key = files.blobstore.get_blob_key(file_name)

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\files\blobstore.py", line 132, in get_blob_key

    namespace='')])[0]

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\datastore.py", line 654, in Get

    return GetAsync(keys, **kwargs).get_result()

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\datastore.py", line 629, in GetAsync

    return _GetConnection().async_get(config, keys, local_extra_hook)

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\datastore\datastore_rpc.py", line 1574, in async_get

    pbs = [key_to_pb(key) for key in keys]

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\model.py", line 653, in key_to_pb

    return key.reference()

AttributeError: 'Key' object has no attribute 'reference'

1 个答案:

答案 0 :(得分:1)

抬起头来!使用文件api将文件写入Blobstore已被弃用。我以前遇到过这个问题。我的代码在开发服务器(localhost)中运行得非常好,但在App Engine服务器上出错了。解决方案是通过Blobstore API在Google Cloud Storage中编写文件。