我正在尝试使用taskqueue和deferred将一个大型用户信息列表从json文件导入到数据存储。
User包含用户信息,包括来自不同应用程序的图像url。在导入过程中,应该抓取图像并将其上载到blob(在测试时效果很好)。
我被困在获取上传图像的blob_键上。
我认为它只发生在taskqueue/deferred中,因为我在一个“普通”的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而不是deferred。
编辑(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'

最佳答案

抬头!不赞成使用files api将文件写入Blobstore。我以前有过这个问题。我的代码在开发服务器(localhost)中运行得很好,但在App Engine服务器上出错。解决方案是通过Blobstore API在Google Cloud Storage中写入文件。

07-24 22:14