Advertisement
Guest User

Demo of GCS resumable upload of unknown content size

a guest
May 28th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. import requests
  2.  
  3. url = 'https://www.googleapis.com/upload/storage/v1/b/my-bucket/o?alt=json&name=test-file2&uploadType=resumable'
  4.  
  5. auth = ('Authorization',
  6.         'Bearer ya29.....g')
  7.  
  8. resp = requests.post(url, headers=dict([auth, ('X-Upload-Content-Type', 'text/plain')]))
  9. assert resp.status_code == 200
  10.  
  11. url = resp.headers['location']
  12.  
  13. # Note using bytes */* in content range
  14. resp = requests.post(url, headers=dict([auth, ('Content-Range', 'bytes */*')]))
  15. assert resp.status_code == 308
  16.  
  17. # send first full 256K chunk, still without specifying full content length
  18. resp = requests.post(url, headers=dict([auth, ('Content-Range', 'bytes 0-262143/*'), ('Content-Type', 'text/plain')]), data='A' * 262144)
  19. assert resp.status_code == 308
  20.  
  21. # Just for fun let's ask where we are in the upload. Still not specifying full content length.
  22. resp = requests.post(url, headers=dict([auth, ('Content-Range', 'bytes */*')]))
  23. assert resp.status_code == 308
  24. assert resp.headers['range'] == 'bytes=0-262143'
  25.  
  26. # upload final chunk of 10 bytes, and specify full content length for the first time
  27. resp = requests.post(url, headers=dict([auth, ('Content-Range', 'bytes 262144-262153/262154'), ('Content-Type', 'text/plain')]), data='A' * 10)
  28. assert resp.status_code == 200
  29. assert resp.json()['size'] == '262154'
  30.  
  31. print 'Grandi successi!!!'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement