/// <summary> /// /// <para>CreateFilePubSubNotification:</para> /// /// <para>Creates file based pub/sub notification</para> /// /// <para>Check <seealso cref="IBFileServiceInterface.CreateFilePubSubNotification"/> for detailed documentation</para> /// /// </summary> public bool CreateFilePubSubNotification(string _BucketName, string _TopicName, string _PathPrefixToListen, List <EBFilePubSubNotificationEventType> _EventsToListen, Action <string> _ErrorMessageAction = null) { var EventTypes = new List <string>(); if (_EventsToListen.Contains(EBFilePubSubNotificationEventType.Uploaded)) { EventTypes.Add("OBJECT_FINALIZE"); } if (_EventsToListen.Contains(EBFilePubSubNotificationEventType.Deleted)) { EventTypes.Add("OBJECT_DELETE"); } try { var Created = GSClient.CreateNotification(_BucketName, new Google.Apis.Storage.v1.Data.Notification() { PayloadFormat = "JSON_API_V1", Topic = "//pubsub.googleapis.com/projects/" + ProjectID + "/topics/" + _TopicName, EventTypes = EventTypes, ObjectNamePrefix = _PathPrefixToListen }); if (Created == null) { _ErrorMessageAction?.Invoke("BFileServiceGC->CreateFilePubSubNotification: Notification could not be created."); return(false); } } catch (Exception e) { _ErrorMessageAction?.Invoke("BFileServiceGC->CreateFilePubSubNotification: " + e.Message + ", trace: " + e.StackTrace); return(false); } return(true); }
public void NotificationsOverview() { string projectId = _fixture.ProjectId; string bucket = _fixture.BucketName; string topicId = "topic-" + Guid.NewGuid().ToString().ToLowerInvariant(); // Sample: NotificationsOverview // First create a Pub/Sub topic. PublisherClient publisherClient = PublisherClient.Create(); TopicName topicName = new TopicName(projectId, topicId); publisherClient.CreateTopic(topicName); // Prepare the topic for Storage notifications. The Storage Service Account must have Publish permission // for the topic. The code below adds the service account into the "roles/pubsub.publisher" role for the topic. // Determine the Storage Service Account name to use in IAM operations. StorageClient storageClient = StorageClient.Create(); string storageServiceAccount = $"serviceAccount:{storageClient.GetStorageServiceAccountEmail(projectId)}"; // Fetch the IAM policy for the topic. Iam.V1.Policy policy = publisherClient.GetIamPolicy(topicName.ToString()); var role = "roles/pubsub.publisher"; // Ensure the Storage Service Account is in the publisher role, setting the IAM policy for the topic // on the server if necessary. if (policy.AddRoleMember(role, storageServiceAccount)) { publisherClient.SetIamPolicy(topicName.ToString(), policy); } // Now that the topic is ready, we can create a notification configuration for Storage Notification notification = new Notification { Topic = $"//pubsub.googleapis.com/{topicName}", PayloadFormat = "JSON_API_V1" }; notification = storageClient.CreateNotification(bucket, notification); Console.WriteLine($"Created notification ID: {notification.Id}"); // End sample _fixture.RegisterTopicToDelete(topicName); }
public void CreateNotification() { var projectId = _fixture.ProjectId; string bucket = _fixture.BucketName; // This creates the topic, which is most of the work... var created = _fixture.CreateNotification("prefix2"); var topicId = created.Topic.Split('/').Last(); // Snippet: CreateNotification(string, Notification, *) TopicName topicName = new TopicName(projectId, topicId); StorageClient client = StorageClient.Create(); Notification notification = new Notification { Topic = $"//pubsub.googleapis.com/{topicName}", PayloadFormat = "JSON_API_V1" }; notification = client.CreateNotification(bucket, notification); Console.WriteLine($"Created notification ID: {notification.Id}"); // End snippet }