gpt4 book ai didi

python - 任务队列/延迟中的 blobstore

转载 作者:行者123 更新时间:2023-11-28 18:39:10 27 4
gpt4 key购买 nike

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

User 包含用户信息,包括来自不同应用的图像 url。在导入过程中,应该抓取图像并将其上传到 blob(测试时工作正常)。

我无法获取上传图像的 blob_key。而且我认为它只发生在 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'

最佳答案

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

关于python - 任务队列/延迟中的 blobstore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28812896/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com