使用Dropbox API上传块

时间:2013-12-22 07:04:33

标签: python api dropbox dropbox-api

我正在尝试使用Dropbox API中upload_chunked()类中的ChunkedUploader方法。我编写了以下代码来完成它。

我班级的

构造函数 DropboxApp

    import webbrowser
    from configobj import ConfigObj
    from dropbox import *

    class DropboxApp:
        def __init__(self): #constructor       
            app_key = 'xxxxxxxxxx'
            app_secret = 'xxxxxxxxxx'
            access_type = "dropbox"
            TOKENS = './dropbox_token.txt'

    #first check if the user has already authenticated the app before
    try:
        token_file = open(TOKENS)
        token_key,token_secret = token_file.read().split('|')
        token_file.close()
        sess = dropbox.session.DropboxSession(app_key,app_secret,access_type )
        sess.set_token(token_key,token_secret)
        self.client = dropbox.client.DropboxClient(sess)
        self.client2 = dropbox.client.ChunkedUploader(sess)

    #if the user is using the app for the first time, we'll have to authenticate the app first    
    except:
        session = dropbox.session.DropboxSession(app_key,app_secret,access_type)

        request_token = session.obtain_request_token()

        url = session.build_authorize_url(request_token)
        webbrowser.open_new_tab(url) 
        raw_input("Press enter to continue")
        access_token = session.obtain_access_token(request_token)

        self.client = dropbox.client.DropboxClient(session)
        self.client2 = dropbox.client.ChunkedUploader(session)

        #save the tokens so that the user doesn't have to authenticate again
        token_file = open(TOKENS,'w')
        token_file.write("%s|%s" % (access_token.key,access_token.secret) )
        token_file.close()

这是上传内容的方法

    def upload_cont(self, upload):
        #upload --> the exact path of where the file to be uploaded is stored on the user's directory
        f = open(upload)
        self.client2.upload_chunked()
        response = self.client2.finish('/uploaded.txt', f)
        print "uploaded:", response

但是当我运行此方法时,我收到以下错误

    Traceback (most recent call last):
    File "/home/archit/Documents/Dropbox/dropbox-api-dev/first_app_1.0.py", line 75, in <module>
drop = DropboxApp()
    File "/home/archit/Documents/Dropbox/dropbox-api-dev/first_app_1.0.py", line 35, in __init__
    self.client2 = dropbox.client.ChunkedUploader(session)
    AttributeError: 'module' object has no attribute 'ChunkedUploader'

我不确定这意味着什么。请告诉我哪里出错了?

1 个答案:

答案 0 :(得分:2)

您使用的是什么版本的Dropbox库? ChunkedUploader肯定应该在那里。我刚刚查看了最新版本(2.0.0),我看到了它。

但无论如何,该代码并不完全正确。你应该这样做:

with open(upload) as f:
    uploader = self.client.get_chunked_uploader(f)
    # ...

请参阅DropboxClient.get_chunked_uploader