Skip to content

Instantly share code, notes, and snippets.

@faisal-w
Created May 4, 2014 07:09
Show Gist options
  • Save faisal-w/840e440271a9292385e9 to your computer and use it in GitHub Desktop.
Save faisal-w/840e440271a9292385e9 to your computer and use it in GitHub Desktop.
Example how to upload file to server using python, accept http multipart request. Work if uploaded file is less than 1.5 mb.
import requests
def upload_file_to_gcs():
url = 'http://127.0.0.1:8500/save-data-to-gcs/'
f = {'file': ('Product_Master.csv', open('C:/Projects/bf/Product_Master.csv', 'rb')), 'file_name': 'Product_Master.csv'}
r = requests.post(url, files=f)
print r
upload_file_to_gcs()
def save_data_to_gcs(request):
file_name = '/gs/bucket-name/' + request.FILES['file'].name # change bucket/object names to suit your needs
writable_file_name = files.gs.create(file_name, mime_type='application/octet-stream',
acl='public-read')
with files.open(writable_file_name, 'a') as f:
f.write(request.FILES['file'].read())
files.finalize(writable_file_name)
return HttpResponse('', mimetype='application/text')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment