开发者

Uploading to BlobStore in Google App Engine from a Command Line Java Application

Here is my server side code for my 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 was null!");
            response.sendRedirect("/error.html");
        }
        else
        {
            response.sendRedirect("/image?blob-key=" + blobKey.getKeyString());
        }
    }

    /**
     * Generates the custom single use blobstore URL's that are needed to upload to the 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 the following code to work against my local development mode server, without the authentication code so I know the multipart/form code is working fine, with the authentication code, it 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
 开发者_如何学C   raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 302: Found

Moved from Java to Python for the 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 just want a simple command line tool that takes a file and posts it to the BlobStore. I can't find a single COMPLETE example anywhere on the internet. There are lots of examples that do all the work on GAE, but none that are command line clients that do the POST of the FORM from a separate client.


Since version 1.4.3 There is an experimental API to write directly to the blobstore.
It saves you from the need to upload to the blobstore using POSTs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜