0

I'm using twitter from python in an environment where I can't store files.

I get a HTTP POST with a text and an image and want to create a tweet from this data without writing a local file (it's zappa on AWS api environment).

Tweepy only allows filenames, which does not work for me.

python-twitter seems to have something like that, but I can't find a doc for this.

Should I just send POST requests to twitter for uploading the images? Is there a simpler way?

2
  • How are you receiving the images? And if can't you make permanent writes, find a way to either push it online by serializing it, that is, if you cannot find the doc. Commented Dec 19, 2017 at 22:51
  • I get it as HTTP Post in base64, so very perfect to send to Twitter, but tweepy wants to make it simple and only allows a filename. Commented Dec 19, 2017 at 22:52

1 Answer 1

2

Try passing a io.BytesIO to tweepy's API.update_with_media as file.

filename – The filename of the image to upload. This will automatically be opened unless file is specified

...

file – A file object, which will be used instead of opening filename. filename is still required, for MIME type detection and to use as a form field in the POST data

Edit: It looks like you have the image data base64 encoded. You can use base64.b64decode to decode it before creating the io.BytesIO:

file = io.BytesIO(base64.b64decode(base64_data))
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, on the third check I found out that I do get multipart-formdata, only the example looked like some encoded data. So i already get file handles.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.