如何使用Box API&下载文件蟒蛇 [英] How to download files with Box API & Python

查看:142
本文介绍了如何使用Box API&下载文件蟒蛇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在执行我的代码的上载部分,如何将其转换为将从box文件夹中下载相应文件的程序?

I have currently the upload portion of my code working, how would I go about converting this into a program that will download the respective files from the box folder?

这是上传程序:

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

这是Box API文档给出的用于下载文件的示例curl请求。

This is the sample curl request that the Box API documentation gives for downloading files.

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

这个问题的第二部分:有没有办法改变这个程序(和下载程序)无论文件名是什么,都可以处理文件夹中的所有文件?

Part two of this question: Is there a way to alter this program (and the download program) to process all of the files within a folder no matter what the name of the file is?

我是编程新手,请原谅缺乏

I am new to programming, so please forgive my lack of skills/knowledge in this area.

推荐答案

假设您的授权正确无误,可以通过在代码中添加几行来下载文件到您现有的代码。
这会将数据从Box文件复制到本地文件,这里的文件名为FileFromBox.xlx

Assume you are getting your authorization correct you can download file by adding few lines to code to your Existing code. This will copy data from box file to local file here name is FileFromBox.xlx

with open('FileFromBox.xls', 'wb') as open_file:
    client.file('FileId_of_box_file').download_to(open_file)
    open_file.close()

这篇关于如何使用Box API&下载文件蟒蛇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆