1

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?

nachon
  • 45
  • 6

1 Answers1

1

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.

Greg
  • 16,359
  • 2
  • 34
  • 44
  • I used that function before but got the same error, anyways tried again but still got error: raise ApiError(res.request_id, dropbox.exceptions.ApiError: ApiError('4b71cce419324c4da53ae8a2011d6c75', GetSharedLinkFileError('shared_link_access_denied', None)) – nachon Feb 24 '22 at 22:09
  • 1
    Please refer to the last paragraph of my answer. It sounds like you're using an access token for an app with the "app folder" access type and so are running in to that bug. You'd need to use an access token for an app with the "full Dropbox" access type to work around that. – Greg Feb 25 '22 at 03:19
  • Actually that was the error, had to create another app with "full Dropbox" access and it worked! – nachon Feb 25 '22 at 23:18