2015-04-12 40 views
1

我目前有我的代碼工作的上傳部分,我將如何將其轉換爲將從盒子文件夾下載相應文件的程序?如何使用Box API和Python下載文件

這是上傳程序:

import requests 
import json 

#the user acces token 
access_token = 'UfUNeHhv4gIxFCn5WEXHgBJwfG8gHT2o' 
#the name of the file as you want it to appear in box 
dst_filename = 'box_file' 
#the actual file path 
src_directory = 'C:\Python\cache\\' 
#the name of the file to be transferred 
src_filename = 'Wildlife.wmv' 
#the id of the folder you want to upload to 
parent_id = '0' 
counter = 1 

for counter in range(1, 6): 
    src_file = (src_directory + src_filename + '-' + str(counter)) 
    print(src_file) 
    box_filename = (dst_filename + '-' + str(counter)) 
    headers = { 'Authorization': 'Bearer {0}'.format(access_token)} 
    url = 'https://upload.box.com/api/2.0/files/content' 
    #open(src_file,'rb') - opens the source file with the buffered reader 
    files = { 'filename': (box_filename, open(src_file,'rb')) } 
    data = { "parent_id": parent_id } 
    response = requests.post(url, data=data, files=files, headers=headers) 
    #file_info = response.json() 
    #print(file_info) 
    print(response) 
    print(url, data, files, headers) 
    counter = counter + 1 

這是樣品捲曲請求盒子API文檔給出了下載文件。

curl -L https://api.box.com/2.0/files/FILE_ID/content \ 
-H "Authorization: Bearer ACCESS_TOKEN" \ 
-o FILE_PATH/file_name.txt 

這個問題的第二部分:有沒有辦法來改變這種程序(和下載程序)來處理所有的文件夾內不管什麼文件的名稱是什麼?

我是新來的編程,所以請原諒我缺乏這方面的技能/知識。

+0

另一個問題,我如何獲得我盒子帳戶中文件夾中每個文件的「FILE_ID」? –

回答

2

我建議你在看Box SDK

正如你可以在自己的文檔看,你的客戶端身份驗證後,您只需要運行下面一行:

client.file(file_id='SOME_FILE_ID').content() 

中有對話框的詳細信息SDK文檔。如果由於您想創建自己的Box SDK而無法滿足您的需求,請等待另一個人針對您的問題做出具體迴應。謝謝。

+0

我正在嘗試使用SDK,但是存在一些我不明白的身份驗證問題。我將創建另一個帖子。 –