zsh_18
zsh_18

Reputation: 1202

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

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.

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('\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 

I would like to read all the content instead of just a mentioned file.

How can I do that?

Upvotes: 1

Views: 13996

Answers (1)

Damião Martins
Damião Martins

Reputation: 1849

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)

Upvotes: 2

Related Questions