The C# .NET SDK currently only supports legacy versions of Nitric prior to v1. This version is maintained for compatibility with existing projects and not recommended for new projects. New projects should be started using a supported SDK (presented automatically using the `nitric new` command) orget in touch to request an update to the latest version.
.NET - Bucket.On()
Create a new bucket notification trigger when certain files are created or deleted.
using System;
using Nitric.Sdk;
using Nitric.Sdk.Resource;
using Nitric.Sdk.Storage;
using BucketNotificationType = Nitric.Sdk.Function.BucketNotificationType;
var assets = Nitric.Bucket("assets");
var accessibleAssets = Nitric.Bucket("assets").With(BucketPermission.Reading);
// The request will contain the name of the file `Key` and the type of event `NotificationType`
assets.On(BucketNotificationType.Delete, "*", context => {
Console.WriteLine("A file named " + context.Req.Key + "was deleted");
return context;
});
assets.On(BucketNotificationType.Write, "/images/cat", context => {
Console.WriteLine("A cat image was written");
return context;
});
// If `.On()` is called with a permissioned bucket, a file reference will also be provided with the request
accessibleAssets.On(BucketNotificationType.Write, "/images/dog", context => {
var dogImage = context.Req.File.Read();
Console.WriteLine(dogImage.ToString());
return context;
});
Nitric.Run();
Parameters
- Name
name
- Required
- Required
- Type
- string
- Description
The unique name/reference to the file.
- Name
notificationType
- Required
- Required
- Type
- BucketNotificationType.Write | BucketNotificationType.Delete
- Description
The notification type for a triggered event, either on a file write or a file delete.
- Name
notificationPrefixFilter
- Required
- Required
- Type
- string
- Description
The file prefix filter that must match for a triggered event. If multiple filters overlap across notifications then an error will be thrown when registering the resource.
- Name
middleware
- Required
- Required
- Type
- BucketNotificationMiddleware | BucketNotificationMiddleware[]
- Description
The middleware (code) to be triggered when a bucket event is triggered.
Available Trigger Types
BucketNotificationType.Write
Triggered when a file in the bucket is created using: file.Write()
BucketNotificationType.Delete
Triggered when a file in the bucket is deleted using: file.Delete()
Cloud Trigger Types
Permission | AWS | GCP | Azure |
---|---|---|---|
BucketNotificationType.Write | s3:ObjectCreated:* | OBJECT_FINALIZE | Microsoft.Storage.BlobCreated |
BucketNotificationType.Delete | s3:ObjectRemoved:* | OBJECT_DELETE | Microsoft.Storage.BlobDeleted |