The Community is undergoing an upgrade and some features may be unavailable. Find out more here.

Forum Discussion

Cryo's avatar
Cryo
Explorer | Level 4
2 months ago

Trying to export files from folders and subfolders to Nakala

Hello.

 

I'm part of a project and i need to export all the Dropbox's files to Nakala which is a data warehouse.

I'm having difficulties listing the Dropbox folders, i think the script doesnt recognize them even when i have the right path to them

Here is the code  : 

 

Spoiler

import dropbox
import requests

# Dropbox and Nakala authentication
DROPBOX_ACCESS_TOKEN = ''
NAKALA_API_URL = "https://apitest.nakala.fr/data/"
NAKALA_API_KEY = "01234567-89ab-cdef-0123-456789abcdef"
dbx = dropbox.Dropbox(DROPBOX_ACCESS_TOKEN)

# Step 1: Recursively list folders and files in Dropbox
def list_all_folders_and_upload_to_nakala(folder_path=""):
try:
result = dbx.files_list_folder(folder_path, recursive=True)
print("Folders and files in Dropbox:")
for entry in result.entries:
if isinstance(entry, dropbox.files.FileMetadata):
file_path_in_dropbox = entry.path_lower
print(f"Processing file: {file_path_in_dropbox}")

# Download the file from Dropbox
file_content = download_from_dropbox(file_path_in_dropbox)

# Upload to Nakala
if file_content:
print(f"Uploading {entry.name} to Nakala...")
upload_to_nakala(entry.name, file_content)
elif isinstance(entry, dropbox.files.FolderMetadata):
print(f"Folder: {entry.path_display}")
except Exception as e:
print(f"Error listing Dropbox folders: {e}")

# Step 2: Download file from Dropbox
def download_from_dropbox(file_path):
try:
metadata, res = dbx.files_download(path=file_path)
print(f"Downloaded: {file_path}")
return res.content
except Exception as e:
print(f"Error downloading from Dropbox: {e}")
return None

# Step 3: Upload file to Nakala
def upload_to_nakala(file_name, file_content):
files = {
'file': (file_name, file_content),
}
headers = {
'Authorization': f'Bearer {NAKALA_API_KEY}',
}
try:
response = requests.post(NAKALA_API_URL, headers=headers, files=files)
if response.status_code == 200:
print(f"File {file_name} uploaded successfully to Nakala!")
else:
print(f"Error uploading {file_name} to Nakala: {response.status_code}, {response.text}")
except Exception as e:
print(f"Error during Nakala upload: {e}")

# Main workflow
folder_path_in_dropbox = "/Sète"
list_all_folders_and_upload_to_nakala(folder_path_in_dropbox)

 

I would like to know why this doesnt work, when i have the folder created 

Thanks you.

 

  • A "path/not_found" Dropbox API error indicates that the API call failed because there was nothing currently found at the specified path in the connected account under the relevant root. For example, this can happen if there's a mistake or typo in the path value the app supplies, if the file/folder has been renamed, moved, or deleted from that path, if the app is not connected to the correct account for that particular path, etc.

     

    When specifying the path, make sure you provide the full and accurate path for the desired file under the relevant root. For example, if you have a file named "example.csv" inside a folder named "folder", the path would be "/folder/example.csv". You can find more information on path formats here.

     

    I see Здравко already helpfully covered some of this, but to elaborate a bit, here are several things you can check in particular to debug this:
     

    • Make sure you're using a currently accurate path value: You can use files_list_folder and files_list_folder_continue to list the contents of a folder so you can check what the correct path values would be for items in those folder(s). To list the root, set path to the empty string "" when calling files_list_folder. You can use the path_lower or id values returned for the files/folders in that folder to interact with those files/folders.
       
    • Make sure you're connected to the correct account for the path value you're using: You can use users_get_current_account to check which account the app is connected to.

     

    • Make sure you're using app folder-relative paths, if your app is registered for the "app folder" access type: Apps with the "app folder" access type can only access the contents of the special app folder that gets automatically created for it. If your app has the "app folder" access type, then it will only be able to access files in the app folder, and the root for any path value supplied by that app will automatically be the app folder root. You can find more information on app permissions here. For example, If your app has the app folder access type and you're trying to access something you can see on the Dropbox web site at "/Apps/<app folder name>/folder/example.csv", you should only send the path value as "/folder/example.csv". Alternatively, if your app is registered for the "app folder" access type but you need to access contents outside the app folder, you'll need to use a different app registration with the full Dropbox access type instead (or you can move the contents into the app folder).

     

    • Make sure you're accessing the relevant root: When using an app with the "full Dropbox" access type, API calls default to the member folder, but if you're trying to access the "team space" (only applicable to members of a team with the "team space" configuration), you'll need to configure that as the root explicitly, as covered in the Team Files Guide.

     

    Given the existence of "Applications" in your screenshot though, it seems like you may have registered an app with the app folder access type, but are trying to access a folder "/Sète" outside the app folder, which would fail, so take note of the third list item above in particular.

  • Здравко's avatar
    Здравко
    Legendary | Level 20

    Cryo wrote:

    ...

    I would like to know why this doesnt work, when i have the folder created 

    ...


    Hi Cryo,

    Is there something in your folder you're trying to process? 🤔 What does actually your script output? (eventual error message or something like)

    Something else that may confuse you is skipping the fact that folder listing is in pages (or may be at least). Using files_list_folder returns only the first page, not entire folder content! You didn't check has_more field to see if there may be next pages. Do this and call files_list_folder_continue too whenever needed for full folder content list. 😉

    Hope this gives direction.

    • Cryo's avatar
      Cryo
      Explorer | Level 4

      Yes there is pictures files.

       

      C:\Users\Maxime\Desktop\Nouveau dossier (7)>python python3.py
      Error listing Dropbox folders: ApiError('4e3209586ff04f77b802c4b0073f44b6', ListFolderError('path', LookupError('not_found', None)))

       

      I have this API error, i don't know why but he cant find anything. When i tried root folder, it found nothing as well but i have a message Folders and files in Dropbox with a blank.

       

      Code is like that : 

       

      import dropbox
      import requests

      # Dropbox and Nakala authentication
      DROPBOX_ACCESS_TOKEN = '.9lC4QlMUxkABZ8fN1H3-wzkKB3Wf5XiDzOKMEItluZw2FxoiKC93UX52k4vlLOmWlywyRwKvkZ'
      NAKALA_API_URL = "https://apitest.nakala.fr/data/"
      NAKALA_API_KEY = "01234567-89ab-cdef-0123-456789abcdef"
      dbx = dropbox.Dropbox(DROPBOX_ACCESS_TOKEN)

      # Step 1: Recursively list folders and files in Dropbox with pagination
      def list_all_folders_and_upload_to_nakala(folder_path=""):
      try:
      result = dbx.files_list_folder(folder_path, recursive=True)
      process_entries(result.entries)

      # Check if more entries are available and continue listing
      while result.has_more:
      result = dbx.files_list_folder_continue(result.cursor)
      process_entries(result.entries)

      except Exception as e:
      print(f"Error listing Dropbox folders: {e}")

      # Process each entry (file or folder) found in Dropbox
      def process_entries(entries):
      for entry in entries:
      if isinstance(entry, dropbox.files.FileMetadata):
      file_path_in_dropbox = entry.path_lower
      print(f"Processing file: {file_path_in_dropbox}")

      # Download the file from Dropbox
      file_content = download_from_dropbox(file_path_in_dropbox)

      # Upload to Nakala
      if file_content:
      print(f"Uploading {entry.name} to Nakala...")
      upload_to_nakala(entry.name, file_content)
      elif isinstance(entry, dropbox.files.FolderMetadata):
      print(f"Folder: {entry.path_display}")

      # Step 2: Download file from Dropbox
      def download_from_dropbox(file_path):
      try:
      metadata, res = dbx.files_download(path=file_path)
      print(f"Downloaded: {file_path}")
      return res.content
      except Exception as e:
      print(f"Error downloading from Dropbox: {e}")
      return None

      # Step 3: Upload file to Nakala
      def upload_to_nakala(file_name, file_content):
      files = {
      'file': (file_name, file_content),
      }
      headers = {
      'Authorization': f'Bearer {NAKALA_API_KEY}',
      }
      try:
      response = requests.post(NAKALA_API_URL, headers=headers, files=files)
      if response.status_code == 200:
      print(f"File {file_name} uploaded successfully to Nakala!")
      else:
      print(f"Error uploading {file_name} to Nakala: {response.status_code}, {response.text}")
      except Exception as e:
      print(f"Error during Nakala upload: {e}")

      # Main workflow
      folder_path_in_dropbox = "/Sète"
      list_all_folders_and_upload_to_nakala(folder_path_in_dropbox)

       

      /spo

      • Здравко's avatar
        Здравко
        Legendary | Level 20

        As the error message shows, you don't point your folder in correct way. There are number of possibilities, but instead of I speculate, better just you start from the root - empty path instead of some concrete - it's always correct one.

        Let's see what will get up.

  • Greg-DB's avatar
    Greg-DB
    Dropbox Staff

    A "path/not_found" Dropbox API error indicates that the API call failed because there was nothing currently found at the specified path in the connected account under the relevant root. For example, this can happen if there's a mistake or typo in the path value the app supplies, if the file/folder has been renamed, moved, or deleted from that path, if the app is not connected to the correct account for that particular path, etc.

     

    When specifying the path, make sure you provide the full and accurate path for the desired file under the relevant root. For example, if you have a file named "example.csv" inside a folder named "folder", the path would be "/folder/example.csv". You can find more information on path formats here.

     

    I see Здравко already helpfully covered some of this, but to elaborate a bit, here are several things you can check in particular to debug this:
     

    • Make sure you're using a currently accurate path value: You can use files_list_folder and files_list_folder_continue to list the contents of a folder so you can check what the correct path values would be for items in those folder(s). To list the root, set path to the empty string "" when calling files_list_folder. You can use the path_lower or id values returned for the files/folders in that folder to interact with those files/folders.
       
    • Make sure you're connected to the correct account for the path value you're using: You can use users_get_current_account to check which account the app is connected to.

     

    • Make sure you're using app folder-relative paths, if your app is registered for the "app folder" access type: Apps with the "app folder" access type can only access the contents of the special app folder that gets automatically created for it. If your app has the "app folder" access type, then it will only be able to access files in the app folder, and the root for any path value supplied by that app will automatically be the app folder root. You can find more information on app permissions here. For example, If your app has the app folder access type and you're trying to access something you can see on the Dropbox web site at "/Apps/<app folder name>/folder/example.csv", you should only send the path value as "/folder/example.csv". Alternatively, if your app is registered for the "app folder" access type but you need to access contents outside the app folder, you'll need to use a different app registration with the full Dropbox access type instead (or you can move the contents into the app folder).

     

    • Make sure you're accessing the relevant root: When using an app with the "full Dropbox" access type, API calls default to the member folder, but if you're trying to access the "team space" (only applicable to members of a team with the "team space" configuration), you'll need to configure that as the root explicitly, as covered in the Team Files Guide.

     

    Given the existence of "Applications" in your screenshot though, it seems like you may have registered an app with the app folder access type, but are trying to access a folder "/Sète" outside the app folder, which would fail, so take note of the third list item above in particular.

    • Cryo's avatar
      Cryo
      Explorer | Level 4

      Thanks you very much !

       

      I didn't have the folder in the application folder, i just put it here and it is working.

      Have a great day !