19
19
20
20
from requests_oauthlib import OAuth1Session
21
21
22
- from twitter_ads .utils import get_version , http_time
22
+ from twitter_ads .utils import get_version , http_time , size
23
23
from twitter_ads .error import Error
24
24
25
25
@@ -195,7 +195,9 @@ class TONUpload(object):
195
195
_DEFAULT_RESOURCE = '/1.1/ton/bucket/'
196
196
_DEFAULT_BUCKET = 'ta_partner'
197
197
_DEFAULT_EXPIRE = datetime .now () + timedelta (days = 10 )
198
- _MIN_FILE_SIZE = 1024 * 1024 * 64
198
+ _DEFAULT_CHUNK_SIZE = 64
199
+ _SINGLE_UPLOAD_MAX = 1024 * 1024 * _DEFAULT_CHUNK_SIZE
200
+ _RESPONSE_TIME_MAX = 5000
199
201
200
202
def __init__ (self , client , file_path , ** kwargs ):
201
203
if not os .path .isfile (file_path ):
@@ -240,13 +242,14 @@ def content_type(self):
240
242
def perform (self ):
241
243
"""Executes the current TONUpload object."""
242
244
243
- if self ._file_size < self ._MIN_FILE_SIZE :
245
+ if self ._file_size < self ._SINGLE_UPLOAD_MAX :
244
246
resource = "{0}{1}" .format (self ._DEFAULT_RESOURCE , self .bucket )
245
247
response = self .__upload (resource , open (self ._file_path , 'rb' ).read ())
246
248
return response .headers ['location' ]
247
249
else :
248
250
response = self .__init_chunked_upload ()
249
- chunk_size = int (response .headers ['x-ton-min-chunk-size' ])
251
+ min_chunk_size = int (response .headers ['x-ton-min-chunk-size' ])
252
+ chunk_size = min_chunk_size * self ._DEFAULT_CHUNK_SIZE
250
253
location = response .headers ['location' ]
251
254
252
255
f = open (self ._file_path , 'rb' )
@@ -257,7 +260,11 @@ def perform(self):
257
260
break
258
261
bytes_start = bytes_read
259
262
bytes_read += len (bytes )
260
- self .__upload_chunk (location , chunk_size , bytes , bytes_start , bytes_read )
263
+ response = self .__upload_chunk (location , chunk_size , bytes , bytes_start , bytes_read )
264
+ response_time = int (response .headers ['x-response-time' ])
265
+ chunk_size = min_chunk_size * size (self ._DEFAULT_CHUNK_SIZE ,
266
+ self ._RESPONSE_TIME_MAX ,
267
+ response_time )
261
268
f .close ()
262
269
263
270
return location .split ("?" )[0 ]
0 commit comments