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}")