@@ -45,9 +45,12 @@ <h1 class="title">Module <code>nitric.api.storage</code></h1>
4545# limitations under the License.
4646#
4747from dataclasses import dataclass
48+ from typing import Union
4849
4950from grpclib import GRPCError
50- from nitric.api.exception import exception_from_grpc_error, InvalidArgumentException
51+ from nitric.exception import exception_from_grpc_error, InvalidArgumentException
52+ from nitric.application import Nitric
53+ from nitric.faas import FunctionServer, FileNotificationWorkerOptions, FileNotificationMiddleware
5154from nitric.utils import new_default_channel
5255from nitric.proto.nitric.storage.v1 import (
5356 StorageServiceStub,
@@ -59,6 +62,7 @@ <h1 class="title">Module <code>nitric.api.storage</code></h1>
5962 StorageListFilesRequest,
6063)
6164from enum import Enum
65+ from warnings import warn
6266
6367
6468class Storage(object):
@@ -80,15 +84,16 @@ <h1 class="title">Module <code>nitric.api.storage</code></h1>
8084
8185 def bucket(self, name: str):
8286 """Return a reference to a bucket from the connected storage service."""
83- return BucketRef(_storage=self, name=name)
87+ return BucketRef(_storage=self, name=name, _server=None )
8488
8589
86- @dataclass(frozen=True, order=True)
90+ @dataclass(order=True)
8791class BucketRef(object):
8892 """A reference to a bucket in a storage service, used to the perform operations on that bucket."""
8993
9094 _storage: Storage
9195 name: str
96+ _server: Union[FunctionServer, None]
9297
9398 def file(self, key: str):
9499 """Return a reference to a file in this bucket."""
@@ -101,6 +106,22 @@ <h1 class="title">Module <code>nitric.api.storage</code></h1>
101106 )
102107 return [self.file(f.key) for f in resp.files]
103108
109+ def on(self, notification_type: str, notification_prefix_filter: str):
110+ """Create and return a bucket notification decorator for this bucket."""
111+
112+ def decorator(func: FileNotificationMiddleware):
113+ self._server = FunctionServer(
114+ FileNotificationWorkerOptions(
115+ bucket=self,
116+ notification_type=notification_type,
117+ notification_prefix_filter=notification_prefix_filter,
118+ )
119+ )
120+ self._server.bucket_notification(func)
121+ Nitric._register_worker(self._server)
122+
123+ return decorator
124+
104125
105126class FileMode(Enum):
106127 """Definition of available operation modes for file signed URLs."""
@@ -160,14 +181,15 @@ <h1 class="title">Module <code>nitric.api.storage</code></h1>
160181
161182 async def upload_url(self, expiry: int = 600):
162183 """Get a temporary writable URL to this file."""
163- await self.sign_url(mode=FileMode.WRITE, expiry=expiry)
184+ return await self.sign_url(mode=FileMode.WRITE, expiry=expiry)
164185
165186 async def download_url(self, expiry: int = 600):
166187 """Get a temporary readable URL to this file."""
167- await self.sign_url(mode=FileMode.READ, expiry=expiry)
188+ return await self.sign_url(mode=FileMode.READ, expiry=expiry)
168189
169190 async def sign_url(self, mode: FileMode = FileMode.READ, expiry: int = 3600):
170191 """Generate a signed URL for reading or writing to a file."""
192+ warn("File.sign_url() is deprecated, use upload_url() or download_url() instead", DeprecationWarning)
171193 try:
172194 response = await self._storage._storage_stub.pre_sign_url(
173195 storage_pre_sign_url_request=StoragePreSignUrlRequest(
@@ -190,20 +212,21 @@ <h2 class="section-title" id="header-classes">Classes</h2>
190212< dl >
191213< dt id ="nitric.api.storage.BucketRef "> < code class ="flex name class ">
192214< span > class < span class ="ident "> BucketRef</ span > </ span >
193- < span > (</ span > < span > _storage: < a title ="nitric.api.storage.Storage " href ="#nitric.api.storage.Storage "> Storage</ a > , name: str)</ span >
215+ < span > (</ span > < span > _storage: < a title ="nitric.api.storage.Storage " href ="#nitric.api.storage.Storage "> Storage</ a > , name: str, _server: Optional[ < a title =" nitric.faas.FunctionServer " href =" ../faas.html#nitric.faas.FunctionServer " > FunctionServer </ a > ] )</ span >
194216</ code > </ dt >
195217< dd >
196218< div class ="desc "> < p > A reference to a bucket in a storage service, used to the perform operations on that bucket.</ p > </ div >
197219< details class ="source ">
198220< summary >
199221< span > Expand source code</ span >
200222</ summary >
201- < pre > < code class ="python "> @dataclass(frozen=True, order=True)
223+ < pre > < code class ="python "> @dataclass(order=True)
202224class BucketRef(object):
203225 """A reference to a bucket in a storage service, used to the perform operations on that bucket."""
204226
205227 _storage: Storage
206228 name: str
229+ _server: Union[FunctionServer, None]
207230
208231 def file(self, key: str):
209232 """Return a reference to a file in this bucket."""
@@ -214,7 +237,23 @@ <h2 class="section-title" id="header-classes">Classes</h2>
214237 resp = await self._storage._storage_stub.list_files(
215238 storage_list_files_request=StorageListFilesRequest(bucket_name=self.name)
216239 )
217- return [self.file(f.key) for f in resp.files]</ code > </ pre >
240+ return [self.file(f.key) for f in resp.files]
241+
242+ def on(self, notification_type: str, notification_prefix_filter: str):
243+ """Create and return a bucket notification decorator for this bucket."""
244+
245+ def decorator(func: FileNotificationMiddleware):
246+ self._server = FunctionServer(
247+ FileNotificationWorkerOptions(
248+ bucket=self,
249+ notification_type=notification_type,
250+ notification_prefix_filter=notification_prefix_filter,
251+ )
252+ )
253+ self._server.bucket_notification(func)
254+ Nitric._register_worker(self._server)
255+
256+ return decorator</ code > </ pre >
218257</ details >
219258< h3 > Class variables</ h3 >
220259< dl >
@@ -256,6 +295,32 @@ <h3>Methods</h3>
256295 return [self.file(f.key) for f in resp.files]</ code > </ pre >
257296</ details >
258297</ dd >
298+ < dt id ="nitric.api.storage.BucketRef.on "> < code class ="name flex ">
299+ < span > def < span class ="ident "> on</ span > </ span > (< span > self, notification_type: str, notification_prefix_filter: str)</ span >
300+ </ code > </ dt >
301+ < dd >
302+ < div class ="desc "> < p > Create and return a bucket notification decorator for this bucket.</ p > </ div >
303+ < details class ="source ">
304+ < summary >
305+ < span > Expand source code</ span >
306+ </ summary >
307+ < pre > < code class ="python "> def on(self, notification_type: str, notification_prefix_filter: str):
308+ """Create and return a bucket notification decorator for this bucket."""
309+
310+ def decorator(func: FileNotificationMiddleware):
311+ self._server = FunctionServer(
312+ FileNotificationWorkerOptions(
313+ bucket=self,
314+ notification_type=notification_type,
315+ notification_prefix_filter=notification_prefix_filter,
316+ )
317+ )
318+ self._server.bucket_notification(func)
319+ Nitric._register_worker(self._server)
320+
321+ return decorator</ code > </ pre >
322+ </ details >
323+ </ dd >
259324</ dl >
260325</ dd >
261326< dt id ="nitric.api.storage.File "> < code class ="flex name class ">
@@ -310,14 +375,15 @@ <h3>Methods</h3>
310375
311376 async def upload_url(self, expiry: int = 600):
312377 """Get a temporary writable URL to this file."""
313- await self.sign_url(mode=FileMode.WRITE, expiry=expiry)
378+ return await self.sign_url(mode=FileMode.WRITE, expiry=expiry)
314379
315380 async def download_url(self, expiry: int = 600):
316381 """Get a temporary readable URL to this file."""
317- await self.sign_url(mode=FileMode.READ, expiry=expiry)
382+ return await self.sign_url(mode=FileMode.READ, expiry=expiry)
318383
319384 async def sign_url(self, mode: FileMode = FileMode.READ, expiry: int = 3600):
320385 """Generate a signed URL for reading or writing to a file."""
386+ warn("File.sign_url() is deprecated, use upload_url() or download_url() instead", DeprecationWarning)
321387 try:
322388 response = await self._storage._storage_stub.pre_sign_url(
323389 storage_pre_sign_url_request=StoragePreSignUrlRequest(
@@ -367,7 +433,7 @@ <h3>Methods</h3>
367433</ summary >
368434< pre > < code class ="python "> async def download_url(self, expiry: int = 600):
369435 """Get a temporary readable URL to this file."""
370- await self.sign_url(mode=FileMode.READ, expiry=expiry)</ code > </ pre >
436+ return await self.sign_url(mode=FileMode.READ, expiry=expiry)</ code > </ pre >
371437</ details >
372438</ dd >
373439< dt id ="nitric.api.storage.File.read "> < code class ="name flex ">
@@ -401,6 +467,7 @@ <h3>Methods</h3>
401467</ summary >
402468< pre > < code class ="python "> async def sign_url(self, mode: FileMode = FileMode.READ, expiry: int = 3600):
403469 """Generate a signed URL for reading or writing to a file."""
470+ warn("File.sign_url() is deprecated, use upload_url() or download_url() instead", DeprecationWarning)
404471 try:
405472 response = await self._storage._storage_stub.pre_sign_url(
406473 storage_pre_sign_url_request=StoragePreSignUrlRequest(
@@ -423,7 +490,7 @@ <h3>Methods</h3>
423490</ summary >
424491< pre > < code class ="python "> async def upload_url(self, expiry: int = 600):
425492 """Get a temporary writable URL to this file."""
426- await self.sign_url(mode=FileMode.WRITE, expiry=expiry)</ code > </ pre >
493+ return await self.sign_url(mode=FileMode.WRITE, expiry=expiry)</ code > </ pre >
427494</ details >
428495</ dd >
429496< dt id ="nitric.api.storage.File.write "> < code class ="name flex ">
@@ -545,7 +612,7 @@ <h3>Methods</h3>
545612
546613 def bucket(self, name: str):
547614 """Return a reference to a bucket from the connected storage service."""
548- return BucketRef(_storage=self, name=name)</ code > </ pre >
615+ return BucketRef(_storage=self, name=name, _server=None )</ code > </ pre >
549616</ details >
550617< h3 > Methods</ h3 >
551618< dl >
@@ -560,7 +627,7 @@ <h3>Methods</h3>
560627</ summary >
561628< pre > < code class ="python "> def bucket(self, name: str):
562629 """Return a reference to a bucket from the connected storage service."""
563- return BucketRef(_storage=self, name=name)</ code > </ pre >
630+ return BucketRef(_storage=self, name=name, _server=None )</ code > </ pre >
564631</ details >
565632</ dd >
566633</ dl >
@@ -587,6 +654,7 @@ <h4><code><a title="nitric.api.storage.BucketRef" href="#nitric.api.storage.Buck
587654< li > < code > < a title ="nitric.api.storage.BucketRef.file " href ="#nitric.api.storage.BucketRef.file "> file</ a > </ code > </ li >
588655< li > < code > < a title ="nitric.api.storage.BucketRef.files " href ="#nitric.api.storage.BucketRef.files "> files</ a > </ code > </ li >
589656< li > < code > < a title ="nitric.api.storage.BucketRef.name " href ="#nitric.api.storage.BucketRef.name "> name</ a > </ code > </ li >
657+ < li > < code > < a title ="nitric.api.storage.BucketRef.on " href ="#nitric.api.storage.BucketRef.on "> on</ a > </ code > </ li >
590658</ ul >
591659</ li >
592660< li >
0 commit comments