Skaros Ilias
Skaros Ilias

Reputation: 1068

get the sharedlink of an existing file in Dropbox using the dropBox-sdk-java

I want to retrieve the shared file url of an existing file on Dropbox. I am using the dropbox-java-sdk, and I have managed to create a shared link for a file I just uploaded. The only way I managed to get the shared link of an existing file is by listing all links and get the one I want depending on the path, like so

public void getShareLink(String path) throws DbxApiException, DbxException{
        DbxRequestConfig config = new DbxRequestConfig("test/DbApi-sdk");
        DbxClientV2 client = new DbxClientV2(config, getToken(AuthorizationFile));
        
        try {
            ListSharedLinksResult sharedLinkMetadata = client.sharing().listSharedLinks();
            for (SharedLinkMetadata slm:    sharedLinkMetadata.getLinks()){
                if(slm.getPathLower().equalsIgnoreCase(path)){
                     System.out.println(slm.getUrl());
                     return;
                }
            }
        } catch (CreateSharedLinkWithSettingsErrorException ex) {
            System.out.println(ex);
        } catch (DbxException ex) {
            System.out.println(ex);
        }
        
    }

Isn’t there a way to directly get the url for the file I want? I just think it is a waste to iterate all items just to get one of them.

Upvotes: 2

Views: 645

Answers (1)

Greg
Greg

Reputation: 16930

Get a ListSharedLinksBuilder from listSharedLinksBuilder and set ListSharedLinksBuilder.withDirectOnly to request only links for the exact path specified:

public String getShareLink(String path) {
    DbxRequestConfig config = new DbxRequestConfig("test/DbApi-sdk");
    DbxClientV2 client = new DbxClientV2(config, getToken(AuthorizationFile));
    try {
        ListSharedLinksResult sh = client.sharing().listSharedLinksBuilder()
                .withPath(path)
                .withDirectOnly(true)
                .start();
        for (SharedLinkMetadata slm : sh.getLinks()) {
            return slm.getUrl();
        }
    } catch (CreateSharedLinkWithSettingsErrorException ex) {
        System.out.println(ex);
        return null;
    } catch (DbxException ex) {
        System.out.println(ex);
        return null;
    }
    return null;
}

Upvotes: 1

Related Questions