简体   繁体   English

从 BOX 下载文件 url C#

[英]Downloading file from BOX url C#

I would like to download a daily changing xls file from a box url using c#. This is the URL: https://samchully.box.com/v/Europe000000我想使用 c# 从盒子 url 下载每天变化的 xls 文件。这是 URL: https://samchully.box.com/v/Europe000000

We do not need to log in to download, so authentication is not required.我们不需要登录即可下载,因此不需要身份验证。

I found the BOX API, but I would like to ask for help in implementing it.我找到了BOX API,但我想寻求帮助实现它。

https://developer.box.com/reference/get-files-id-content/ https://developer.box.com/reference/get-files-id-content/

To use the box-api or the box-windows-sdk , you need to be authenticated, independently of the security context of what you're trying to access.要使用box-apibox-windows-sdk ,您需要经过身份验证,而与您尝试访问的内容的安全上下文无关。

If "samchully" provided a direct download link for the file you're interested, you could probably download it directly without the box api. But since you only have a public link to a shared folder inside the box app, you need to use the box api, box cli or box sdk.如果“samchully”为您感兴趣的文件提供了直接下载链接,您可能可以直接下载它而无需盒子 api。但是由于您只有一个指向盒子应用程序内共享文件夹的公共链接,因此您需要使用盒子 api,盒子 cli 或盒子 sdk。

So, if not done already, create a box developer account (or use your own), create an app, preferably using JWT auth (see authentication guides )因此,如果尚未完成,请创建一个盒子开发者帐户(或使用您自己的帐户),创建一个应用程序,最好使用 JWT 身份验证(请参阅身份验证指南

From here the steps are simple:从这里开始,步骤很简单:

  1. authenticate your app to be able to use the api验证您的应用程序以便能够使用 api
  2. Instantiate a folder object from the public URL从public URL实例化一个文件夹object
  3. Find the file you need inside that folder在该文件夹中找到您需要的文件
  4. Download the file (or all of them)下载文件(或全部)

I don't have a console C# example right now, but here is a python to illustrate the steps, the box SDK's are very similar:我现在没有控制台 C# 示例,但这里有一个 python 来说明步骤,盒子 SDK 非常相似:

import os
from boxsdk import JWTAuth, Client


def main():
    auth = JWTAuth.from_settings_file('.jwt.config.json')
    auth.authenticate_instance()
    client = Client(auth)

    web_link_url = "https://samchully.app.box.com/v/Europe000000"

    user = client.user().get()
    print(f"User: {user.id}:{user.name}")

    shared_folder = client.get_shared_item(web_link_url,'' )
    print(f"Shared Folder: {shared_folder.id}:{shared_folder.name}")
    print("#" * 80)

    print("Type\tID\t\tName")
    os.chdir('downloads')
    items = shared_folder.get_items()
    download_items(items)
    os.chdir('..')

def download_items(items):

    for item in items:
        if item.type == 'folder':
            os.mkdir(item.name)
            os.chdir(item.name)
            download_items(item.get_items())
            os.chdir('..')

        if item.type == 'file':
            print(f"{item.type}\t{item.id}\t{item.name}",end='')
            with open(item.name,'wb') as download_file:
                item.download_to(download_file)
            print("\tdone")


if __name__ == "__main__":
    main()
    print("Done")

Resulting in:导致:

User: 20344589936:UI-Elements-Sample
Shared Folder: 99073953467:Europe000
################################################################################
Type    ID              Name
file    591676566119    2015 Frieght .pdf       done
file    591665840076    Banking Info.pdf        done
file    869134630385    FedEx SCS Service Type Breakdown.docx   done
file    892046618240    NEW 2022 Milling Accessories Price List.pdf     done
file    892047887544    NEW 2022 TURNING ACCESSORIES PRICE LIST.pdf     done
file    1129055114831   Stock_Report_SCW_2023-02-01-23-35-00.xlsx       done
file    782816728512    TRANSIT NL OCEAN SHIPMENTS.xlsx done
file    591661165145    Zoning Exp & ST.pdf     done
Done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM