Anonymous file upload with Python

There are many file and image sharing web sites but we need one that allows upload via an API. anonymousfiles.io provides an easy API for an anonymous file upload so lets use that. Images are hosted and displayed on the service so we can use it for file hosting too.

Below you can see an example of a CURL request to upload an image file, the API returns us the information we need to access the file upload.

$ curl -F file=@anonymous_activists.jpg https://api.anonymousfiles.io/
{"url": "https://anonymousfiles.io/qZB1DXsM/", "id": "qZB1DXsM", "name": "anonymous_activists.jpg", "size": 114230, "mime_type": "image/jpeg"}

So now we can translate the CURL request into Python using the requests library.

import requests
r =  requests.post('https://api.anonymousfiles.io', files={'file': open('/Users/myself/anonymous_activists.jpg', 'rb')})
print(r.text)
#{"url": "https://anonymousfiles.io/qZB1DXsM/", "id": "qZB1DXsM", "name": "anonymous_activists.jpg", "size": 114230, "mime_type": "image/jpeg"}

We grab the url param from the response and we can see our image uploaded and hosted in all its glory.