-
-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathupload_large.py
More file actions
40 lines (28 loc) · 1.55 KB
/
Copy pathupload_large.py
File metadata and controls
40 lines (28 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
Demonstrates how to upload a large file using chunked upload session.
See https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-folders-and-files-with-rest#working-with-large-files-by-using-rest
"""
import argparse
import os
from typing import Any
from office365.sharepoint.client_context import ClientContext
from tests.settings import client_id, password, site_url, tenant, username
def main():
parser = argparse.ArgumentParser(description="Upload a large file using a chunked upload session")
parser.add_argument("--path", default="../../../tests/data/big_buck_bunny.mp4", help="path to the local file")
parser.add_argument("--chunk-size", type=int, default=1_000_000, help="chunk size in bytes")
parser.add_argument("--target-folder", default="Shared Documents/archive", help="server-relative target folder URL")
args = parser.parse_args()
def print_progress(offset: int, *_: Any) -> None:
file_size = os.path.getsize(args.path)
pct = offset / file_size * 100
print(f"Uploaded {offset} bytes / {file_size} bytes ({pct:.1f}%)")
ctx = ClientContext(site_url).with_username_and_password(
tenant=tenant, client_id=client_id, username=username, password=password
)
target_folder = ctx.web.get_folder_by_server_relative_url(args.target_folder)
with open(args.path, "rb") as f:
uploaded = target_folder.files.create_upload_session(f, args.chunk_size, print_progress).execute_query()
print(f"{uploaded.server_relative_url} uploaded")
if __name__ == "__main__":
main()