1
1
import os
2
2
import uuid
3
3
from unittest import TestCase
4
- from office365 .onedrive .driveItemUploadableProperties import DriveItemUploadableProperties
5
- from office365 .runtime .utilities .http_method import HttpMethod
6
- from office365 .runtime .utilities .request_options import RequestOptions
4
+ from office365 .onedrive .file_upload import ResumableFileUpload
7
5
from settings import settings
8
6
9
7
from office365 .graphClient import GraphClient
@@ -18,16 +16,6 @@ def get_token(auth_ctx):
18
16
return token
19
17
20
18
21
- def read_in_chunks (file_object , chunk_size = 1024 ):
22
- """Lazy function (generator) to read a file piece by piece.
23
- Default chunk size: 1k."""
24
- while True :
25
- data = file_object .read (chunk_size )
26
- if not data :
27
- break
28
- yield data
29
-
30
-
31
19
def create_listDrive (client ):
32
20
list_info = {
33
21
"displayName" : "Lib_" + uuid .uuid4 ().hex ,
@@ -70,6 +58,13 @@ def test2_upload_file(self):
70
58
self .client .execute_query ()
71
59
self .assertIsNotNone (self .target_file .webUrl )
72
60
61
+ def test21_upload_file_session (self ):
62
+ file_name = "big_buck_bunny.mp4"
63
+ local_path = "{0}/data/{1}" .format (os .path .dirname (__file__ ), file_name )
64
+ uploader = ResumableFileUpload (self .target_drive .root , local_path , 1000000 )
65
+ uploader .execute ()
66
+ print ("{0} bytes has been uploaded" .format (0 ))
67
+
73
68
def test3_download_file (self ):
74
69
result = self .__class__ .target_file .download ()
75
70
self .client .execute_query ()
@@ -97,31 +92,3 @@ def test6_delete_file(self):
97
92
self .client .execute_query ()
98
93
99
94
self .assertEqual (before_count - 1 , len (items ))
100
-
101
- def test7_upload_file_session (self ):
102
- file_name = "big_buck_bunny.mp4"
103
- path = "{0}/data/{1}" .format (os .path .dirname (__file__ ), file_name )
104
- # 1. create a file
105
- target_item = self .target_drive .root .upload (file_name , None )
106
- self .client .execute_query ()
107
- self .assertIsNotNone (target_item .properties ['id' ])
108
- # 2. create upload session
109
- item = DriveItemUploadableProperties ()
110
- item .name = file_name
111
- session_result = target_item .create_upload_session (item )
112
- self .client .execute_query ()
113
- self .assertIsNotNone (session_result .value )
114
- # 3. start upload
115
- f = open (path , 'rb' )
116
- st = os .stat (path )
117
- f_pos = 0
118
- for piece in read_in_chunks (f , chunk_size = 1000000 ):
119
- req = RequestOptions (session_result .value .uploadUrl )
120
- req .method = HttpMethod .Put
121
- req .set_header ('Content-Length' , str (len (piece )))
122
- req .set_header ('Content-Range' , 'bytes {0}-{1}/{2}' .format (f_pos , (f_pos + len (piece ) - 1 ), st .st_size ))
123
- req .set_header ('Accept' , '*/*' )
124
- req .data = piece
125
- resp = self .client .execute_request_direct (req )
126
- self .assertTrue (resp .ok )
127
- f_pos += len (piece )
0 commit comments