Skip to content

Commit ce7afe6

Browse files
authored
Add support for bucket notifications (googleapis#4127)
Closes googleapis#3956.
1 parent 3123de6 commit ce7afe6

File tree

6 files changed

+1061
-1
lines changed

6 files changed

+1061
-1
lines changed

storage/google/cloud/storage/bucket.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
from google.cloud.storage.acl import DefaultObjectACL
3636
from google.cloud.storage.blob import Blob
3737
from google.cloud.storage.blob import _get_encryption_headers
38+
from google.cloud.storage.notification import BucketNotification
39+
from google.cloud.storage.notification import NONE_PAYLOAD_FORMAT
3840

3941

4042
def _blobs_page_start(iterator, page, response):
@@ -76,6 +78,26 @@ def _item_to_blob(iterator, item):
7678
return blob
7779

7880

81+
def _item_to_notification(iterator, item):
82+
"""Convert a JSON blob to the native object.
83+
84+
.. note::
85+
86+
This assumes that the ``bucket`` attribute has been
87+
added to the iterator after being created.
88+
89+
:type iterator: :class:`~google.api.core.page_iterator.Iterator`
90+
:param iterator: The iterator that has retrieved the item.
91+
92+
:type item: dict
93+
:param item: An item to be converted to a blob.
94+
95+
:rtype: :class:`.BucketNotification`
96+
:returns: The next notification being iterated.
97+
"""
98+
return BucketNotification.from_api_repr(item, bucket=iterator.bucket)
99+
100+
79101
class Bucket(_PropertyMixin):
80102
"""A class representing a Bucket on Cloud Storage.
81103
@@ -159,6 +181,27 @@ def blob(self, blob_name, chunk_size=None, encryption_key=None):
159181
return Blob(name=blob_name, bucket=self, chunk_size=chunk_size,
160182
encryption_key=encryption_key)
161183

184+
def notification(self, topic_name,
185+
topic_project=None,
186+
custom_attributes=None,
187+
event_types=None,
188+
blob_name_prefix=None,
189+
payload_format=NONE_PAYLOAD_FORMAT):
190+
"""Factory: create a notification resource for the bucket.
191+
192+
See: :class:`.BucketNotification` for parameters.
193+
194+
:rtype: :class:`.BucketNotification`
195+
"""
196+
return BucketNotification(
197+
self, topic_name,
198+
topic_project=topic_project,
199+
custom_attributes=custom_attributes,
200+
event_types=event_types,
201+
blob_name_prefix=blob_name_prefix,
202+
payload_format=payload_format,
203+
)
204+
162205
def exists(self, client=None):
163206
"""Determines whether or not this bucket exists.
164207
@@ -382,6 +425,30 @@ def list_blobs(self, max_results=None, page_token=None, prefix=None,
382425
iterator.prefixes = set()
383426
return iterator
384427

428+
def list_notifications(self, client=None):
429+
"""List Pub / Sub notifications for this bucket.
430+
431+
See:
432+
https://cloud.google.com/storage/docs/json_api/v1/notifications/list
433+
434+
:type client: :class:`~google.cloud.storage.client.Client` or
435+
``NoneType``
436+
:param client: Optional. The client to use. If not passed, falls back
437+
to the ``client`` stored on the current bucket.
438+
439+
:rtype: list of :class:`.BucketNotification`
440+
:returns: notification instances
441+
"""
442+
client = self._require_client(client)
443+
path = self.path + '/notificationConfigs'
444+
iterator = page_iterator.HTTPIterator(
445+
client=client,
446+
api_request=client._connection.api_request,
447+
path=path,
448+
item_to_value=_item_to_notification)
449+
iterator.bucket = self
450+
return iterator
451+
385452
def delete(self, force=False, client=None):
386453
"""Delete this bucket.
387454

0 commit comments

Comments
 (0)