|
35 | 35 | from google.cloud.storage.acl import DefaultObjectACL |
36 | 36 | from google.cloud.storage.blob import Blob |
37 | 37 | 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 |
38 | 40 |
|
39 | 41 |
|
40 | 42 | def _blobs_page_start(iterator, page, response): |
@@ -76,6 +78,26 @@ def _item_to_blob(iterator, item): |
76 | 78 | return blob |
77 | 79 |
|
78 | 80 |
|
| 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 | + |
79 | 101 | class Bucket(_PropertyMixin): |
80 | 102 | """A class representing a Bucket on Cloud Storage. |
81 | 103 |
|
@@ -159,6 +181,27 @@ def blob(self, blob_name, chunk_size=None, encryption_key=None): |
159 | 181 | return Blob(name=blob_name, bucket=self, chunk_size=chunk_size, |
160 | 182 | encryption_key=encryption_key) |
161 | 183 |
|
| 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 | + |
162 | 205 | def exists(self, client=None): |
163 | 206 | """Determines whether or not this bucket exists. |
164 | 207 |
|
@@ -382,6 +425,30 @@ def list_blobs(self, max_results=None, page_token=None, prefix=None, |
382 | 425 | iterator.prefixes = set() |
383 | 426 | return iterator |
384 | 427 |
|
| 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 | + |
385 | 452 | def delete(self, force=False, client=None): |
386 | 453 | """Delete this bucket. |
387 | 454 |
|
|
0 commit comments