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
pyraxic
7 years agoHelpful | Level 6
Error downloading files from dropbox API
I am trying to download files from dropbox but keep hitting the ValidationError.
for dfiles in flist: dbx.files_download_to_file('/Users/fela/Downloads/dropbox', dfiles, rev=None)
Here is the error:
ValidationError at /dropbox_auth_finish '2005-02-15 00.39.15-2.tif' did not match pattern '(/(.|[\r\n])*|id:.*)|(rev:[0-9a-f]{9,})|(ns:[0-9]+(/.*)?)' Request Method: GET Request URL: https://localhost:8000/dropbox_auth_finish?state=fNWCqBEYwxETbqJU-N5zlA%3D%3D&code=wPfEvSKsCIEAAAAAAAAZdQWDsobxTQufArgHJU7is Django Version: 1.11.3 Exception Type: ValidationError Exception Value: '2005-02-15 00.39.15-2.tif' did not match pattern '(/(.|[\r\n])*|id:.*)|(rev:[0-9a-f]{9,})|(ns:[0-9]+(/.*)?)' Exception Location: /Users/fela/miniconda3/envs/clauks/lib/python3.6/site-packages/dropbox/stone_validators.py in validate, line 317 Python Executable: /Users/fela/miniconda3/envs/clauks/bin/python Python Version: 3.6.2
You do need to use the full path when specifying the file you want to download. Using files_list_folder and files_list_folder_continue will give you every entry, and you can get the full path from the returned (File)Metadata.path_lower. (I.e., use `entry.path_lower` instead of `'/'+entry.name`.)
- pyraxicHelpful | Level 6
Seems like I was missing a leading "/" from my command but now it gives another error.
for dfiles in flist: dbx.files_download_to_file('/Users/fela/Downloads/dropbox/'+dfiles, '/'+dfiles, rev=None)
Error:
ApiError at /dropbox_auth_finish ApiError('41b9e1718f4f92d51231197741ac31d5', DownloadError('path', LookupError('not_found', None))) Request Method: GET Request URL: https://localhost:8000/dropbox_auth_finish?state=NSz1ObeJgWL234R2kIXBMw%3D%3D&code=wPfEvSKsCIEAAAAAAAAZi9WtSCRKLq18aMUUfGQJY Django Version: 1.11.3 Exception Type: ApiError Exception Value: ApiError('41b9e1718f4f92d51231197741ac31d5', DownloadError('path', LookupError('not_found', None))) Exception Location: /Users/fela/miniconda3/envs/clauks/lib/python3.6/site-packages/dropbox/dropbox.py in request, line 256 Python Executable: /Users/fela/miniconda3/envs/clauks/bin/python Python Version: 3.6.2
- pyraxicHelpful | Level 6
I figured it out. It seems that I need to enter the remote path name. For example, if I have a folder named "Camera", it should use /Camera/filename. But how do I get the list of folder name and files under that folder name (if I have multiple folders)? I could use files_list_folder which gives me the list of files but how can I get the hierarchy (folder structure)?
- Greg-DBDropbox Staff
That's correct, the `path` parameter value for files_download_to_file should be the full remote (Dropbox) path of the file you want to download.
Using files_list_folder is the right way to list files and folders under any particular path. (Use the empty string "" as the path for root if that's what you're looking for.)
If you want to list everything, included nested items, set recursive=true. Either way, be sure to check the resulting ListFolderResult.has_more to see if you need to call back to files_list_folder_continue to get more results.
- acomerfordNew member | Level 2
Another solution that might better fit your needs is to use the http api instead https://www.dropbox.com/developers/documentation/http/overview.
Specifically looking at the "files/download" endpoint. https://www.dropbox.com/developers/documentation/http/documentation#files-download
With the python dropbox sdk you download each file synchronously. In order to get some more efficency you can use some of the asynchronous functionality in python 3.7 (and some additional libraries) to try and download your files together.
Here is some example code that will save a list of files to your "/tmp" directory
import aiohttp import aiofiles import asyncio import json from tqdm import tqdm_notebook as tqdm DROPBOX_URL='https://content.dropboxapi.com/2/files/download' async def dropbox_async_download(session, path, prefix_path="/tmp"): ## Create headers to talk to dropbox api ## see https://www.dropbox.com/developers/documentation/http/documentation#files-download headers = { "Authorization": "Bearer %s"%os.environ["DROPBOX_ACCESS_TOKEN"], "Dropbox-API-Arg": json.dumps({"path": path}) } ## Create async post request ## Open file asynchronously ## write chunks from server async to file i=0 with tqdm(total=1000) as pbar: async with session.post(DROPBOX_URL, headers=headers) as response,\ aiofiles.open(prefix_path+path, "w") as f: async for chunk, _ in response.content.iter_chunks(): await f.write(chunk.decode('utf-8')) pbar.set_postfix(file=path) pbar.update(1) print ("Done!", path) async def dropbox_download_files(files): async with aiohttp.ClientSession() as session: coroutines = list( map(lambda f:dropbox_async_download(session,f),files) ) return await asyncio.gather(*coroutines)
files = ["a.txt", "b.txt", "c.txt"]
loop = asyncio.get_event_loop()
loop.run_until_complete(dropbox_download_files(files)Hope this helps!
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
5,882 PostsLatest Activity: 23 hours agoIf 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!