【问题标题】:Dropbox upload file by python errorDropbox通过python错误上传文件
【发布时间】:2012-03-21 21:34:08
【问题描述】:

我正在尝试将文件列表上传到 Dropbox,但出现各种错误。我试过这个,并尝试了网上的一切。但我仍然无法让它工作。

# Include the Dropbox SDK libraries
from dropbox import rest, session
import webbrowser
import os
import glob
import zipfile
import datetime
from dropbox import client

# Get your app key and secret from the Dropbox developer website

# (app keys defined here)

# ACCESS_TYPE should be 'dropbox' or 'app_folder' as configured for your app
ACCESS_TYPE = 'app_folder'
sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)

request_token = sess.obtain_request_token()

url = sess.build_authorize_url(request_token)

file_list = []

#get the date
now = datetime.datetime.now()

def initialize():

    #print "url:", url
    # open a public URL, in this case, the webbrowser docs
    #url = "http://docs.python.org/library/webbrowser.html"
    webbrowser.open(url)
    print "Please click the 'Allow' button to Authorize..."
    print """
    Please select by entering the specific number...
    1 Backup all my files by one by one
    2 Backup specific folder
    3 Backup specific file
    4 Get my account details
    5 About this software
    6 Exit
    """
    try:
        # This will fail if the user didn't visit the above URL and hit 'Allow'
        access_token = sess.obtain_access_token(request_token)
    except:
        print "Error has occured"

def getAccountInfo():
    from dropbox import client
    client = client.DropboxClient(sess)
    account_info_dict = client.account_info()
    print "linked account:"

    for item in account_info_dict:
        if type(account_info_dict[item]) == dict:
           inner_dict = account_info_dict[item]
           for item1 in inner_dict:
               print item1, ":", inner_dict[item1]
        print item, ":", account_info_dict[item]

def getAllFiles():
   for dirname, dirnames, filenames in os.walk('I:/'):
       for subdirname in dirnames:
           print os.path.join(dirname, subdirname)
       for filename in filenames:
           file_name = os.path.join(dirname, filename)
           print file_name
           file_list.append(file_name)
   return file_list

def upload_one_by_one(sess):
   from dropbox import client
   files = getAllFiles()
   client = client.DropboxClient(sess)
   #zip_file_name = now.strftime("%Y%m%d%H%M")+ ".zip"    
   #z = zipfile.ZipFile(zip_file_name, "w")
   for file_item in files:
       #z.write(file_item)
       #f = open(zip_file_name)
       response = client.put_file("test/", file_item)
       print "uploaded:", response
       break


initialize()
#getAccountInfo()
upload_one_by_one(sess)

client.put_file() 参数的问题。我正在尝试在测试文件夹中上传图像文件。但它作为文本文件上传,其中包含图像文件的路径。

我不能通过 dropbox sdk 将图片文件上传到 Dropbox 吗?

【问题讨论】:

  • 您的开发密钥已被删除,但不能保证谷歌没有将它们编入索引。最好买新的。

标签: python sdk dropbox dropbox-api


【解决方案1】:

您似乎没有打开要上传的文件,这可能有效:

for file_item in files:
  file = open(file_item)
  response = client.put_file("test/", file)

【讨论】:

    【解决方案2】:

    put_file() 接受类似文件的对象或表示文件内容的字符串。如果你传入一个文件名,它不会自动读入文件的内容。

    【讨论】:

    • 好的,你知道怎么做吗?如果您知道可以请在此处发布。感谢您的承诺!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-01
    • 1970-01-01
    • 2013-01-12
    • 1970-01-01
    相关资源
    最近更新 更多