Home > database >  Python to append download link to list of files in Dropbox API
Python to append download link to list of files in Dropbox API

Time:01-26

I'm trying to pull a list of dictionaries from Dropbox API for some images and to be represented as follows;

[{name: 'XYZ', url: 'THIS IS A URL'}]

I've use the following which generates a list of file names and path's just fine.

path = ''
files_list = []

def dropbox_list_files(path):

    dbx = dropbox_connect()

    try:
        files = dbx.files_list_folder(path).entries
        for file in files:
            if isinstance(file, dropbox.files.FileMetadata):
                metadata = {
                    'name': file.name,
                    'path_lower': file.path_lower,
                    }
                files_list.append(metadata)

But I'm stuck on now creating the sharable links, aswell as then appending these to each of the relevant files in a list.

In the Dropbox API documentation they have the 'sharing_create_shared_link_with_settings' function which looks like it can do this, but requires a file path, which I am not sure if this is just a single path, or how I could A) pass in an iterable of paths for all the files in sequence, and then B) how would I append these together for the purpose explained above?

Any help is much appreciated.

CodePudding user response:

Once you have the files_list populated, loop through and pass the file path to the sharing_create_shared_link method. To do that, you would add something like this to your script to print a list of links..

for file in files_list:
    try:
        link = dbx.sharing_create_shared_link(file['path_lower'])
        print(link.url)
    except Exception as e:
        print(e)

Assuming you are using the Dropbox SDK, you will want to ensure you have appropriate scopes set (e.g. sharing.write) otherwise will you encounter a permission error.

  •  Tags:  
  • Related