You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.

Forum Discussion

Egorov_WIIW's avatar
Egorov_WIIW
New member | Level 2
10 months ago

Python API: dbx.files_upload does not upload files sometimes (no errors reported)

Good afternoon,

We are setting up a process with daily upload of the CSV files from the local repository to the Dropbox repo. The dropbox folder belongs to another colleague with a different Dropbox account, but it is a shared one with a turned-on right to edit and overwrite files.
Retrieval of the access token works fine, downloading files from Dropbox works too.


However, the functions that upload documents to the folder work inconsistently.
The script runs without throwing any error messages. However, sometimes the files do not appear in the Dropbox folder (or only some of them appear).

Here are 2 functions we are using for uploads, both of them work and do not work from time to time without any clear pattern:

def upload_to_dropbox(access_token, project_folder, folder):
    dbx = dropbox.Dropbox(access_token)

    try:
        files_to_upload = define_upload_files()

        for file in files_to_upload:
            local_file_path = os.path.join(folder, file)
            dropbox_path = f'/{project_folder}/{folder}/{file}'.replace(os.path.sep, '/')

            with open(local_file_path, 'rb') as f:
                file_content = f.read()
                dbx.files_upload(file_content, dropbox_path, mode = dropbox.files.WriteMode('overwrite'))
                print(f"File uploaded to Dropbox: {dropbox_path}")

    except ApiError as e:
        print(f"Error: {e}")

def upload_csvs(access_token, project_folder, folder):
    dbx = dropbox.Dropbox(access_token)
    try:
        files_to_upload = define_upload_files()
        for file in files_to_upload:
            local_file_path = os.path.join(folder, file)
            df = pd.read_csv(local_file_path)
            csv_content = df.to_csv(index=False)
            dropbox_path = f'/{project_folder}/{folder}/{file}'.replace(os.path.sep, '/')
            dbx.files_upload(csv_content.encode('utf-8'), dropbox_path, mode=dropbox.files.WriteMode('overwrite'))
            print(f"CSV uploaded to Dropbox: {dropbox_path}")
    except dropbox.exceptions.ApiError as e:
        print(f"Error uploading CSV file to Dropbox: {e}")

 

  • Hi Egorov_WIIW,

    There are variety of reasons for such behavior. You didn't check actually for all possible cases! Try catch 'DropboxException' in an except statement, not only 'ApiError'. There should be some more information. By the way using access token only is not a good idea. It expires and you need to authorize your application anew. Share single Dropbox client object instead of access token. Once properly initialized with refresh token such an object will work in a more stable way and more efficiently. 😉

    Hope this gives direction.

  • Egorov_WIIW Like Здравко said, make sure you're catching and handling any/all potentially relevant exceptions. Additionally, for the case where no exceptions are being raised, check the return value of files_upload.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,888 PostsLatest Activity: 2 days ago
326 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!