Skip to content

Instantly share code, notes, and snippets.

@nbari
Last active October 7, 2024 16:10

Revisions

  1. nbari renamed this gist Nov 6, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. nbari renamed this gist Nov 6, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions chunk upload → gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    #!/usr/bin/env python

    import os
    import requests
    import uuid
  3. nbari created this gist Nov 6, 2013.
    41 changes: 41 additions & 0 deletions chunk upload
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    import os
    import requests
    import uuid
    from random import randint
    from uuid import uuid4

    def read_in_chunks(file_object, chunk_size=65536):
    while True:
    data = file_object.read(chunk_size)
    if not data:
    break
    yield data

    def main(file, url):
    content_name = str(file)
    content_path = os.path.abspath(file)
    content_size = os.stat(content_path).st_size

    print content_name, content_path, content_size

    f = open(content_path)

    index = 0
    offset = 0
    headers = {}

    for chunk in read_in_chunks(f):
    offset = index + len(chunk)
    headers['Content-Type'] = 'application/octet-stream'
    headers['Content-length'] = content_size
    headers['Content-Range'] = 'bytes %s-%s/%s' % (index, offset, content_size)
    index = offset
    try:
    r = requests.put(url, data=chunk, headers=headers)
    print "r: %s, Content-Range: %s" % (r, headers['Content-Range'])
    except Exception, e:
    print e

    if __name__ == '__main__':
    url = 'http://localhost:8080/test_upload/%s' % str(uuid4())
    main('images/test_image_%d.jpg' % randint(1,3), url)