cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Re: How to only upload newest file with python 3?

How to only upload newest file with python 3?

kwjamesblond
Explorer | Level 4

Hello I am working on an app and have developed code for uploading video that I have taken with a camera setup.  The problem I have been facing is that using python 3 I am only capable of uploading a single folder and every time a new video is added every video is reoploaded every time even if it is already present in dropbox.  I was hopeing to get to the point were only my code uploads the newest video every time as uploading to dropbox is quite slow and with six plus video files this becomes a big time waste. 

My code is below

 

for dir, dirs, files in os.walk(rootdirx): #Code to upload folder with video info
for file in files:
try:
file_path = os.path.join(dir,file)
dest_path = os.path.join('/', file)
print ('Uploading %s to %s' % (file_path, dest_path))
with open(file_path, 'rb') as f:
dbx.files_upload(f.read(), dest_path, mute=True)
except Exception as err:
print ('Failed to upload %s\n%s' % (file, err))
print('Finsihed uploads')

5 Replies 5

Greg-DB
Dropbox Staff

It looks like you already have the code for uploading to Dropbox, so you'd need to write some more code for decided whether or not to do so for any particular local file. That's not specific to the Dropbox API though, so I'm afraid I can't offer much help in that regard.

 

For example, you may want to retrieve the local created/modified metadata for each local file and sort the list, and then only upload the last one accordingly.

 

Or, you can decide whether or not to upload a file based on whether or not it's already listed on Dropbox. In that case, you can use files_list_folder and files_list_folder_continue to list what's currently on Dropbox and compare that to the local list.

kwjamesblond
Explorer | Level 4

In an effort to keep things as simple as possible I have added two lines of python code that scans and save the newest file in the folder than saves it to a varible.  Next I added some basic code to upload the file to dropbox but I am running into two problems.  The main issue is while the files are uploaded and show up on dropbox they are not completly uploaded and cannot be played and I think they are corrupt.  The other issue is I am unable to preserve the filenames which is important becuase I added a timestamp to the video names to easily record when they were filmed.

 

allfiles = glob.glob('/home/pi/Documents/CameraFeeds/*')
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)

Greg-DB
Dropbox Staff
[Cross-linking for reference: https://stackoverflow.com/questions/48600061/simple-code-to-upload-a-video-to-dropbox-with-python ]

It looks like you already got this sorted out using upload sessions in your StackOverflow post.

herbarium
New member | Level 2

A couple of python-API questions related to this:

- How do you get the latest created/modified time for a dropbox-hosted file? (in order to compare it against the local file)

- After you upload or download a file, can we trust that timestamp will be identical in both files? 

Thanks

Greg-DB
Dropbox Staff

@herbarium 

  • For any file on Dropbox, you can retrieve the FileMetadata, e.g., as returned by files_get_metadata, or directly by files_upload when uploading, etc. The FileMetadata will have two times set for the file: client_modified and server_modified. The server_modified is when the file version was uploaded to Dropbox, set by the Dropbox servers. The client_modified will be the (unverified) time set for the file by the client that uploaded it, if any. (If it wasn't set, it will just be equal to server_modified.) For example, if you're uploading the file from your code, you can set it using the 'client_modified' parameter on the files_upload call.
  • The server_modified time will be the server time at the time of upload, which will not necessarily match the local modified time for the original local file. The client_modified can be arbitrarily set by the client, so the Dropbox servers can't make any guarantee on that.
Need more support?