I've managed to get email attachments onto Amazon S3 from a GAE incoming email, but does anyone know a technique to get an attachment, like an image, into the blobstore.
Any help would be much appreciated.
Code so far (with help from Alex)
upload_url = blobstore.create_upload_url('/upload')
msg = MIMEMultipart()
msg.set_type('multipart/form-data')
msg.set_payload({'file': content})
result = urlfetch.fetch(upload_url, payload=urllib.urlencode(msg), method=urlfetch.POST, headers={'Content-Type': 'multipart/form-data'})
To receive mail in your GAE app, follow the docs here: in particular, you'll get a instance of class InboundEmailMessage
with an attachments
attribute which, and I quote:
is a list of file attachments, possibly empty. Each value in the list is a tuple of two elements: the filename and the file contents.
Then, per these GAE docs, you "create an upload URL" and in your upload handler (typically a subclass of BlobstoreUploadHandler) you use get_upload
to get BlobInfo instances and put their metadata somewhere that will later let you fetch them back as your app may require.
Finally, you POST
the data (that you have from attachments
, above) to your own freshly generated "upload URL", e.g. using urlfetch.fetch (with method-POST
and a payload in standard application/x-www-form-urlencoded
encoding for the "form" that the user would be filling in if they were uploading the data directly, which is the "normal" way to put data in the blobstore -- e.g. you can use urllib.urlencode to prepare the payload).
That "self-POST" will be using another instance of your app to "receive" the data into the blobstore (while the instance that received the email waits, as fetch
is synchronous).
App Engine (version 1.4.3) allows you to directly write data to the blobstore.
You no longer need to use the upload url method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With