I'm writing a script to upload files to Dropbox using python dropbox module (V2). The files will be uploaded in one go or via a session depending on the size. The relevant code is as follows:
with open(file, 'rb') as f:
try:
file_size = os.path.getsize(file)
chunk_size = 4*1024*1024
if file_size < chunk_size:
dbx.files_upload(f.read(), file_to, mode=dropbox.files.WriteMode.overwrite)
else:
session_start_result = dbx.files_upload_session_start(f.read(chunk_size))
cursor = dropbox.files.UploadSessionCursor(session_id=session_start_result.session_id, offset=f.tell())
commit = dropbox.files.CommitInfo(path=file_to)
while f.tell() < file_size:
if (file_size - f.tell()) <= chunk_size:
dbx.files_upload_session_finish(f.read(chunk_size), cursor, commit)
else:
dbx.files_upload_session_append_v2(f.read(chunk_size), cursor)
cursor.offset = f.tell()
However, this will raise an error if a the session option is used to upload a large file with the same name as one already in the Dropbox folder. For a small file upload, you can set the WriteMode, but I couldn't find any documentation on how to do this when using a session/cursor.
Any help or a nudge in the right direction would be much appreciated.
When using upload sessions, you can set the WriteMode
on the dropbox.files.CommitInfo
object, via the mode
parameter. That should be a dropbox.files.WriteMode
, just like in the small file scenario.
You then pass that CommitInfo
to files_upload_session_finish
like you're already doing.