I have the following Python command line program that works just fine with
my local GAE developer mode server, but when I deploy it to the actual GAE
servers, it doesn't work, it doesn't find any blobs in the request?
Here is my "upload" servlet:
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.
*
* @param request
* @param response
*
* @throws ServletException
* @throws IOException
*/
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);
}
}
Here is my command line python code that works with developer mode just
fine, but causes the above servlet code to log "BlobKey was null!" on every
request.
#!/usr/bin/env python
import mimetools
import mimetypes
import itertools
import urllib2
class MultiPartForm(object):
"""Accumulate the data to be used when posting a form."""
def __init__(self):
self.form_fields = []
self.files = []
self.boundary = mimetools.choose_boundary()
return
def get_content_type(self):
return 'multipart/form-data; boundary=%s' % self.boundary
def add_field(self, name, value):
"""Add a simple field to the form data."""
self.form_fields.append((name, value))
return
def add_file(self, fieldname, filename, fileHandle, mimetype=None):
"""Add a file to be uploaded."""
body = fileHandle.read()
if mimetype is None:
mimetype = mimetypes.guess_type(filename)[0] or
'application/octet-stream'
self.files.append((fieldname, filename, mimetype, body))
return
def __str__(self):
"""Return a string representing the form data, including attached
files."""
# Build a list of lists, each containing "lines" of the
# request. Each part is separated by a boundary string.
# Once the list is built, return a string where each
# line is separated by '\r\n'.
parts = []
part_boundary = '--' + self.boundary
# Add the form fields
parts.extend(
[ part_boundary,
'Content-Disposition: form-data; name="%s"' % name,
'',
value,
]
for name, value in self.form_fields
)
# Add the files to upload
parts.extend(
[ part_boundary,
'Content-Disposition: file; name="%s"; filename="%s"' % \
(field_name, filename),
'Content-Type: %s' % content_type,
'',
body,
]
for field_name, filename, content_type, body in self.files
)
# Flatten the list and add closing boundary marker,
# then return CR+LF separated data
flattened = list(itertools.chain(*parts))
flattened.append('--' + self.boundary + '--')
flattened.append('')
return '\r\n'.join(flattened)
# my-app is replaced with my actual application name
f =
urllib2.urlopen('http://my-app.appspot.com/upload<http://localhost:8080/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)
print request.get_data()
print
print urllib2.urlopen(request).read()
What am I missing for this to work?
--
You received this message because you are subscribed to the Google Groups
"Google App Engine" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine?hl=en.