Last active
December 19, 2015 05:08
-
-
Save romanlevin/5901664 to your computer and use it in GitHub Desktop.
Python script for uploading an image to a Google App Engine Dev Server's Blobstore from an external URL.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
DEV_SERVER_HOST = 'localhost' | |
INSTANCE_PORT = 8080 | |
UPLOAD_PATH = 'admin/upload' | |
def upload_image(url): | |
def get_upload_url(): | |
cookies = {'dev_appserver_login': 'test@example.com:True'} | |
resp = requests.get( | |
'http://%s:%d/%s' % (DEV_SERVER_HOST, INSTANCE_PORT, UPLOAD_PATH), | |
cookies=cookies) | |
upload_url = resp.json()['message'] | |
return upload_url | |
response = requests.get(url) | |
content_type = response.headers['content-type'] | |
file_extension = content_type.split('/')[1] | |
image = response.content | |
files = {'file': ('image.%s' % file_extension, image)} | |
upload_url = get_upload_url() | |
response = requests.post(upload_url, files=files) | |
return response.json() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment