0

I have been working at writing simple code to upload the newest video in a folder to Dropbox with python. I almost got it working but am running into two issues. The big issue is while the video shows up on Dropbox it cannot be played and I believe the file is corrupted when uploaded. The other issue is that the filename is renamed which I would like to keep the filename because I added a timestamp to the name to easily record when the video was taken. -Thanks

dbx.dropbox.Dropbox('EmptyKey')
allfiles = glob.glob('/home/pi/Documents/CameraFeeds/*.h264')
newestfile = max(allfiles, key=os.path.getctime)
dropbox_path = os.path.join('/*')
with open(newestfile, 'rb') as f:
    dbx.files_upload(f.read(), dropbox_path, mute=True)
kwjamesblond
  • 39
  • 1
  • 1
  • 7
  • Hi, welcome to Stack Overflow. Can you tell me what the symbol `dbx` is? I'm assuming it's some object created by some python dropbox binding. Obviously omit your API keys, but I would like to be sure. – Danny Staple Feb 03 '18 at 17:36
  • [Cross-linking for reference: https://www.dropboxforum.com/t5/API-support/How-to-only-upload-newest-file-with-python-3/m-p/262839#M15346 ] – Greg Feb 05 '18 at 16:01

1 Answers1

1

I don't know the size of the videos you're trying to upload, but given a simple calculation of filesize for video length, it seems like you may be running into the 150MB-per-upload limit using just dbx.files_upload(). I think you'll have better results with the files_upload_session_start(), files_upload_session_append_v2(), and files_upload_session_finish() commands (found here).

As for the file being renamed when it gets to Dropbox, the issue is with your dropbox_path definition. When you call files_upload(), the f.read() argument is just the raw data to upload; the dropbox_path arg is the only indication of the expected filename. You'll need to include newestfile in your dropbox_path definition (be careful, though, if you're using Windows: os.path.join uses \\ to join the paths, which isn't compatible with Dropbox).

jshrimp29
  • 588
  • 6
  • 8
  • I am still learning python and am having a hard time understanding the documentation, is there anything you would recommend to help me figure out what is being said there? – kwjamesblond Feb 03 '18 at 18:44
  • @kwjamesblond It's not just you; that documentation is...not straightforward. Check out the link below for code that worked for me (via Python 3.6). Basically, you start a session, sequentially upload chunks of your file (the 150MB-per-upload still applies, so I have it set to append <150MB chunks), then finish the session with the last of your data and a path to save the file. https://www.dropbox.com/s/aljw1ppv80ywm0l/dropbox_large_upload_example.py?dl=0 – jshrimp29 Feb 03 '18 at 22:01
  • Hello I am still having the same issue with the code example you provided, I noticed there is no .h264 at the end of this file in Dropbox it just says binary when looking at this file. Could this be the issue? Also I had to lower the chunk_size to get the program to run properly. I am using a Raspberry Pi 3 with Raspbian so this may factor in. – kwjamesblond Feb 04 '18 at 01:38
  • @kwjamesblond Yep, I think that's the issue. I didn't include the changes to the `dropbox_path` definition I noted in my answer. You'll want to redefine this to `dropbox_path = os.path.join('/',newestfile)` (also I think the asterisk could be confusing to Dropbox). Oh nice...yeah I imagine you'll want to let Dropbox handle a bunch of small appends since you're on a Pi. – jshrimp29 Feb 04 '18 at 02:04
  • Thanks I was able to get the original code and the one you commented with to work now, thanks a lot for the great help. – kwjamesblond Feb 04 '18 at 22:30