4343from gcloud ._helpers import get_default_project
4444from gcloud .exceptions import NotFound
4545from gcloud .storage ._helpers import _PropertyMixin
46+ from gcloud .storage ._helpers import _require_connection
4647from gcloud .storage ._helpers import _scalar_property
4748from gcloud .storage .acl import BucketACL
4849from gcloud .storage .acl import DefaultObjectACL
@@ -114,23 +115,29 @@ def __contains__(self, blob_name):
114115 blob = Blob (blob_name , bucket = self )
115116 return blob .exists ()
116117
117- def exists (self ):
118+ def exists (self , connection = None ):
118119 """Determines whether or not this bucket exists.
119120
121+ :type connection: :class:`gcloud.storage.connection.Connection` or
122+ ``NoneType``
123+ :param connection: Optional. The connection to use when sending
124+ requests. If not provided, falls back to default.
125+
120126 :rtype: boolean
121127 :returns: True if the bucket exists in Cloud Storage.
122128 """
129+ connection = _require_connection (connection )
123130 try :
124131 # We only need the status code (200 or not) so we seek to
125132 # minimize the returned payload.
126133 query_params = {'fields' : 'name' }
127- self . connection .api_request (method = 'GET' , path = self .path ,
128- query_params = query_params )
134+ connection .api_request (method = 'GET' , path = self .path ,
135+ query_params = query_params )
129136 return True
130137 except NotFound :
131138 return False
132139
133- def create (self , project = None ):
140+ def create (self , project = None , connection = None ):
134141 """Creates current bucket.
135142
136143 If the bucket already exists, will raise
@@ -142,19 +149,25 @@ def create(self, project=None):
142149 :param project: Optional. The project to use when creating bucket.
143150 If not provided, falls back to default.
144151
152+ :type connection: :class:`gcloud.storage.connection.Connection` or
153+ ``NoneType``
154+ :param connection: Optional. The connection to use when sending
155+ requests. If not provided, falls back to default.
156+
145157 :rtype: :class:`gcloud.storage.bucket.Bucket`
146158 :returns: The newly created bucket.
147159 :raises: :class:`EnvironmentError` if the project is not given and
148160 can't be inferred.
149161 """
162+ connection = _require_connection (connection )
150163 if project is None :
151164 project = get_default_project ()
152165 if project is None :
153166 raise EnvironmentError ('Project could not be inferred '
154167 'from environment.' )
155168
156169 query_params = {'project' : project }
157- api_response = self . connection .api_request (
170+ api_response = connection .api_request (
158171 method = 'POST' , path = '/b' , query_params = query_params ,
159172 data = {'name' : self .name })
160173 self ._set_properties (api_response )
@@ -198,7 +211,7 @@ def path(self):
198211
199212 return self .path_helper (self .name )
200213
201- def get_blob (self , blob_name ):
214+ def get_blob (self , blob_name , connection = None ):
202215 """Get a blob object by name.
203216
204217 This will return None if the blob doesn't exist::
@@ -214,13 +227,19 @@ def get_blob(self, blob_name):
214227 :type blob_name: string
215228 :param blob_name: The name of the blob to retrieve.
216229
230+ :type connection: :class:`gcloud.storage.connection.Connection` or
231+ ``NoneType``
232+ :param connection: Optional. The connection to use when sending
233+ requests. If not provided, falls back to default.
234+
217235 :rtype: :class:`gcloud.storage.blob.Blob` or None
218236 :returns: The blob object if it exists, otherwise None.
219237 """
238+ connection = _require_connection (connection )
220239 blob = Blob (bucket = self , name = blob_name )
221240 try :
222- response = self . connection .api_request (method = 'GET' ,
223- path = blob .path )
241+ response = connection .api_request (method = 'GET' ,
242+ path = blob .path )
224243 name = response .get ('name' ) # Expect this to be blob_name
225244 blob = Blob (name , bucket = self )
226245 blob ._set_properties (response )
@@ -291,7 +310,7 @@ def list_blobs(self, max_results=None, page_token=None, prefix=None,
291310 result .next_page_token = page_token
292311 return result
293312
294- def delete (self , force = False ):
313+ def delete (self , force = False , connection = None ):
295314 """Delete this bucket.
296315
297316 The bucket **must** be empty in order to submit a delete request. If
@@ -310,9 +329,15 @@ def delete(self, force=False):
310329 :type force: boolean
311330 :param force: If True, empties the bucket's objects then deletes it.
312331
332+ :type connection: :class:`gcloud.storage.connection.Connection` or
333+ ``NoneType``
334+ :param connection: Optional. The connection to use when sending
335+ requests. If not provided, falls back to default.
336+
313337 :raises: :class:`ValueError` if ``force`` is ``True`` and the bucket
314338 contains more than 256 objects / blobs.
315339 """
340+ connection = _require_connection (connection )
316341 if force :
317342 blobs = list (self .list_blobs (
318343 max_results = self ._MAX_OBJECTS_FOR_BUCKET_DELETE + 1 ))
@@ -326,11 +351,12 @@ def delete(self, force=False):
326351 raise ValueError (message )
327352
328353 # Ignore 404 errors on delete.
329- self .delete_blobs (blobs , on_error = lambda blob : None )
354+ self .delete_blobs (blobs , on_error = lambda blob : None ,
355+ connection = connection )
330356
331- self . connection .api_request (method = 'DELETE' , path = self .path )
357+ connection .api_request (method = 'DELETE' , path = self .path )
332358
333- def delete_blob (self , blob_name ):
359+ def delete_blob (self , blob_name , connection = None ):
334360 """Deletes a blob from the current bucket.
335361
336362 If the blob isn't found (backend 404), raises a
@@ -353,16 +379,22 @@ def delete_blob(self, blob_name):
353379 :type blob_name: string
354380 :param blob_name: A blob name to delete.
355381
382+ :type connection: :class:`gcloud.storage.connection.Connection` or
383+ ``NoneType``
384+ :param connection: Optional. The connection to use when sending
385+ requests. If not provided, falls back to default.
386+
356387 :raises: :class:`gcloud.exceptions.NotFound` (to suppress
357388 the exception, call ``delete_blobs``, passing a no-op
358389 ``on_error`` callback, e.g.::
359390
360391 >>> bucket.delete_blobs([blob], on_error=lambda blob: None)
361392 """
393+ connection = _require_connection (connection )
362394 blob_path = Blob .path_helper (self .path , blob_name )
363- self . connection .api_request (method = 'DELETE' , path = blob_path )
395+ connection .api_request (method = 'DELETE' , path = blob_path )
364396
365- def delete_blobs (self , blobs , on_error = None ):
397+ def delete_blobs (self , blobs , on_error = None , connection = None ):
366398 """Deletes a list of blobs from the current bucket.
367399
368400 Uses :func:`Bucket.delete_blob` to delete each individual blob.
@@ -375,22 +407,30 @@ def delete_blobs(self, blobs, on_error=None):
375407 :class:`gcloud.exceptions.NotFound`;
376408 otherwise, the exception is propagated.
377409
410+ :type connection: :class:`gcloud.storage.connection.Connection` or
411+ ``NoneType``
412+ :param connection: Optional. The connection to use when sending
413+ requests. If not provided, falls back to default.
414+
378415 :raises: :class:`gcloud.exceptions.NotFound` (if
379416 `on_error` is not passed).
380417 """
418+ connection = _require_connection (connection )
381419 for blob in blobs :
382420 try :
383421 blob_name = blob
384422 if not isinstance (blob_name , six .string_types ):
385423 blob_name = blob .name
386- self .delete_blob (blob_name )
424+ self .delete_blob (blob_name , connection = connection )
387425 except NotFound :
388426 if on_error is not None :
389427 on_error (blob )
390428 else :
391429 raise
392430
393- def copy_blob (self , blob , destination_bucket , new_name = None ):
431+ @staticmethod
432+ def copy_blob (blob , destination_bucket , new_name = None ,
433+ connection = None ):
394434 """Copy the given blob to the given bucket, optionally with a new name.
395435
396436 :type blob: string or :class:`gcloud.storage.blob.Blob`
@@ -403,18 +443,24 @@ def copy_blob(self, blob, destination_bucket, new_name=None):
403443 :type new_name: string
404444 :param new_name: (optional) the new name for the copied file.
405445
446+ :type connection: :class:`gcloud.storage.connection.Connection` or
447+ ``NoneType``
448+ :param connection: Optional. The connection to use when sending
449+ requests. If not provided, falls back to default.
450+
406451 :rtype: :class:`gcloud.storage.blob.Blob`
407452 :returns: The new Blob.
408453 """
454+ connection = _require_connection (connection )
409455 if new_name is None :
410456 new_name = blob .name
411457 new_blob = Blob (bucket = destination_bucket , name = new_name )
412458 api_path = blob .path + '/copyTo' + new_blob .path
413- copy_result = self . connection .api_request (method = 'POST' , path = api_path )
459+ copy_result = connection .api_request (method = 'POST' , path = api_path )
414460 new_blob ._set_properties (copy_result )
415461 return new_blob
416462
417- def upload_file (self , filename , blob_name = None ):
463+ def upload_file (self , filename , blob_name = None , connection = None ):
418464 """Shortcut method to upload a file into this bucket.
419465
420466 Use this method to quickly put a local file in Cloud Storage.
@@ -447,16 +493,21 @@ def upload_file(self, filename, blob_name=None):
447493 of the bucket with the same name as on your local
448494 file system.
449495
496+ :type connection: :class:`gcloud.storage.connection.Connection` or
497+ ``NoneType``
498+ :param connection: Optional. The connection to use when sending
499+ requests. If not provided, falls back to default.
500+
450501 :rtype: :class:`Blob`
451502 :returns: The updated Blob object.
452503 """
453504 if blob_name is None :
454505 blob_name = os .path .basename (filename )
455506 blob = Blob (bucket = self , name = blob_name )
456- blob .upload_from_filename (filename )
507+ blob .upload_from_filename (filename , connection = connection )
457508 return blob
458509
459- def upload_file_object (self , file_obj , blob_name = None ):
510+ def upload_file_object (self , file_obj , blob_name = None , connection = None ):
460511 """Shortcut method to upload a file object into this bucket.
461512
462513 Use this method to quickly put a local file in Cloud Storage.
@@ -489,13 +540,18 @@ def upload_file_object(self, file_obj, blob_name=None):
489540 of the bucket with the same name as on your local
490541 file system.
491542
543+ :type connection: :class:`gcloud.storage.connection.Connection` or
544+ ``NoneType``
545+ :param connection: Optional. The connection to use when sending
546+ requests. If not provided, falls back to default.
547+
492548 :rtype: :class:`Blob`
493549 :returns: The updated Blob object.
494550 """
495551 if blob_name is None :
496552 blob_name = os .path .basename (file_obj .name )
497553 blob = Blob (bucket = self , name = blob_name )
498- blob .upload_from_file (file_obj )
554+ blob .upload_from_file (file_obj , connection = connection )
499555 return blob
500556
501557 @property
0 commit comments