2015-04-12 41 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,但是存在一些我不明白的身份验证问题。我将创建另一个帖子。 –