@@ -153,6 +153,26 @@ def _pop_batch(self):
153
153
"""
154
154
return self ._batch_stack .pop ()
155
155
156
+ def _bucket_arg_to_bucket (self , bucket_or_name ):
157
+ """Helper to return given bucket or create new by name.
158
+
159
+ Args:
160
+ bucket_or_name (Union[ \
161
+ :class:`~google.cloud.storage.bucket.Bucket`, \
162
+ str, \
163
+ ]):
164
+ The bucket resource to pass or name to create.
165
+
166
+ Returns:
167
+ google.cloud.storage.bucket.Bucket
168
+ The newly created bucket or the given one.
169
+ """
170
+ if isinstance (bucket_or_name , Bucket ):
171
+ bucket = bucket_or_name
172
+ else :
173
+ bucket = Bucket (self , name = bucket_or_name )
174
+ return bucket
175
+
156
176
@property
157
177
def current_batch (self ):
158
178
"""Currently-active batch.
@@ -252,12 +272,7 @@ def get_bucket(self, bucket_or_name):
252
272
>>> bucket = client.get_bucket(bucket) # API request.
253
273
254
274
"""
255
-
256
- bucket = None
257
- if isinstance (bucket_or_name , Bucket ):
258
- bucket = bucket_or_name
259
- else :
260
- bucket = Bucket (self , name = bucket_or_name )
275
+ bucket = self ._bucket_arg_to_bucket (bucket_or_name )
261
276
262
277
bucket .reload (client = self )
263
278
return bucket
@@ -299,7 +314,7 @@ def create_bucket(self, bucket_or_name, requester_pays=None, project=None):
299
314
Optional. Whether requester pays for API requests for this
300
315
bucket and its blobs.
301
316
project (str):
302
- Optional. the project under which the bucket is to be created.
317
+ Optional. the project under which the bucket is to be created.
303
318
If not passed, uses the project set on the client.
304
319
305
320
Returns:
@@ -331,12 +346,7 @@ def create_bucket(self, bucket_or_name, requester_pays=None, project=None):
331
346
>>> bucket = client.create_bucket(bucket) # API request.
332
347
333
348
"""
334
-
335
- bucket = None
336
- if isinstance (bucket_or_name , Bucket ):
337
- bucket = bucket_or_name
338
- else :
339
- bucket = Bucket (self , name = bucket_or_name )
349
+ bucket = self ._bucket_arg_to_bucket (bucket_or_name )
340
350
341
351
if requester_pays is not None :
342
352
bucket .requester_pays = requester_pays
@@ -394,6 +404,80 @@ def download_blob_to_file(self, blob_or_uri, file_obj, start=None, end=None):
394
404
395
405
blob_or_uri .download_to_file (file_obj , client = self , start = start , end = end )
396
406
407
+ def list_blobs (
408
+ self ,
409
+ bucket_or_name ,
410
+ max_results = None ,
411
+ page_token = None ,
412
+ prefix = None ,
413
+ delimiter = None ,
414
+ versions = None ,
415
+ projection = "noAcl" ,
416
+ fields = None ,
417
+ ):
418
+ """Return an iterator used to find blobs in the bucket.
419
+
420
+ If :attr:`user_project` is set, bills the API request to that project.
421
+
422
+ Args:
423
+ bucket_or_name (Union[ \
424
+ :class:`~google.cloud.storage.bucket.Bucket`, \
425
+ str, \
426
+ ]):
427
+ The bucket resource to pass or name to create.
428
+
429
+ max_results (int):
430
+ (Optional) The maximum number of blobs in each page of results
431
+ from this request. Non-positive values are ignored. Defaults to
432
+ a sensible value set by the API.
433
+
434
+ page_token (str):
435
+ (Optional) If present, return the next batch of blobs, using the
436
+ value, which must correspond to the ``nextPageToken`` value
437
+ returned in the previous response. Deprecated: use the ``pages``
438
+ property of the returned iterator instead of manually passing the
439
+ token.
440
+
441
+ prefix (str):
442
+ (Optional) prefix used to filter blobs.
443
+
444
+ delimiter (str):
445
+ (Optional) Delimiter, used with ``prefix`` to
446
+ emulate hierarchy.
447
+
448
+ versions (bool):
449
+ (Optional) Whether object versions should be returned
450
+ as separate blobs.
451
+
452
+ projection (str):
453
+ (Optional) If used, must be 'full' or 'noAcl'.
454
+ Defaults to ``'noAcl'``. Specifies the set of
455
+ properties to return.
456
+
457
+ fields (str):
458
+ (Optional) Selector specifying which fields to include
459
+ in a partial response. Must be a list of fields. For
460
+ example to get a partial response with just the next
461
+ page token and the name and language of each blob returned:
462
+ ``'items(name,contentLanguage),nextPageToken'``.
463
+ See: https://cloud.google.com/storage/docs/json_api/v1/parameters#fields
464
+
465
+ Returns:
466
+ Iterator of all :class:`~google.cloud.storage.blob.Blob`
467
+ in this bucket matching the arguments.
468
+ """
469
+ bucket = self ._bucket_arg_to_bucket (bucket_or_name )
470
+ return bucket .list_blobs (
471
+ max_results = max_results ,
472
+ page_token = page_token ,
473
+ prefix = prefix ,
474
+ delimiter = delimiter ,
475
+ versions = versions ,
476
+ projection = projection ,
477
+ fields = fields ,
478
+ client = self
479
+ )
480
+
397
481
def list_buckets (
398
482
self ,
399
483
max_results = None ,
0 commit comments