Search code examples
pythondropbox

Python3:How to keep-alive dropbox.files_upload() . So to download and upload at the same time?


I want to download a file from the internet and upload it to dropbox at the same time . I am downloading file as chunks and after every completed chunk I want it to upload it to dropbox.

import multiprocessing as m
import requests as rr
import dropbox
url='http://www.onirikal.com/videos/mp4/battle_games.mp4'
db=dropbox.Dropbox(Accesstoken)
def d(url):
    r=rr.get(url,stream=True)
    with open('file.mp4','wb')as f:
        for a in r.iter_content(chunk_size=1000000):
            if a:
                f.write('')
                f.write(a)

def u():
    try:
        with open('file.mp4','rb')as ff:
            db.files_upload(ff.read(),'/file.mp4')
    except FileNotFoundError:
        pass
if __name__=='__main__':
    p=m.Pool()
    re= p.apply_async(d,[url])
    ree=p.apply_async(u)
    re.get(timeout=10)
    ree.get(timeout=10)

But the uploaded file is having a size 0byte

EDIT

I am using f.write('') to save space on the server as i am only getting 512mb as storage


Solution

  • You should not use multiprocessing for this. Instead, simply download the chunks as you are already doing, and immediately call upload_chunk(a, len(a), offset, upload_id). You do not need the temporary file on disk.