1919
2020from requests_oauthlib import OAuth1Session
2121
22- from twitter_ads .utils import get_version , http_time
22+ from twitter_ads .utils import get_version , http_time , size
2323from twitter_ads .error import Error
2424
2525
@@ -195,7 +195,9 @@ class TONUpload(object):
195195 _DEFAULT_RESOURCE = '/1.1/ton/bucket/'
196196 _DEFAULT_BUCKET = 'ta_partner'
197197 _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
199201
200202 def __init__ (self , client , file_path , ** kwargs ):
201203 if not os .path .isfile (file_path ):
@@ -240,13 +242,14 @@ def content_type(self):
240242 def perform (self ):
241243 """Executes the current TONUpload object."""
242244
243- if self ._file_size < self ._MIN_FILE_SIZE :
245+ if self ._file_size < self ._SINGLE_UPLOAD_MAX :
244246 resource = "{0}{1}" .format (self ._DEFAULT_RESOURCE , self .bucket )
245247 response = self .__upload (resource , open (self ._file_path , 'rb' ).read ())
246248 return response .headers ['location' ]
247249 else :
248250 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
250253 location = response .headers ['location' ]
251254
252255 f = open (self ._file_path , 'rb' )
@@ -257,7 +260,11 @@ def perform(self):
257260 break
258261 bytes_start = bytes_read
259262 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 )
261268 f .close ()
262269
263270 return location .split ("?" )[0 ]
0 commit comments