Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
Hi there!
I'm asking myself what's the best practice to check if a folder exists, because I made a fudge in the meanwhile and the performance is not optimal.
I'm giving the option to our users to overwrite or not the desired uploading file. I actually perform a DropboxClient.Files.SearchAsync with a try catch and if it fails I enable a flag which, if it's enabled, it writes the folder/file (of course it creates automatically the folder). If the flag is not enabled, it searches again (with this performance losing) overwriting the file or not.
I've found that DropboxClient.Files.GetMetadataAsync is the recommended one, but if the folder doesn't exist it basically throws an Exception, so it's working by now the same way my SearchAsync does.
I'm quite sure there is another way to do this in a better way and I already searched through the Internet, but with no convincent result.
Thanks,
Gonzo345.
EDIT:
I have just made some refactoring and making some research on the customs Exceptions the API has, so firstly searches and if it goes ok, then proceeds. If it gets the APIException<SearchError> then launches the UploadAsync, creating the folder and uploading the file anyway 🙂
try { var taskBusqueda = Task.Run(async () => await dropbox.Files.SearchAsync(carpetaCloud, nombreArchivo, 0, 100, SearchMode.Filename.Instance)); taskBusqueda.Wait(); //If finds a coincidence, acts depending
//on how we concreted if (taskBusqueda.Result.Matches.Count == 1 && tipoSobreescritura == Tipos.TipoSobreescritura.No) //We don't allow overwriting, abort operation
throw new SobreescrituraNoPermitidaException("No se ha permitido la sobreescritura del archivo"); carpetaCloud = carpetaCloud + "/" + nombreArchivo; //Upload the file, overwriting by default var taskSubidaSobreescribir = Task.Run(async () => await dropbox.Files.UploadAsync(carpetaCloud, body: fileStream)); taskSubidaSobreescribir.Wait();
} catch(AggregateException ex) when (ex.InnerException is ApiException<SearchError>) { //If the search fails, the folder doesn't exist, //so proceed creating the folder and uploading the file carpetaCloud = carpetaCloud + "/" + nombreArchivo; var taskSubida = Task.Run(async () => await dropbox.Files.UploadAsync(carpetaCloud, body: fileStream)); taskSubida.Wait();
} return GenerarLink(carpetaCloud);
There might be a better method, but this "works" by now lol
Can I have much brief description of the solution and also example related to it if possible. Thanks.
That's so great of you. Thank you so much.
Hi there!
If you need more help you can view your support options (expected response time for a 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!