Java 必须从blob上载回调请求调用
我已经在Java中为blobstore API实现了这个GWT示例: 当POST通过客户端表单(在浏览器中)生成时,它可以正常工作 但是,现在我正在将文件(图像)发送到同一个/upload服务处理程序,但来自脱机程序(而不是浏览器)中的python请求: 我得到了以下例外 必须从blob上载回调请求调用 在(服务器端)的第一行中:Java 必须从blob上载回调请求调用,java,python,google-app-engine,gwt,Java,Python,Google App Engine,Gwt,我已经在Java中为blobstore API实现了这个GWT示例: 当POST通过客户端表单(在浏览器中)生成时,它可以正常工作 但是,现在我正在将文件(图像)发送到同一个/upload服务处理程序,但来自脱机程序(而不是浏览器)中的python请求: 我得到了以下例外 必须从blob上载回调请求调用 在(服务器端)的第一行中: Map blobs=blobstoreService.getUploads(req); List blobKeys=blobs.get(“myFile”); 我做
Map blobs=blobstoreService.getUploads(req);
List blobKeys=blobs.get(“myFile”);
我做错了什么?无论是从浏览器还是python应用程序,
/upload
处理程序都不允许您直接调用。相反,您的应用程序将需要进行两次调用:第一次调用服务器以获取临时URL,第二次调用上载到该URL,该URL将直接与blobstore连接。您的服务器应该使用blobstoreService.createUploadUrl
生成该临时URL,如您链接的文档的步骤1所述
在第二次调用(上载)过程中,blobstore将直接调用您的上载处理程序,通知您的应用程序有关新blob的信息。这就是blobstoreService.getUploads(req)
知道如何解释的
因此,python应用程序将进行2次调用,服务器也将处理2次请求。第一个请求直接来自python应用程序,只是请求url。第二个请求将在上载过程中发生,但实际上将直接来自blobstore。该
/upload
处理程序不允许您直接从浏览器或python应用程序调用。相反,您的应用程序将需要进行两次调用:第一次调用服务器以获取临时URL,第二次调用上载到该URL,该URL将直接与blobstore连接。您的服务器应该使用blobstoreService.createUploadUrl
生成该临时URL,如您链接的文档的步骤1所述
在第二次调用(上载)过程中,blobstore将直接调用您的上载处理程序,通知您的应用程序有关新blob的信息。这就是blobstoreService.getUploads(req)
知道如何解释的
因此,python应用程序将进行2次调用,服务器也将处理2次请求。第一个请求直接来自python应用程序,只是请求url。第二个请求将在上传过程中发生,但实际上直接来自blobstore。查找下面的示例代码,以了解flask和python的回调
项目>app.py
创建index.html文件并复制下面的代码
项目/模板>index.html
JSON输出
IDName电子邮件
{数据%中d的%s}
{{d['id']}{d['name']}{{d['email']}
{%endfor%}
{{result}}
使用cmd>python app.pyFind运行下面的示例代码,以了解使用flask和python的回调
项目>app.py
创建index.html文件并复制下面的代码
项目/模板>index.html
JSON输出
IDName电子邮件
{数据%中d的%s}
{{d['id']}{d['name']}{{d['email']}
{%endfor%}
{{result}}
使用cmd>python app.py运行亲爱的Eric!这很有道理:)谢谢你的快速回复。我会详细说明的!有没有更好的方法从python应用程序上传文件(图像)到blobstore?没有,这是最好的方法。这是谷歌自己在我们链接的文档中概述的方式。你可以考虑改用谷歌云存储,你可以用同样的方式上传到谷歌云存储(见文档),但也提供了其他选择。不过我对其他的选择不太熟悉,所以我恐怕这是我能帮到的最大的忙了。亲爱的埃里克!这很有道理:)谢谢你的快速回复。我会详细说明的!有没有更好的方法从python应用程序上传文件(图像)到blobstore?没有,这是最好的方法。这是谷歌自己在我们链接的文档中概述的方式。你可以考虑改用谷歌云存储,你可以用同样的方式上传到谷歌云存储(见文档),但也提供了其他选择。不过,我对其他的选择不太熟悉,所以我恐怕这是我能提供的最大帮助。
r = requests.post(url+'upload', files= {'myFile': open('fig.jpeg', 'rb')})
Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(req);
List<BlobKey> blobKeys = blobs.get("myFile");
import datetime
import time
import requests # to make get and post requests
import json
from flask import Flask, Response, request, render_template # request : is not same as requests its flask request on url
app = Flask(__name__)
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True
# 3. no need to give any url mapping for callback function,
# this will get call once demo() return back to hello() at post request call.
def callbacks(response, *args, **kwargs):
print(" -- callbacks starts -- : ", datetime.datetime.now().time())
if response.status_code == 200:
#print(response.text)
print(" -- callback end -- : ", datetime.datetime.now().time());
#return response
# return Response(response, status=200) do not return response here this will cause problem when
# 2. on post request call made from hello(): resp = requests.post(......)
@app.route('/demo' , methods=['POST']) # validate request of type methods=['POST']
def demo():
print("Post request received : ", datetime.datetime.now().time())
if request.method == 'POST': # check request of type post
time.sleep(5) # some sleep time to check if callback is working
print("Post response sent to callback : ", datetime.datetime.now().time())
# return response with data received from request.data and status = 200
return Response(request.data, headers={'Content-Type': 'application/json'}, status=200)
# 1. run this file and hit url localhost:5000/ to execute ( GET request)
@app.route('/', methods=['GET'])
def hello():
print("Hello start : ", datetime.datetime.now().time())
url = 'http://jsonplaceholder.typicode.com/users'; # url will return json list
data = requests.get(
url=url,
headers={'Content-Type': 'application/json'}
) # get request to get json data from url using requests lib
# check if flask request is get
if request.method == 'GET':
# post request to another url /demo with data as a json as argument ,
# imp : hooks to link callback function once /demo url returns response callback will start execute
resp = requests.post( url = 'http://localhost:5000/demo',
headers = {'Content-Type': 'application/json'},
json = data.json(),
hooks={'response': callbacks }
)
print("Hello End : ",datetime.datetime.now().time())
# render with html template with argument as data and result
return render_template('index.html', result=resp.json(), data=data.json())
# OR
# return Response(response='OK', status=resp.status_code) # return response with 200 or 'OK'
if __name__ == '__main__':
app.run(debug=False)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JSON OUTPUT</title>
</head>
<body>
<table border="1">
<tr><td>ID</td><td>Name</td><td>Email</td></tr>
{% for d in data %}
<tr><td>{{ d['id'] }}</td><td>{{ d['name'] }}</td><td>{{ d['email'] }}</td></tr>
{% endfor %}
</table>
<div>{{ result }}</div>
</body>
</html>