如何读取 OneDrive 的所有文件并用 Python 进行复制
问题描述
我正在尝试将我的整个 OneDrive 同步到 AWS S3.为此,我想读取 OneDrive 中的所有文件和文件夹(以便我可以将它们写入 AWS S3).如何获取 OneDrive 文件夹中所有文件和文件的列表?我打算以数据框的形式读取所有文件,然后写入AWS S3.
I am trying to sync my whole OneDrive to AWS S3. For this purpose, I want to read all the files and folders in OneDrive (so that I can write them in AWS S3). How do I either get a list of all the files and files in the folders for OneDrive? I plan to read all files in the form of a data frame and then write it to AWS S3.
我现在使用的代码从 OneDrive 读取我的代码中明确提到的文件.代码来自 这个答案
The code that I am using right now reads a file from OneDrive that is mentioned explicitly in my code. The code is from 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('
> Failed: Access_Token NOT Retrieved')
sys.exit()
Response = requests.get(OneDrive_FileURL, headers={'Authorization': 'Bearer ' + Access_Token})
if Response.status_code == 200:
print('
> Response Success')
with open('Excel File.xlsx', 'wb') as File:
File.write(Response.content)
print('
> File Downloaded')
else:
print('
> Failed:', Response.status_code)
print(Response.content)
python-3.x azure python-requests
我想阅读所有内容,而不仅仅是提到的文件.
I would like to read all the content instead of just a mentioned file.
我该怎么做?
解决方案
根据文档,你可以列出文件夹中的文件然后下载:
According to documentation, you can list the files in a folder and then download:
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)
相关文章