Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropbox API v2 - uploading files

I'm trying to loop through a folder structure in python and upload each file it finds to a specified folder. The problem is that it's uploading a file with the correct name, however there is no content and the file size is only 10 bytes.

import dropbox, sys, os
try:
  dbx = dropbox.Dropbox('some_access_token')
  user = dbx.users_get_current_account()
except:
  print ("Negative, Ghostrider")
  sys.exit()

rootdir = os.getcwd()

print ("Attempting to upload...")
for subdir, dirs, files in os.walk(rootdir):
      for file in files:
        try:  
          dbx.files_upload("afolder",'/bfolder/' + file, mute=True)
          print("Uploaded " + file)
        except:
          print("Failed to upload " + file)
print("Finished upload.")
like image 383
Mike Resoli Avatar asked Dec 11 '22 19:12

Mike Resoli


2 Answers

Your call to dbx.files_upload("afolder",'/bfolder/' + file, mute=True) says: "Send the text afolder and write it as a file named '/bfolder/' + file".

From doc:

files_upload(f, path, mode=WriteMode('add', None), autorename=False, client_modified=None, mute=False)
Create a new file with the contents provided in the request.

Parameters:

  • f – A string or file-like obj of data.
  • path (str) – Path in the user’s Dropbox to save the file.
    ....

Meaning that f must be the content of the file (and not the filename string).

Here is a working example:

import dropbox, sys, os

dbx = dropbox.Dropbox('token')
rootdir = '/tmp/test' 

print ("Attempting to upload...")
# walk return first the current folder that it walk, then tuples of dirs and files not "subdir, dirs, files"
for dir, dirs, files in os.walk(rootdir):
    for file in files:
        try:
            file_path = os.path.join(dir, file)
            dest_path = os.path.join('/test', file)
            print 'Uploading %s to %s' % (file_path, dest_path)
            with open(file_path) as f:
                dbx.files_upload(f, dest_path, mute=True)
        except Exception as err:
            print("Failed to upload %s\n%s" % (file, err))

print("Finished upload.")

EDIT: For Python3 the following should be used:

dbx.files_upload(f.read(), dest_path, mute=True)

like image 76
Cyrbil Avatar answered Dec 31 '22 12:12

Cyrbil


For Dropbox Business API below python code helps uploading files to dropbox.

#function code

import dropbox

def dropbox_file_upload(access_token,dropbox_file_path,local_file_name):

'''
The function upload file to dropbox.

    Parameters:
        access_token(str): Access token to authinticate dropbox
        dropbox_file_path(str): dropboth file path along with file name
        Eg: '/ab/Input/f_name.xlsx'
        local_file_name(str): local file name with path from where file needs to be uploaded
        Eg: 'f_name.xlsx' # if working directory
Returns:
    Boolean: 
        True on successful upload
        False on unsuccessful upload
'''
try:
    dbx = dropbox.DropboxTeam(access_token)
    # get the team member id for common user
    members = dbx.team_members_list()
    for i in range(0,len(members.members)):
        if members.members[i].profile.name.display_name == logged_in_user:
            member_id = members.members[i].profile.team_member_id
            break
    # connect to dropbox with member id
    dbx = dropbox.DropboxTeam(access_token).as_user(member_id)
    # upload local file to dropbox
    f = open(local_file_name, 'rb')
    dbx.files_upload(f.read(),dropbox_file_path)
    return True
except Exception as e:
    print(e)
    return False
like image 41
Vishal Telmani Avatar answered Dec 31 '22 12:12

Vishal Telmani