Search code examples
.netdropbox-api

DropboxClient C# get Image URL


I am writing a .NET core 6 app and trying o read files form a Dropbox folder. Root folder is located at https://www.dropbox.com/sh/4qwqh8piw4ky4ls/AADIJ-Ie6Pr_AdWp19uoOaIVa?dl=0 and it has Folder 1 and then an image file. There would be many more folders and file when I get it working.

I need a url of the image file, for example I need Image url like enter image description here

Here is my code.

using (var dbx = new DropboxClient("accesstoken"))
        {
            List<string> fileLinks = new List<string>();
            var sharedLink = new SharedLink("https://www.dropbox.com/sh/4qwqh8piw4ky4ls/AADIJ-Ie6Pr_AdWp19uoOaIVa");
            await this.GetDropboxFileLinks(dbx, "", fileLinks, sharedLink);

        }

public static async Task GetDropboxFileLinks(DropboxClient dbx, string folderUrl, List<string> fileLinks, SharedLink sharedLink)
    {
        var result = await dbx.Files.ListFolderAsync(path: folderUrl, sharedLink: sharedLink);
        foreach (var entry in result.Entries)
        {
            if (entry.IsFolder)
            {
                //var folder = (FolderMetadata)entry;
                folderUrl = $"{folderUrl}/{entry.Name}";
                await GetDropboxFileLinks(dbx, folderUrl, fileLinks, sharedLink);
            }
            else
            {
                string fileUrl = $"{folderUrl}/{entry.Name}";
                var link = await dbx.Sharing.ListSharedLinksAsync();
                string sharedFileLink = "";
                if (link.Links.Count == 0)
                {
                    var linkResult = await dbx.Sharing.CreateSharedLinkWithSettingsAsync(fileUrl);
                    sharedFileLink = linkResult.Url;
                }
                else
                {
                    sharedFileLink = link.Links[0].Url;
                }
                fileLinks.Add(sharedFileLink);
            }
        }
    }

I can generate https://www.dropbox.com/sh/4qwqh8piw4ky4ls/**AAC_dTayIVro_KbUwKND0buwa**/Folder%201/ALF01ASET-1.jpg?dl=0 but don't know how to get AADwV4p6Ibyc3n02I2TTEvoOa which is part of the url


Solution

  • You can create a shared link for any file or folder in the connected account using CreateSharedLinkWithSettingsAsync. I see you have that in your code already, but only use it on files. It can also be used on folders.

    Alternatively, since you're working from a shared link for the parent folder, you can use GetSharedLinkMetadataAsync to get the sub-link for any item underneath that parent folder, without explicitly creating a new shared link for each item. To do so, you'd call GetSharedLinkMetadataAsync with the url parameter being the shared link of the parent folder, (https://www.dropbox.com/sh/4qwqh8piw4ky4ls/AADIJ-Ie6Pr_AdWp19uoOaIVa?dl=0 in your example) and the path parameter being the relative path of the nested item for which you want the sub-link, (/Folder 1 for the nested folder, or /Folder 1/ALF01ASET-1.jpg for the nested file in your example). The returned SharedLinkMetadata.url would be the sub-link for specified item, like in your screenshot.