-
Notifications
You must be signed in to change notification settings - Fork 93
docs: Adding PubSub Notification Samples #1317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b8b5e5c
Adding PubSub Notification Samples
sydney-munro 37c0c33
Fix checkstyle issues
sydney-munro 584abff
Address PR comments
sydney-munro f790f1d
Removing unneeded newline
sydney-munro 9022a7c
Making topics test specific to avoid conflicts
sydney-munro ab6979b
Checkstyle fixes
sydney-munro ddbc554
Adding newlines in comment section to improve readability
sydney-munro acb0115
Addressing PR comments on ETAG and Selflink not being included in the…
sydney-munro 9e0d817
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...les/snippets/src/main/java/com/example/storage/bucket/CreateBucketPubSubNotification.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Copyright 2022 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.storage.bucket; | ||
|
|
||
| // [START storage_create_bucket_notifications] | ||
| import com.google.cloud.storage.Notification; | ||
| import com.google.cloud.storage.NotificationInfo; | ||
| import com.google.cloud.storage.NotificationInfo.EventType; | ||
| import com.google.cloud.storage.NotificationInfo.PayloadFormat; | ||
| import com.google.cloud.storage.Storage; | ||
| import com.google.cloud.storage.StorageOptions; | ||
| import java.util.Map; | ||
|
|
||
| public class CreateBucketPubSubNotification { | ||
|
|
||
| public static void createBucketPubSubNotification( | ||
| String bucketName, | ||
| String topicName, | ||
| Map<String, String> customAttributes, | ||
| EventType[] eventTypes, | ||
| String objectNamePrefix, | ||
| PayloadFormat payloadFormat) { | ||
| // The ID to give your GCS bucket | ||
| // String bucketName = "your-unique-bucket-name"; | ||
|
|
||
| // The name of the topic you would like to create a notification for | ||
| // String topicName = "projects/{your-project}/topics/{your-topic}"; | ||
|
|
||
| // Any custom attributes | ||
| // Map<String, String> customAttributes = Map.of("label", "value"); | ||
|
|
||
| // The object name prefix for which this notification configuration applies | ||
| // String objectNamePrefix = "blob-"; | ||
|
|
||
| // Desired content of the Payload | ||
| // PayloadFormat payloadFormat = PayloadFormat.JSON_API_V1.JSON_API_V1; | ||
|
|
||
| Storage storage = StorageOptions.newBuilder().build().getService(); | ||
| NotificationInfo notificationInfo = | ||
| NotificationInfo.newBuilder(topicName) | ||
| .setCustomAttributes(customAttributes) | ||
| .setEventTypes(eventTypes) | ||
| .setObjectNamePrefix(objectNamePrefix) | ||
| .setPayloadFormat(payloadFormat) | ||
| .build(); | ||
| Notification notification = storage.createNotification(bucketName, notificationInfo); | ||
| System.out.println("Successfully created notification for topic " + notification.getTopic()); | ||
| } | ||
| } | ||
| // [END storage_create_bucket_notifications] |
41 changes: 41 additions & 0 deletions
41
...les/snippets/src/main/java/com/example/storage/bucket/DeleteBucketPubSubNotification.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * Copyright 2022 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.storage.bucket; | ||
|
|
||
| // [START storage_delete_bucket_notification] | ||
| import com.google.cloud.storage.Storage; | ||
| import com.google.cloud.storage.StorageOptions; | ||
|
|
||
| public class DeleteBucketPubSubNotification { | ||
|
|
||
| public static void deleteBucketPubSubNotification(String bucketName, String notificationId) { | ||
| // The ID to give your GCS bucket | ||
| // String bucketName = "your-unique-bucket-name"; | ||
|
|
||
| // The NotificationId for the notification you would like to delete | ||
| // String notificationId = "your-unique-notification-id" | ||
|
|
||
| Storage storage = StorageOptions.newBuilder().build().getService(); | ||
| boolean success = storage.deleteNotification(bucketName, notificationId); | ||
| if (success) { | ||
| System.out.println("Successfully deleted notification"); | ||
| } else { | ||
| System.out.println("Failed to find notification"); | ||
| } | ||
| } | ||
| } | ||
| // [END storage_delete_bucket_notification] |
39 changes: 39 additions & 0 deletions
39
samples/snippets/src/main/java/com/example/storage/bucket/ListPubSubNotifications.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * Copyright 2022 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.storage.bucket; | ||
|
|
||
| // [START storage_list_bucket_notifications] | ||
| import com.google.cloud.storage.Notification; | ||
| import com.google.cloud.storage.Storage; | ||
| import com.google.cloud.storage.StorageOptions; | ||
| import java.util.List; | ||
|
|
||
| public class ListPubSubNotifications { | ||
|
|
||
| public static void listPubSubNotifications(String bucketName) { | ||
| // The ID to give your GCS bucket | ||
| // String bucketName = "your-unique-bucket-name"; | ||
|
|
||
| Storage storage = StorageOptions.newBuilder().build().getService(); | ||
| List<Notification> notificationList = storage.listNotifications(bucketName); | ||
| for (Notification notification : notificationList) { | ||
| System.out.println( | ||
| "Found notification " + notification.getTopic() + " for bucket " + bucketName); | ||
| } | ||
| } | ||
| } | ||
| // [END storage_list_bucket_notifications] | ||
39 changes: 39 additions & 0 deletions
39
samples/snippets/src/main/java/com/example/storage/bucket/PrintPubSubNotification.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * Copyright 2022 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.storage.bucket; | ||
|
|
||
| // [START storage_print_pubsub_bucket_notification] | ||
| import com.google.cloud.storage.Notification; | ||
| import com.google.cloud.storage.Storage; | ||
| import com.google.cloud.storage.StorageOptions; | ||
|
|
||
| public class PrintPubSubNotification { | ||
|
|
||
| public static void printPubSubNotification(String bucketName, String notificationId) { | ||
| // The ID to give your GCS bucket | ||
| // String bucketName = "your-unique-bucket-name"; | ||
|
|
||
| // The Pub/Sub topic you would like to find | ||
| // String notificationId = "your-unique-notification-id" | ||
|
|
||
| Storage storage = StorageOptions.newBuilder().build().getService(); | ||
| Notification notification = storage.getNotification(bucketName, notificationId); | ||
| System.out.println( | ||
| "Found notification " + notification.getTopic() + " for bucket " + bucketName); | ||
| } | ||
| } | ||
| // [END storage_print_pubsub_bucket_notification] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.