Search code examples
c#dropboxdropbox-apidropbox-sdk

Downloading images from publicly shared folders and sub-folders on Dropbox


This is similar to my previous question: Downloading images from publicly shared folder on Dropbox

I have this piece of code (simplified version) that needs to download all images from publicly shared folder and all sub-folders.

using Dropbox.Api;
using Dropbox.Api.Files;
...
// AccessToken - get it from app console 
// FolderToDownload - https://www.dropbox.com/sh/{unicorn_string}?dl=0

using (var dbx = new DropboxClient(_dropboxSettings.AccessToken))
{
    var sharedLink = new SharedLink(_dropboxSettings.FolderToDownload);
    var sharedFiles = await dbx.Files.ListFolderAsync(path: "", sharedLink: sharedLink);

    // var sharedFiles = await dbx.Files.ListFolderAsync(path: "", sharedLink: sharedLink, recursive: true); 
    // "recursive: true" throws:  Error in call to API function "files/list_folder": Recursive list folder is not supported for shared link.

    foreach (var entry in sharedFiles.Entries)
    {
        if (entry.IsFile)
        {
            var link = await dbx.Sharing.GetSharedLinkFileAsync(url: _dropboxSettings.FolderToDownload, path: "/" + entry.Name);
            var byteArray = await link.GetContentAsByteArrayAsync();
        }

        if (entry.IsFolder)
        {
            var subFolder = entry.AsFolder;
            // var folderContent = await dbx.Files.ListFolderAsync(path: subFolder.Id);
            // var subFolderSharedLink = new SharedLink(???);
        }
    }        
}

How do I list entries of all sub-folders?


Solution

  • For any given subfolder, to list its contents, you'll need to call back to ListFolderAsync again, using the same sharedLink value, but supplying a path value for the subfolder, relative to the root folder for the shared link.

    For example, if you list the contents of the folder shared link, and one of the entries is a folder with the name "SomeFolder", to then list the contents of "SomeFolder", you would need to make a call like:

    await dbx.Files.ListFolderAsync(path: "/SomeFolder", sharedLink: sharedLink);