@@ -331,7 +331,7 @@ def download_as_string(self, connection=None):
331331 return string_buffer .getvalue ()
332332
333333 def upload_from_file (self , file_obj , rewind = False , size = None ,
334- content_type = None , num_retries = 6 ):
334+ content_type = None , num_retries = 6 , connection = None ):
335335 """Upload the contents of this blob from a file-like object.
336336
337337 The content type of the upload will either be
@@ -367,7 +367,13 @@ def upload_from_file(self, file_obj, rewind=False, size=None,
367367
368368 :type num_retries: integer
369369 :param num_retries: Number of upload retries. Defaults to 6.
370+
371+ :type connection: :class:`gcloud.storage.connection.Connection` or
372+ ``NoneType``
373+ :param connection: Optional. The connection to use when sending
374+ requests. If not provided, falls back to default.
370375 """
376+ connection = _require_connection (connection )
371377 content_type = (content_type or self ._properties .get ('contentType' ) or
372378 'application/octet-stream' )
373379
@@ -377,11 +383,10 @@ def upload_from_file(self, file_obj, rewind=False, size=None,
377383
378384 # Get the basic stats about the file.
379385 total_bytes = size or os .fstat (file_obj .fileno ()).st_size
380- conn = self .connection
381386 headers = {
382387 'Accept' : 'application/json' ,
383388 'Accept-Encoding' : 'gzip, deflate' ,
384- 'User-Agent' : conn .USER_AGENT ,
389+ 'User-Agent' : connection .USER_AGENT ,
385390 }
386391
387392 upload = transfer .Upload (file_obj , content_type , total_bytes ,
@@ -393,20 +398,20 @@ def upload_from_file(self, file_obj, rewind=False, size=None,
393398 upload_config = _UploadConfig ()
394399
395400 # Temporary URL, until we know simple vs. resumable.
396- base_url = conn .API_BASE_URL + '/upload'
397- upload_url = conn .build_api_url (api_base_url = base_url ,
398- path = self .bucket .path + '/o' )
401+ base_url = connection .API_BASE_URL + '/upload'
402+ upload_url = connection .build_api_url (api_base_url = base_url ,
403+ path = self .bucket .path + '/o' )
399404
400405 # Use apitools 'Upload' facility.
401406 request = http_wrapper .Request (upload_url , 'POST' , headers )
402407
403408 upload .ConfigureRequest (upload_config , request , url_builder )
404409 query_params = url_builder .query_params
405- base_url = conn .API_BASE_URL + '/upload'
406- request .url = conn .build_api_url (api_base_url = base_url ,
407- path = self .bucket .path + '/o' ,
408- query_params = query_params )
409- upload .InitializeUpload (request , conn .http )
410+ base_url = connection .API_BASE_URL + '/upload'
411+ request .url = connection .build_api_url (api_base_url = base_url ,
412+ path = self .bucket .path + '/o' ,
413+ query_params = query_params )
414+ upload .InitializeUpload (request , connection .http )
410415
411416 # Should we be passing callbacks through from caller? We can't
412417 # pass them as None, because apitools wants to print to the console
@@ -416,15 +421,16 @@ def upload_from_file(self, file_obj, rewind=False, size=None,
416421 callback = lambda * args : None ,
417422 finish_callback = lambda * args : None )
418423 else :
419- http_response = http_wrapper .MakeRequest (conn .http , request ,
424+ http_response = http_wrapper .MakeRequest (connection .http , request ,
420425 retries = num_retries )
421426 response_content = http_response .content
422427 if not isinstance (response_content ,
423428 six .string_types ): # pragma: NO COVER Python3
424429 response_content = response_content .decode ('utf-8' )
425430 self ._set_properties (json .loads (response_content ))
426431
427- def upload_from_filename (self , filename , content_type = None ):
432+ def upload_from_filename (self , filename , content_type = None ,
433+ connection = None ):
428434 """Upload this blob's contents from the content of a named file.
429435
430436 The content type of the upload will either be
@@ -448,15 +454,22 @@ def upload_from_filename(self, filename, content_type=None):
448454
449455 :type content_type: string or ``NoneType``
450456 :param content_type: Optional type of content being uploaded.
457+
458+ :type connection: :class:`gcloud.storage.connection.Connection` or
459+ ``NoneType``
460+ :param connection: Optional. The connection to use when sending
461+ requests. If not provided, falls back to default.
451462 """
452463 content_type = content_type or self ._properties .get ('contentType' )
453464 if content_type is None :
454465 content_type , _ = mimetypes .guess_type (filename )
455466
456467 with open (filename , 'rb' ) as file_obj :
457- self .upload_from_file (file_obj , content_type = content_type )
468+ self .upload_from_file (file_obj , content_type = content_type ,
469+ connection = connection )
458470
459- def upload_from_string (self , data , content_type = 'text/plain' ):
471+ def upload_from_string (self , data , content_type = 'text/plain' ,
472+ connection = None ):
460473 """Upload contents of this blob from the provided string.
461474
462475 .. note::
@@ -473,14 +486,19 @@ def upload_from_string(self, data, content_type='text/plain'):
473486 :type data: bytes or text
474487 :param data: The data to store in this blob. If the value is
475488 text, it will be encoded as UTF-8.
489+
490+ :type connection: :class:`gcloud.storage.connection.Connection` or
491+ ``NoneType``
492+ :param connection: Optional. The connection to use when sending
493+ requests. If not provided, falls back to default.
476494 """
477495 if isinstance (data , six .text_type ):
478496 data = data .encode ('utf-8' )
479497 string_buffer = BytesIO ()
480498 string_buffer .write (data )
481499 self .upload_from_file (file_obj = string_buffer , rewind = True ,
482- size = len (data ),
483- content_type = content_type )
500+ size = len (data ), content_type = content_type ,
501+ connection = connection )
484502
485503 def make_public (self ):
486504 """Make this blob public giving all users read access."""
0 commit comments