Download a single file from a shared Dropbox's folder without having the share link of this file

1.8k Views Asked by At

As the title says, I have access to a shared folder where some files are uploaded. I just want to donwload an specific file, called "db.dta". So, I have this script:

def download_file(url, filename):
    url = url
    file_name = filename
    with open(file_name, "wb") as f:
        print("Downloading %s" % file_name)
        response = requests.get(url, stream=True)
        total_length = response.headers.get('content-length')

        if total_length is None: # no content length header
            f.write(response.content)
        else:
            dl = 0
            total_length = int(total_length)
            for data in response.iter_content(chunk_size=4096):
                dl += len(data)
                f.write(data)
                done = int(50 * dl / total_length)
                sys.stdout.write("\r[%s%s]" % ('=' * done, ' ' * (50-done)) )    
                sys.stdout.flush()
    print(" ")
    print('Descarga existosa.')

It actually download shares links of files if I modify the dl=0 to 1, like this:

https://www.dropbox.com/s/ajklhfalsdfl/db_test.dta?dl=1

The thing is, I dont have the share link of this particular file in this shared folder, so if I use the url of the file preview, I get an error of denied access (even if I change dl=0 to 1).

https://www.dropbox.com/sh/a630ksuyrtw33yo/LKExc-MKDKIIWJMLKFJ?dl=1&preview=db.dta

Error given:

dropbox.exceptions.ApiError: ApiError('22eaf5ee05614d2d9726b948f59a9ec7', GetSharedLinkFileError('shared_link_access_denied', None))

Is there a way to download this file?

1

There are 1 best solutions below

3
Greg On

If you have the shared link to the parent folder and not the specific file you want, you can use the /2/sharing/get_shared_link_file endpoint to download just the specific file.

In the Dropbox API v2 Python SDK, that's the sharing_get_shared_link_file method (or sharing_get_shared_link_file_to_file). Based on the error output you shared, it looks like you are already using that (though not in the particular code snippet you posted).

Using that would look like this:

import dropbox

dbx = dropbox.Dropbox(ACCESS_TOKEN)

folder_shared_link = "https://www.dropbox.com/sh/a630ksuyrtw33yo/LKExc-MKDKIIWJMLKFJ"
file_relative_path = "/db.dat"

res = dbx.sharing_get_shared_link_file(url=folder_shared_link, path=file_relative_path)

print("Metadata: %s" % res[0])
print("File data: %s bytes" % len(res[1].content))

(You mentioned both "db.dat" and "db.dta" in your question. Make sure you use whichever is actually correct.)

Additionally, note if you using a Dropbox API app registered with the "app folder" access type: there's currently a bug that can cause this shared_link_access_denied error when using this method with an access token for an app folder app.