Dropbox API v2 - 上传文件

Dropbox API v2 - uploading files

我正在尝试遍历 python 中的文件夹结构,并将它找到的每个文件上传到指定文件夹。问题是它上传了一个名称正确的文件,但是没有内容,文件大小只有 10 个字节。

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.")

您打给 dbx.files_upload("afolder",'/bfolder/' + file, mute=True) 的电话说:"Send the text afolder and write it as a file named '/bfolder/' + file"。

来自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.
    ....

意味着 f 必须是文件的 内容 (而不是文件名字符串)。

这是一个工作示例:

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.")

编辑:对于Python3,应使用以下内容:

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

对于 Dropbox Business API,下面的 python 代码有助于将文件上传到 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