Skip to main content
 首页 » 编程设计

python之使用 Dropbox API 上传区 block

2024年11月01日9luoye11

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

我的类 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' 

我不知道那是什么意思。请告诉我哪里出错了?

请您参考如下方法:

您使用的是哪个版本的 Dropbox 库? ChunkedUploader 绝对应该在那里。我刚刚检查了最新版本 (2.0.0),我看到了。

无论如何,该代码都不太正确。你应该这样做:

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

参见 DropboxClient.get_chunked_uploader .