如何读取OneDrive的所有文件并用Python复制

How to read all the files of OneDrive and copy it with Python

我正在尝试将我的整个 OneDrive 同步到 AWS S3。 为此,我想读取 OneDrive 中的所有文件和文件夹(以便我可以将它们写入 AWS S3)。 如何获取 OneDrive 文件夹中所有文件和文件的列表? 我打算以数据框的形式读取所有文件,然后将其写入AWS S3。

我现在使用的代码从我的代码中明确提到的 OneDrive 读取一个文件。代码来自this answer

import sys, os, time, requests
import pandas as pd
import urllib.parse

OneDrive_FilePath = 'New Folder/Knox EARNSTSALV2020.xlsx'

OneDrive_FileURL = 'https://graph.microsoft.com/v1.0/me/drive/root:/' + OneDrive_FilePath + ':/content'
OneDrive_FileURL = urllib.parse.quote(OneDrive_FileURL, safe=':/')
print(OneDrive_FileURL)

Client_Id = 'XXXX'
Tenant_Id = 'YYYYY'
Refresh_Token_First = 'ZZZZZ'

PostStr = {'grant_type': 'refresh_token', 'client_id': Client_Id, 'refresh_token': Refresh_Token_First}

Token_Response = requests.post('https://login.microsoftonline.com/' + Tenant_Id + '/oauth2/v2.0/token', data=PostStr)

Access_Token = Token_Response.json()['access_token']
New_Refresh_Token = Token_Response.json()['refresh_token']

if Access_Token is None or New_Refresh_Token is None:
    print('\n> Failed: Access_Token NOT Retrieved')
    sys.exit()

Response = requests.get(OneDrive_FileURL, headers={'Authorization': 'Bearer ' + Access_Token})

if Response.status_code == 200:
    print('\n> Response Success')

    with open('Excel File.xlsx', 'wb') as File:
    File.write(Response.content)
    print('\n> File Downloaded')
else:
    print('\n> Failed:', Response.status_code)
    print(Response.content)
python-3.x azure python-requests 

我想阅读所有内容,而不仅仅是提到的文件。

我该怎么做?

根据documentation,您可以列出文件夹中的文件,然后下载:

response = requests.get('/drives/{drive-id}/root:/New Folder/children', headers={'Authorization': 'Bearer ' + Access_Token})

content = json.loads(response.content)
for file in content.values:
    file_response = requests.get(f'/drives/{drive-id}/root:/New Folder/{file.name}/content', headers={'Authorization': 'Bearer ' + Access_Token})
    with open(file.name, 'wb') as dest_file:
        dest_file.write(file_response.content)