python - Uploading to BlobStore in Google App Engine from a Command Line Java Application -
here server side code blobstore uploading command line application:
public class uploadservlet extends httpservlet { private static final logger log = logger.getlogger(uploadservlet.class.getname()); private final blobstoreservice bs = blobstoreservicefactory.getblobstoreservice(); protected void dopost(final httpservletrequest request, final httpservletresponse response) throws servletexception, ioexception { final map<string, blobkey> blobs = bs.getuploadedblobs(request); final blobkey blobkey = blobs.get("blob"); if (blobkey == null) { log.severe("blobkey null!"); response.sendredirect("/error.html"); } else { response.sendredirect("/image?blob-key=" + blobkey.getkeystring()); } } /** * generates custom single use blobstore url's needed upload blobstore programmatically. */ protected void doget(final httpservletrequest request, final httpservletresponse response) throws servletexception, ioexception { final string uploadurl = bs.createuploadurl("/upload"); final printwriter pw = response.getwriter(); response.setcontenttype("text/plain"); pw.write(uploadurl); } }
i have gotten following code work against local development mode server, without authentication code know multipart/form
code working fine, authentication code, fails with:
r = opener.open(request) file "c:\python26\lib\urllib2.py", line 397, in open response = meth(req, response) file "c:\python26\lib\urllib2.py", line 510, in http_response 'http', request, response, code, msg, hdrs) file "c:\python26\lib\urllib2.py", line 435, in error return self._call_chain(*args) file "c:\python26\lib\urllib2.py", line 369, in _call_chain result = func(*args) file "c:\python26\lib\urllib2.py", line 518, in http_error_default raise httperror(req.get_full_url(), code, msg, hdrs, fp) httperror: http error 302: found
moved java python command line client:
f = urllib2.urlopen('http://myapp.appspot.com/upload') bloburl = f.read(1024) print bloburl print image = file('120.jpg', 'r') form = multipartform() form.add_file('blob', 'blob', image, 'image/jpeg') request = urllib2.request(bloburl) body = str(form) request.add_header('content-type', form.get_content_type()) request.add_header('content-length', len(body)) request.add_data(body) opener = auth.get_auth_opener('myapp', 'username', 'password') r = opener.open(request) data = r.read() print data
i want simple command line tool takes file , posts blobstore. can't find single complete example anywhere on internet. there lots of examples work on gae, none command line clients post
of form
separate client.
since version 1.4.3 there experimental api write directly blobstore.
saves need upload blobstore using posts.
Comments
Post a Comment