使用 API 从 Box 位置下载文件

Download files from a Box location using API

如何以编程方式从 Box 位置下载文件?

我有一个共享的盒子位置URL(不是盒子位置的确切路径)。

我要下载该位置下的所有文件

我在下面检查了 sdk 以连接到框,但无法找到 methods/library 从共享 link 下载文件。

https://github.com/box/box-python-sdk

from boxsdk import Client
from boxsdk import OAuth2

oauth = OAuth2(
    client_id='XXX',
    client_secret='XXX',
    store_tokens='XXX',
)


data = client.make_request(
    'GET',
    '<Shared BOX URL>',
)

请帮忙

你可以使用直接给你的方法 URL:

download_url = client.file(file_id='SOME_FILE_ID').get_shared_link_download_url()

然后就可以使用urlib下载到本地了:

import urllib
urllib.urlretrieve (download_url , your_local_file_name)

能解决您的问题吗?

获取共享框的元数据link:

shared_folder = client.get_shared_item("https://app.box.com/s/0123456789abcdef0123456789abcdef")

遍历文件夹中的每个项目并使用 boxsdk.object.file.File.content or boxsdk.object.file.File.download_to:

下载每个文件
for item in shared_folder.get_items(limit=1000):
    if item.type == 'file':
        # Get file contents into memory
        file_contents = client.file(file_id=item.id).content()
        # Or download to file
        client.file(file_id=item.id).download_to(item.name)

Pre-requisite:

oauth = OAuth2(
    client_id = 'strBoxClientID',
    client_secret = 'strBoxClientSecret', 
    access_token = access_token,
    )
client = Client(oauth)

初次尝试(失败,生成一个空文件):

with open(Box_File.name, 'wb') as open_file:
    client.file(Box_File.id).download_to(open_file)
    open_file.close()

最终解决方案:

output_file = open('strFilePath' + str(Box_File.name), 'wb')
Box_File.download_to(output_file)