Dropbox API v2 - 上传文件

时间:2015-11-20 11:43:13

标签: python dropbox-api

我正在尝试遍历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.")

2 个答案:

答案 0 :(得分:7)

您对dbx.files_upload("afolder",'/bfolder/' + file, mute=True)的来电说:"发送文字afolder并将其写为名为'/bfolder/' + file"的文件。

来自doc

  

files_upload(f,path,mode = WriteMode(' add',None),autorename = False,client_modified = None,mute = False)
  使用请求中提供的内容创建一个新文件。

     

参数:

     
      
  • f - 类似字符串或文件的obj。
  •   
  • path(str) - 用户Dropbox中保存文件的路径   ....
  •   

意味着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)

答案 1 :(得分:1)

对于 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