Skip to content

Commit

Permalink
OneDrive examples: download a large file
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Jan 23, 2022
1 parent 6e95d01 commit bc71fab
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
16 changes: 16 additions & 0 deletions examples/onedrive/download_file_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os
import tempfile

from examples import acquire_token_by_username_password
from office365.graph_client import GraphClient
from office365.onedrive.driveitems.driveItem import DriveItem

client = GraphClient(acquire_token_by_username_password)
# 1. address file by path and get file metadata
file_item = client.me.drive.root.get_by_path("archive/Sample.rtf").get().execute_query() # type: DriveItem

# 2. download file content
with tempfile.TemporaryDirectory() as local_path:
with open(os.path.join(local_path, file_item.name), 'wb') as local_file:
file_item.download(local_file).execute_query()
print("File '{0}' has been downloaded into {1}".format(file_item.name, local_file.name))
20 changes: 20 additions & 0 deletions examples/onedrive/download_large_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os
import tempfile

from examples import acquire_token_by_username_password
from office365.graph_client import GraphClient
from office365.onedrive.driveitems.driveItem import DriveItem


def print_download_progress(offset):
print("Downloaded '{0}' bytes...".format(offset))


client = GraphClient(acquire_token_by_username_password)
# # 1. address file by path and get file metadata
file_item = client.me.drive.root.get_by_path("archive/big_buck_bunny.mp4").get().execute_query() # type: DriveItem
# 2 download a large file (chunked file download)
with tempfile.TemporaryDirectory() as local_path:
with open(os.path.join(local_path, file_item.name), 'wb') as local_file:
file_item.download_session(local_file, print_download_progress).execute_query()
print("File '{0}' has been downloaded into {1}".format(file_item.name, local_file.name))
3 changes: 2 additions & 1 deletion examples/sharepoint/connect_with_client_certificate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
cert_settings = {
'client_id': '51d03106-4726-442c-86db-70b32fa7547f',
'thumbprint': "61C754D8D9629BE91972B6A0C1999DC678FB0145",
'cert_path': '{0}/selfsigncert.pem'.format(os.path.dirname(__file__))
'cert_path': '{0}/selfsigncert.pem'.format(os.path.dirname(__file__)),
'scopes': ['{0}.default'.format(test_site_url)]
}

ctx = ClientContext(test_site_url).with_client_certificate(test_tenant, **cert_settings)
Expand Down

0 comments on commit bc71fab

Please sign in to comment.