开发者

Is is possible to get an email attachment into App Engine's Blobstore?

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'开发者_如何学Python: 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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜